0% found this document useful (0 votes)
30 views3 pages

Class 4

The document discusses Python operators and conditional statements. It describes 7 types of operators - arithmetic, comparison, assignment, logical, membership, identity, and bitwise operators. It also explains Python uses indentation rather than curly braces to define code blocks for conditional statements, functions and loops. Examples are given to find the biggest and smallest number among three variables using if-elif-else conditional statements.

Uploaded by

Tarique Ejaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views3 pages

Class 4

The document discusses Python operators and conditional statements. It describes 7 types of operators - arithmetic, comparison, assignment, logical, membership, identity, and bitwise operators. It also explains Python uses indentation rather than curly braces to define code blocks for conditional statements, functions and loops. Examples are given to find the biggest and smallest number among three variables using if-elif-else conditional statements.

Uploaded by

Tarique Ejaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

+++++++++++++++++++++++

Class 4 notes
+++++++++++++++++++++++

*** Operators ***


***************
There are 7 Operators in Python
--------------------------------------
1. Arithmetic Operators
2. Comparison/Relational Operators
3. Assignment Operators
4. Logical Operators
5. Membership Operators
6. Identity Operators
7. Bitwise Operators

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

**** Conditional Statements ****


****************************

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

Get biggest number


------------------------
Variable: a,b,c=10,20,30
if a>b and a>c:
print('a %d is biggest number' % a)
elif b>a and b>c:
print('b %d is biggest number' % b)
else:
print('c %d is biggest number' % c)

Get smallest number


-------------------
Variable: a,b,c=10,20,30
if a<b and a<c:
print('(a=%d) is smallest number' % a)
elif b<a and b<c:
print('(b=%d) is smallest number' % b)
else:
print('(c=%d) is smallest number' % c)

You might also like