Class 3
Class 3
if Statement
The if statement evaluates a condition. If the condition is True , the block of code inside
the if statement is executed. If the condition is False , the block of code is skipped.
# Basic structure of an if statement if condition: # Block of code to execute if condition is True pass
In [1]: x = 10
if x > 5:
print("x is greater than 5")
x is greater than 5
enter a number: 4
else Statement
The else statement follows an if statement and executes a block of code if the if
condition is False
# Basic structure of an if-else statement if condition: # Block of code to execute if condition is True pass else: #
Block of code to execute if condition is False pass
In [3]: x = 3
if x > 5:
print("x is greater than 5")
else:
print("x is not greater than 5")
if x > 5:
print("x is greater than 5")
else:
print("x is not greater than 5")
enter a number: 4
x is not greater than 5
elif Statement
The elif (short for "else if") statement allows you to check multiple conditions. It must
follow an if statement or another elif statement. If the if condition is False , the
program checks the elif condition. If the elif condition is True , its block of code is
executed. You can have multiple elif statements.
# Basic structure of an if-elif-else statement if condition1: # Block of code to execute if condition1 is True pass
elif condition2: # Block of code to execute if condition1 is False and condition2 is True pass else: # Block of
if x > 5:
print("x is greater than 5")
elif x == 5:
print("x is equal to 5 ")
else:
print("x is less than 5")
x is greater than 5
Nested if Statements
You can nest if , elif , and else statements inside one another to create more complex
conditions.
if x > 10:
print("x is greater than 10")
if x > 20:
print("x is also greater than 20")
else:
print("x is not greater than 20")
else:
print("x is not greater than 10")
enter a number: 45
x is greater than 10
x is also greater than 20
Combining Conditions
You can combine conditions using logical operators such as and , or , and not .
In [7]: x = 8
y = 12
In [8]: x = 2
y = 12
Indentation
Python relies on indentation (whitespace at the beginning of a line) to define scope in the
code. Other programming languages often use curly-brackets for this purpose.
In [9]: a = 33
b = 200
if b > a:
print("b is greater than a") # you will get an error
Python Loops
Python has two primitive loop commands:
while loops
for loops
1. while loop
A while loop in Python repeatedly executes a block of code as long as a specified
condition is True .
It is useful when the number of iterations is not known beforehand and depends on a
certain condition being met.
1
2
3
4
5
In [11]: print(count)
In [12]: # example where we use a while loop to calculate the sum of numbers from 1 to 10:
total = 0
number = 1
break Statement
With the break statement we can stop the loop even if the while condition is true
In [13]: i = 1
while i < 6:
print(i)
if i == 3:
break
i += 1
1
2
3
continue Statement
With the continue statement we can stop the current iteration, and continue with the
next
In [14]: i = 0
while i < 10:
i += 1
if i == 7:
continue
print(i)
1
2
3
4
5
6
8
9
10
In [15]: count = 0
if count % 2 == 0:
continue # Skip the rest of the loop for even numbers
if count > 7:
break # Exit the loop if count is greater than 7
print(count)
1
3
5
7
In [16]: i = 1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")
2. for Loop
The for loop in Python is used to iterate over a sequence (like a list, tuple, dictionary, set,
or string).
for n in numbers:
square = n ** 2
print(f"The square of {n} is {square}")
The square of 1 is 1
The square of 2 is 4
The square of 3 is 9
The square of 4 is 16
The square of 5 is 25
A
p
p
l
e
enumerate Function
In [20]: eic
'apples'
Out[20]:
In [21]: oic
Nested Loops
i = 0, j = 0
i = 0, j = 1
i = 1, j = 0
i = 1, j = 1
0
1
2
3
4
0
1
2
3
4
5
0
1
2
3
4
5
7
8
9
0
1
2
3
4
5
6
7
8
9
In [31]: n = 5
for i in range(n):
for j in range(n - i - 1):
print(" ", end="")
for k in range(2 * i + 1):
print("*", end="")
print()
*
***
*****
*******
*********
In [32]: X="MBUAMNBDARIA"
In [33]: X[::2]
'MUMBAI'
Out[33]:
In [34]: X[1::2]
'BANDRA'
Out[34]: