Class 4
Class 4
Class 4 notes
+++++++++++++++++++++++
1. Arithmetic Operators:
Variable example: a,b=10,20
----------------------------------
i) addition + [example: a+b = 30]
ii) subtraction - [example: a-b = -10]
iii) Multiplication * [example: a*b = 200]
iv) Division / [example: b/a = 2]
v) Floor diision // [example: b//a = 2, b//3 = 6]
vi) Modulus % [example: a%3 = 1] remainder
vii) Exponent ** [example: a**2 = 100] power
2. Comparison/Relational Operators:
Variable example: a,b=10,20
-----------------------------------
i) Equal compare == [example: a==b = False]
ii) Not equal != [example: a!=b = True]
iii) Greater than > [example: a>b = False]
iv) Less than < [example: a<b = True]
v) Greater than or equal to >= [example: a>=b = False]
vi) Less than or equal to <= [example: a<=b = True]
3. Assignment Operators:
Variable example: a,b=10,20
-----------------------------------
i) = [example: a = 10]
ii) += [example: a+=5, a = a + 5]
iii) -= [example: a-=5, a = a - 5]
iv) *= [example: a*=5, a = a * 5]
v) /= [example: a/=5, a = a / 5]
vi) //= [example: a//=5,a = a // 5]
vii) %= [example: a%=5, a = a % 5]
viii) **= [example: a**=5,a = a ** 5]
4. Logical Operators:
Variable example: a,b=10,20
----------------------------------
i) and [example: a<5 and b>10 = False]
ii) or [example: a<5 or b>10 = True]
iii) not [example: not(a<5) = True, not(a>5) = False]
5. Membership operators
List: a = [10,20,30]
---------------------------
i) in [example: (10 in a) = True]
ii) not in [example: (10 not in a) = False]
6. Identity Operators
Variable example:
a=b=10
c=20
d=a
---------------------------
Identity operators compare the memory locations of two objects.
i) is [example: (a is b) = True, (a is c) = False, (a is d) = True]
ii) is not [example: (a is not b) = Fase, (a is not c) = True, (a is not d) = False]
7. Bitwise Operators
Bitwise operator works on bits and performs bit by bit operation.
Assume if a = 20 and b = 13
Byte position 7 6 5 4 3 2 1 0
128 64 32 16 8 4 2 1
a = 20 1 0 1 0 0
b = 13 1 1 0 1
Indentation
--------------
In Python {} are not available to define the body of conditional statement, functions, loop etc.
For define body we use 'indentation'
Syntax:
---------
if <condition>:
#statement
else:
#statement
Variable: a,b=10,20
if a<b:
print('a is less than b')
print('%d is less than %d' % (a, b))
else:
print('a is greater than b')
print('%d is greater than %d' % (a, b))
if <condition>:
#statement
elif <condition>:
#statement
else:
#statement