You are on page 1of 18

AGHU EDUCATIONAL INSTITUTIONS

Python Control Statements File Handling


Decision Making


if

if else

elif

Nested if

loops
if - statement

if statement syntax:
if test expression:
statement(s)

In Python, the body of the if statement is indicated by the indentation.


Body starts with an indentation and the first un indented line marks the end.
Example:

num = 3

if num > 0:

print(num, "is a positive number.")

print("This is always printed.")
if else
Syntax of if...else:

if test expression:
Body of if
else:
Body of else

body of if only when test condition is True. If the condition is False, body
of else is executed. Indentation is used to separate the blocks.

Example:
if num >= 0:
print("Positive or Zero")
else:
print("Negative number")
elif
Syntax of if...elif...else:

if test expression:

Body of if

elif test expression:

Body of elif

else:

Body of else
• The elif is short for else if. It allows us to check for multiple expressions.
• If the condition for if is False, it checks the condition of the next elif block
and so on.
• If all the conditions are False, body of else is executed.
• Only one block among the several if...elif...else blocks is executed according
to the condition.
Multiple elif blocks

• The if block can have only one else block. But it can have
multiple elif blocks.
Example:

if num > 0:

print("Positive number")

elif num == 0:

print("Zero")

else:

print("Negative number")
Nested if

• We can have a if...elif...else statement inside


another if...elif...else statement. This is called nesting in
computer programming.

• Any number of these statements can be nested inside one


another. Indentation is the only way to figure out the level of
nesting.
Nested if

– num = float(input("Enter a number: "))


– if num >= 0:
– if num == 0:
– print("Zero")
– else:
– print("Positive number")
– else:
– print("Negative number")
Output 1
Enter a number: 5
Positive number
Output 2
Enter a number: -1
Negative number
Output 3
Enter a number: 0
Zero
Loops
Loops are often used when we have a piece of code that we need to
repeat for (i) N number of times or (ii) until a condition becomes true or
false

Concepts in loops:
– For loop
– While loop
– Nested loops
– Break
– Continue
for loop
for, is often used when we have a piece of code which we want to repeat "n" number of
times.
Example:
for i in range(1,5):
print(i)
Output:
1
2
3
4

Range function iterates i value starting from 1st argument up to but not including 2nd
argument. So i value is iterated only up to 4, but not 5
for loop

The third argument in range function indicates the difference between each number in the
sequence:

Example:
for i in range(1,10,2):
print(i)

Output:
1
3
5
7
9
for loop

Difference can also have a negative value


Example:
for i in range(0,-10,-2):
print(i)

Output:
0
-2
-4
-6
-8
for loop
The for loop can also be used to iterate over elements of a sequence such as lists
Example:
computer_brands = ["Apple", "Asus", "Dell", "Samsung"]
for brands in computer_brands:
print(brands)
# prints all the items in list

Example:
for i in range(0,len(computer_brands)):
print(computer_brands[i])
# this is an alternative way of printing all items in a list
while loop
A while loop statement in Python programming language repeatedly
executes a target statement as long as a given condition is true.

Example:
– computer_brands = ["Apple", "Asus", "Dell", "Samsung"]
– i=0
– while i < len(computer_brands):
– print computer_brands(i)
– i=i+1

This reads, as long as the value of the variable i is less than the length of
the list (computer_brands), and prints out each item
Nested loops
Python programming language allows to use one loop inside another loop.

Syntax for nested for loop:


for iterating_var in sequence:
for iterating_var in sequence:
statements(s)
statements(s)

syntax for a nested while loop:


while expression:
while expression:
statement(s)
statement(s)
Nested loops
Example :
– num_list = [1, 2, 3]
– alpha_list = ['a', 'b', 'c']
– for number in num_list:
print(number)
for letter in alpha_list:
print(letter)
– Output:
– 1
– a
– b
– c
– 2
– a
– b
– c
– 3
– a
– b
– c
Break statement
The break statement terminates the loop containing it. Control of the
program flows to the statement immediately after the body of the loop.

Example:

for val in "string":
if val == "i":
break
print(val)
Output:
s
t
r
• If break statement is inside a nested loop (loop inside another loop), break will
terminate the innermost loop.
Continue Statement
The continue statement is used to skip the rest of the code inside a loop for the current
iteration only. Loop does not terminate but continues on with the next iteration.

Example:
for val in "string":
if val == "i":
continue
print(val)
Output:
s
t
r
n
g

You might also like