Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

02 Flow Control

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

PYTHON

2. Python Flow Control


Python if...else Statement
# If the number is positive, we print an appropriate
message

num = 3
if num > 0:
print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
print(num, "is a positive number.")
print("This is also always printed.")
Python if...else Statement
num = 3

if num >= 0:
# num is >= 0
print("Positive or Zero")
else:
# num is < 0
print("Negative number")
Python if...else Statement
'''In this program, we check if the number is positive or
negative or zero and display an appropriate message'''

num = 3.4

if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
Python Nested if Example
'''In this program, we input a number check if the number is
positive or negative or zero and display an appropriate
message This time we use nested if statement'''

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


if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
Which one is better?

num = 3.4 num = 3.4

if num > 0: if num >= 0:


print("Positive number") if num == 0:
elif num == 0: print("Zero")
print("Zero") else:
else: print("Positive number")
print("Negative number") else:
print("Negative number")
Which one is better?

num = 0 num = 0

if num > 0: if num >= 0:


print("Positive number") if num == 0:
elif num == 0: print("Zero")
print("Zero") else:
else: print("Positive number")
print("Negative number") else:
print("Negative number")
Which one is better?

num = -1 num = -1

if num > 0: if num >= 0:


print("Positive number") if num == 0:
elif num == 0: print("Zero")
print("Zero") else:
else: print("Positive number")
print("Negative number") else:
print("Negative number")
for loop
The for loop in Python is used to iterate over a sequence (list, tuple, string) or other iterable objects. Iterating over a
sequence is called traversal.

# Program to find the sum of all numbers


stored in a list

# List of numbers
numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]

# variable to store the sum


sum = 0

# iterate over the list


for val in numbers:
sum = sum+val

print("The sum is", sum)


The range() function
range(10) will generate numbers from 0 to 9 (10 numbers).

print(range(10)) # Program to iterate through a list using


Output: range(0, 10) indexing

print(list(range(10))) fruit = ['Apple', 'Orange', 'Mango']


Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# iterate over the list using index
print(list(range(2, 8))) for i in range(len(fruit)):
Output: [2, 3, 4, 5, 6, 7] print("I like", fruit[i])

print(list(range(2, 20, 3)))


Output: [2, 5, 8, 11, 14, 17]
for loop with else
A for loop can have an optional else block. The else part is executed if the items in the sequence used
in for loop exhausts.

The break keyword can be used to stop a for loop. In such cases, the else part is ignored.

Hence, a for loop's else part runs if no break occurs.


# program to display student's marks from record
student_name = 'Saleem'

marks = {'Ahmad': 90, 'Mohmd': 95, 'Samer': 55}

for student in marks:


if student == student_name:
print(marks[student])
break
else:
print('No entry with that name found.')
Python while Loop
# Program to add natural numbers up to n
# sum = 1+2+3+...+n

n = 10

# initialize sum and counter


sum = 0
i = 1

while i <= n:
sum = sum + i
i = i+1 # update counter

# print the sum


print("The sum is", sum)
Python while Loop
The else part is executed if the condition in the while loop evaluates to False.

'''Example to illustrate the use of else statement


with the while loop'''

counter = 0

while counter < 3:


print("Inside loop")
counter = counter + 1
else:
print("Inside else")

Output:
Inside loop
Inside loop
Inside loop
Inside else
Python break and continue

for var in sequence:


# Codes inside for loop
if condition:
break
# Codes inside for loop
# Codes outside for loop

While test expression:


# Codes inside while loop
if condition:
break
# Codes inside while loop
# Codes outside while loop
Python break and continue

for var in sequence:


# Codes inside for loop
if condition:
continue
# Codes inside for loop
# Codes outside for loop

While test expression:


# Codes inside while loop
if condition:
continue
# Codes inside while loop
# Codes outside while loop
Python pass statement
The pass statement is a null statement. The difference between a comment and a pass statement in
Python is that while the interpreter ignores a comment entirely, pass is not ignored.
However, nothing happens when the pass is executed. It results in no operation (NOP).

Example '''pass is just a placeholder for


functionality to be added later.'''
sequence = {'p', 'a', 's', 's'}
for val in sequence:
pass

Example def function(args):


pass

Example class Example:


pass

You might also like