2. Python Operators and Control Flow Statements
2. Python Operators and Control Flow Statements
Operators
1. Arithmetic Operators:
2. Comparison/Relational Operators:
3. Assignment Operator:
4. Logical Operators:
5. Bitwise Operators:
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.
7. Identity Operators:
Conditional Statements / Decision Making Statements
1. 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.
Syntax:
if expression:
statement
2. if-else Statement:
The if-else statement provides an else block combined with the if statement which
is executed in the false case of the condition.
If the condition is true, then the if-block is executed. Otherwise, the else-block is
executed.
Syntax:
if condition:
#block of statements
else:
#Another block of statements (else-block)
3. elif Statement:
The elif statement enables us to check multiple conditions and execute the specific
block of statements depending upon the true condition among them.
However, using elif is optional.
Syntax:
if expression 1:
# block of statements
elif expression 2:
# block of statements
elif expression 3:
# block of statements
else:
# block of statements
4. Nested if Statements:
When a programmer writes one if statements inside another if statement then it is
called a nested if statement.
Syntax:
if condition1:
if condition2:
statement1
else:
statemen3
Looping in Python
1. for Loop:
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.
Syntax:
for iterating_var in sequence:
statement(s)
3. while Loop:
It is a most basic looping statement in Python programming. It execute a sequence
of statements repeatedly as long as a condition is true.
Syntax:
X = 0 #initialize counter
while condition:
# statements
# increment / decrement
Loop Manipulation
1. continue
The continue statement in python is used to bring the program control to the
beginning of the loop.
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.
Syntax:
continue
2. pass
In Python, pass keyword is used to execute nothing.
It means, when we don't want to execute code, the pass can be used to execute
empty.
Syntax:
pass
3. break
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.
Syntax:
break
4. else
The else block is executed when the condition given in the loop statement
becomes false.
Syntax:
Else
❖ Write a program to print following
1
12
123
1234
for i in range(1,5):
for j in range(1,i+1):
print(j,end=' ')
print()
output:
i is 20
a=2
for i in range(1,5):
for j in range(i):
print(a,end="\t")
a+=2
print()