Unit 2
Unit 2
OPERATORS IN PYTHON
a. Arithmetic operators: The symbols used to perform arithmetic operations are: +,-,*,/,//,%,**
Below table shows the description of these operators.
B. Logical operators: These operators take conditional statements as an expression and return
True or False if the expression evaluates true or false. Symbols used are : and ,or , not
C. Comparison/Relational operators
This type of operator is used to compare two values and returns True or False. The symbols used
are: > ,< , >=, <=, ==, !=
<= Less than or equal Returns True if the first operand is less a=10;b=20,c=10
to than or equal to the second else returns print(a<=b,a>=c)
false. #output: True,True
D. Bitwise operators
This type of operator is used to perform the operations on the given data at the bit-level. When a
number is given , these operators use its binary value (0’s and 1’s) to perform arithmetic
computation and return the result. The symbols used are: &, |, ^, << , >>, ~
& Bitwise and Sets each bit to 1 if both bits are 1 else a=5;b=4
sets to 0 in the given operands in bit print(a&b)
level #output: 4
^ Bitwise xor Sets each bit to 1 if only one of the bits a=5;b=4
is 1 else sets to 0 in the given operands print(a^b)
in bit level #output: 1
<< Bitwise left-shift Each bit in the first operand Shifts left a=5;b=4
by pushing zeros in from the right and print(a<<b)
the leftmost bits will be discarded. #output: 80
This shifting will be repeated upto
second operand times.
>> Bitwise right-shift Each bit in the first operand Shifts a=60;b=4
right by pushing zeros in from the left print(a>>b)
and the rightmost bits will be #output: 3
discarded. This shifting will be
repeated upto second operand times.
+= a=3;b=4 a=3;b=4
a+=b a=a+b
print(a) print(a)
#output:7 #output:7
-= a=3;b=4 a=3;b=4
a-=b a=a-b
print(a) print(a)
#output:-1 #output:-1
*= a=3;b=4 a=3;b=4
a*=b a=a*b
print(a) print(a)
#output:12 #output:12
/= a=3;b=4 a=3;b=4
a/=b a=a/b
print(a) print(a)
#output:0.75 #output:0.7
5
%= a=3;b=4 a=3;b=4
a%=b a=a%b
print(a) print(a)
#output:3 #output:3
**= a=3;b=4 a=3;b=4
a**=b a=a**b
print(a) print(a)
#output:81 #output:81
|= a=3;b=4 a=3;b=4
a|=b a=a|b
print(a) print(a)
#output:7 #output:7
^= a=3;b=4 a=3;b=4
a^=b a=a^b
print(a) print(a)
#output:7 #output:7
F. Identity operators:
These operators are used to compare two objects and check if both objects are referring to
the same memory location or not. Two keywords are used for comparing objects: is and is not
is not Returns true if two objects are not same and a=10; b=a;c=20
not referring to same memory location print(c is not a)
print( b is not a)
#output: True False
G. Membership operators
These operators are used to check for the existence of a value in a given sequence of data.
Two keywords are used for comparing objects: in and not in
OPERATOR PRECEDENCE
In python, we follow a special rule named as PEMDAS rule to specify the order of evaluating a
given arithmetic expression.
If an expression contains arithmetic operators then as per PEMDAS rule those operators will be
evaluated first.
PEMDAS stands for Parenthesis (), Exponent (**), Multiplication (*) and Division(/, //) ,
Addition(+) and Subtraction(-)
As per the above rule, first priority is given to (), second priority is for Exponent operator(**),
third priority is for *,/,// and fourth priority is for +,-.
As *,/,// and +,- are having equal precedence, the given expression gets evaluated from left to
right occurrence of the operator.
Operator Description
** Exponentiation
+,- addition,subtraction
5. type(): This method is used to specify the data type of a given variable or a value.
6. eval() : This method is used to evaluate the given arithmetic expression. Expression will
be given as a string.
Example:
7. exec(): This function is used to execute the set of statements given as a string.
Example:
8. hex(): This function is used to return the hexadecimal value of a given number.
Example:
9. oct(): This function is used to return the octal value of a given number.
Example:
10. bin(): This function is used to return the binary value of a given number.
Example:
11. max(): This function returns the maximum value among the list of numbers.
Example:
12. min(): This function returns the minimum value among the list of numbers.
Example:
13. pow(a,b): This function returns the exponent value given numbers a,b. Returns a power
b.
Example:
15. dir(): display the list of variables, functions of the given object.
Example: Displaying the list of methods and variables in a tuple
CONDITIONAL STATEMENTS
These statements are also called Control Statements which are crucial for any programming
language.
Control statements are used to control the flow of execution in a program.
They are used to perform some tasks like:
i) executing statements through decision making
ii) executing statements repeatedly more than once
iii) skipping statements and jump to other statements
In python, conditional statements are classified into three:
a. Selection statements
b. Iteration statements
c. Jump statements
Without these ,the program should perform sequential execution upto nth statement.
Example-1 Output:
a=10 before if
print(“before if”) after if block
If a>20:
print(“inside if block”)
print(“a is greater than 20”)
print(“after if block”)
Example-2 Output:
a=100 before if
print(“before if”) inside if block
If a>20: a is greater than 20
print(“inside if block”) after if block
print(“a is greater than 20”)
print(“after if block”)
Example-2 Output:
a=100 before if
print(“before if”) inside if block
If a>20: a>b
print(“inside if block”) after if-else block
print(“a is greater than 20”)
print(“after if block”)
Example: Output:
a=56 between 50 and 100
if a>0 and a<20:
print(“ between 0 and 20”)
elif a>20 and a<50:
print(“between 20 and 50”)
elif a>50 and a<100:
print(“between 50 and 100”)
else:
print(“ value > 100”)
d. Nested if-else statements: An if statement inside another if statement or else
statement or if-else statement inside another if statement or else statement.
Syntax-1: Flowchart-1
if expr-1:
if expr-2:
Stmt-1
else:
if expr-3:
stmt-2
Syntax-2: Flowchart-2
if expr-1:
if expr-2:
Stmt-1
else:
Stmt-2
else:
if expr-3:
Stmt-3
else:
stmt-4
Example: Output:
iv) Iteration statements: These statements are used to execute a certain set of statements
repeatedly till a given condition is satisfied. To perform an iteration statements, 3 thingsto be
considered:
i. Variable initialization
ii. Iteration condition
iii. Variable updation
Once a variable has been initialized, it will be used for checking an iteration condition. If
the condition is true,then statements inside the condition block will be executed and the iteration
variable should be updated. The control repeats the iteration condition again for evaluating true
or false. This process repeats until the condition becomes false.
During the iteration process, condition, statements inside condition and variable updation will be
executed many times but variable initialization will happen only once. Observe the below
flowchart.
Two keywords are used for iteration statements: while and for statements.
I. While statement: It is called a pre-test iteration statement as the condition is evaluated at
the beginning of an iteration. While statements never execute at all if the condition fails.
Counter based loop → loop runs with a finite condition and a counter variable is
updated always inside the loop to reach the finite condition. User knows how many times
the loop runs.
Event based loop→ loop runs with infinite condition. User doesn't know how many
times the loop runs. To stop this loop, a special if statement code called sentinel code
should be used in the loop to stop the loop unconditionally.
II. Continue : this keyword is used to skip the statements after ‘continue’ in the current
iteration of the loop and repeat the next iteration of the loop.
→ it is used as a sentinel code for event based loops.
→ It cannot be used in outside loops