PYTHON – OPERATORS AND CONDITIONAL STATEMENTS
NOTES (OPERATORS IN PYTHON)
Learning Objective
1. Use different operators in Python Programs.
2. Describe the function of different types of operators
K Know to create a basic programs in python using input()
W Want to learn the function of different types of operators
L
1. Arithmetic Operators
Operators that are used to perform mathematical operations
Operator Function Examples
+ Addition (number) / >>>100+25 >>> “Indian” + “ ” + “School”
Concatenation (Strings) 125 Indian School
- Subtraction >>>100-25
75
* Multiplication(number) / >>>100*25 >>>‘ADIS’*3
Replication (Strings) 2500 ADISADISADIS
/ Division >>>8/5
1.6
// Floor Division (Integer >>>8//5
Division) 1
% Modulus (Reminder) >>>8%5
3
** Exponentiation >>>8**3
512
2. Comparison (Relational) Operators
Used to compare values.
Return either ‘True’ or ‘False’ according to the condition.
Operator Example Output
> >>>50>49 True
>= >>>34>=36 False
< >>>8<5 False
<= >>>14<=27 True
== >>>63==64 False
!= >>>63!=64 True
3. Logical Operators
Used to compare values.
Return either ‘True’ or ‘False’ according to the condition.
Operator Example 1 Example 2 Example 3
and >>>3>2 and 4>2 >>>3>2 and 4>2 and 5==2
True False
or >>>13>5 or 5==5 >>> 13>5 or 5!=5 or 6>5 >>> 2>5 or 5!=5 or 3>5
True True False
not >>>not 5>3 >>>not 4!=4
False True
4. Assignment Operators
Operators that are used to assign value to variables
Operator Examples
= >>>x=5
>>>print(x)
5
+= >>>x=5
>>>x+=2 #x=x+2
>>>print(x)
7
-= >>>x=5
>>>x-=2 #x=x-2
>>>print(x)
3
*= >>>x=5
>>>x*=2 #x=x*2
>>>print(x)
10
/= >>>x=5
>>>x/=2 #x=x/2
>>>print(x)
2.5
NOTES (CONDITIONAL STATEMENTS IN PYTHON)
Learning Objective
1. To use conditional control structures in Python Programing
2. Use if, if-else and if-elif-else control structures in Python Programing
if Statement
‘if’ statement is a decision-making statement. It contains a body of statements to be executed only when the
condition is true
Syntax Example
if condition:
statement 1
statement 2
If-else Statement
if statement will check the condition; if the condition is true then if block is
executed. If the condition is false then else block is executed.
Syntax Example
if condition:
statement 1
statement 2
else:
statement 3
statement 4
If-elif-else Statement
if-elif-else statements are used to make multipath decisions
Syntax Example
if condition:
statement 1
statement 2
elif:
statement 3
statement 4
else:
statement 5
statement 6
Nested if Statement
Nesting is the process of using and if-elif-else statement inside another statement. If enables us to make
complex decisions based on different inputs.