PWP - Unit - 2 - Notes - Part - 1
PWP - Unit - 2 - Notes - Part - 1
BASIC OPERATORS:
o Arithmetic operators
o Comparison operators
o Assignment Operators
= += -= *= %= **= //=
o Logical Operators
and or not
o Bitwise Operators
o Membership Operators
in not in
o Identity Operators
is is not
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS
1. Arithmetic operators
Operator Description
+ (Addition) It is used to add two operands. For example, if a = 20, b = 10 => a+b = 30
- (Subtraction) It is used to subtract the second operand from the first operand. If the first
operand is less than the second operand, the value result negative. For
example, if a = 20, b = 10 => a - b = 10
/ (divide) It returns the quotient after dividing the first operand by the second
operand. For example, if a = 20, b = 10 => a/b = 2
* (Multiplication) It is used to multiply one operand with the other. For example, if a = 20, b =
10 => a * b = 200
% (reminder) It returns the reminder after dividing the first operand by the second
operand. For example, if a = 20, b = 10 => a%b = 0
// (Floor It gives the floor value of the quotient produced by dividing the two
division) operands.
2. Comparison/Relational operators
Operator Description
== If the value of two operands is equal, then the condition becomes true.
!= If the value of two operands is not equal then the condition becomes true.
<= If the first operand is less than or equal to the second operand, then the condition
becomes true.
>= If the first operand is greater than or equal to the second operand, then the condition
becomes true.
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS
> If the first operand is greater than the second operand, then the condition becomes
true.
< If the first operand is less than the second operand, then the condition becomes true.
3. Assignment operator
Operator Description
= It assigns the the value of the right expression to the left operand.
+= It increases the value of the left operand by the value of the right operand and
assign the modified value back to left operand. For example, if a = 10, b = 20 => a+
= b will be equal to a = a+ b and therefore, a = 30.
-= It decreases the value of the left operand by the value of the right operand and
assign the modified value back to left operand. For example, if a = 20, b = 10 => a- =
b will be equal to a = a- b and therefore, a = 10.
*= It multiplies the value of the left operand by the value of the right operand and
assign the modified value back to left operand. For example, if a = 10, b = 20 => a*
= b will be equal to a = a* b and therefore, a = 200.
%= It divides the value of the left operand by the value of the right operand and assign
the reminder back to left operand. For example, if a = 20, b = 10 => a % = b will be
equal to a = a % b and therefore, a = 0.
**= a**=b will be equal to a=a**b, for example, if a = 4, b =2, a**=b will assign 4**2 = 16
to a.
//= A//=b will be equal to a = a// b, for example, if a = 4, b = 3, a//=b will assign 4//3
= 1 to a.
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS
4. Logical operators
Operator Description
and If both the expression are true, then the condition will be true. If a and b are the
two expressions, a → true, b → true => a and b → true.
or If one of the expressions is true, then the condition will be true. If a and b are
the two expressions, a → true, b → false => a or b → true.
not If an expression a is true then not (a) will be false and vice versa.
5. Bitwise operators
The bitwise operators perform bit by bit operation on the values of the two operands.
For example,
if a = 7;
b = 6;
then, binary (a) = 0111
binary (b) = 0011
Operator Description
& (binary If both the bits at the same place in two operands are 1, then 1 is copied to
and) the result. Otherwise, 0 is copied.
| (binary or) The resulting bit will be 0 if both the bits are zero otherwise the resulting
bit will be 1.
^ (binary The resulting bit will be 1 if both the bits are different otherwise the
xor) resulting bit will be 0.
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS
~ (negation) It calculates the negation of each bit of the operand, i.e., if the bit is 0, the
resulting bit will be 1 and vice versa.
<< (left shift) The left operand value is moved left by the number of bits present in the
right operand.
>> (right The left operand is moved right by the number of bits present in the right
shift) operand.
6. Membership operators
Python membership operators are used to check the membership of value inside a Python
data structure. If the value is present in the data structure, then the resulting value is true
otherwise it returns false.
Operator Description
in It is evaluated to be true if the first operand is found in the second operand (list,
tuple, or dictionary).
not in It is evaluated to be true if the first operand is not found in the second operand
(list, tuple, or dictionary).
7. Identity Operators
Operator Description
is It is evaluated to be true if the reference present at both sides point to the same
object.
is not It is evaluated to be true if the reference present at both side do not point to the
same object.
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS
8. Python Operator Precedence
The precedence of the operators is important to find out since it enables us to know
which operator should be evaluated first. The precedence table of the operators in
python is given below.
Operator Description
** The exponent operator is given priority over all the others used in the
expression.
<= < > >= Comparison operators (less then, less then equal to, greater then, greater
then equal to).
The if statement
The if statement is used to test a particular condition and if the condition is true, it executes
a block of code known as if-block. The condition of if statement can be any valid logical
expression which can be either evaluated to true or false.
if expression:
statement
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS
Example
The if-else statement provides an else block combined with the if statement which is
executed in the false case of the condition.
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS
If the condition is true, then the if-block is executed. Otherwise, the else-block is executed.
if condition:
#block of statements
else:
#another block of statements (else-block)
Example 1 : Program to check whether a person is eligible to vote or not.
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS
The elif statement enables us to check multiple conditions and execute the specific block of
statements depending upon the true condition among them.
if expression 1:
# block of statements
elif expression 2:
# block of statements
elif expression 3:
# block of statements
else:
# block of statements
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS
Example 1
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS
Example 2
Loop Description
Statement
for loop The for loop is used in the case where we need to execute some part of the code
until the given condition is satisfied. The for loop is also called as a per-tested
loop. It is better to use for loop if the number of iteration is known in advance.
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS
while loop The while loop is to be used in the scenario where we don't know the number of
iterations in advance. The block of statements is executed in the while loop until
the condition specified in the while loop is satisfied. It is also called a pre-tested
loop.
do-while The do-while loop continues until a given condition satisfies. It is also called post
loop tested loop. It is used when it is necessary to execute the loop at least once
(mostly menu driven programs).
The for loop in Python is used to iterate the statements or a part of the program several
times. It is frequently used to traverse the data structures like list, tuple, or dictionary.
Python allows us to nest any number of for loops inside a for loop.
The inner loop is executed n number of times for every iteration of the outer loop.
Example 1
n = int(input("Enter the number of rows you want to print?"))
i,j=0,0
for i in range(0,n):
print()
for j in range(0,i+1):
print("*",end="")
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS
Output:
Example 1
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS
Example 2:
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS
The else block is executed when the condition given in the while statement becomes false.
if the while loop is broken using break statement, then the else block will not be executed
and the statement present after else block will be executed.
The continue statement in python is used to bring the program control to the beginning of
the loop.
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS
The continue statement skips the remaining lines of code inside the loop and start with the
next iteration.
It is mainly used for a particular condition inside the loop so that we can skip some specific
code for a particular condition.
1. #loop statements
2. continue;
3. #the code to be skipped
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS
Example 2
Python Pass
It means, when we don't want to execute code, the pass can be used to execute empty.
pass
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS
The break statement breaks the loops one by one, i.e., in the case of nested loops, it breaks
the inner loop first and then proceeds to outer loops.
break is used to abort the current execution of the program and the control goes to the next
line after the loop.
The break is commonly used in the cases where we need to break the loop for a given
condition.
#loop statements
break;
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS
CO: DEVELOP PYTHON PROGRAM TO DEMONSTRATE USE OF OPERATORS