Conditions Statetements
Conditions Statetements
Flow Control:
1. if...else Statement
2. Loops
For Loop
While Loop
3. break and continue
4. pass Statement
if...else Statement
• if condition:
# body of if statement
flow_if_exercise()
if...else Statement
• number = 5
# outer if statement
if number >= 0:
# inner if statement
if number == 0:
print('Number is 0')
# inner else statement
else:
print('Number is positive')
# outer else statement
else:
print('Number is negative')
pass Statement
if statements cannot be empty, but for some reason have an if statement with
no content, put in the pass statement to avoid getting an error.
Example:
# pass statement
a = 33
b = 200
if b > a:
pass
Combined Example
# this program is about flow control
a = 12
b = 13
c = 90
def if_statment():
if (a > b):
print("a is greater than b")
def if_else_statment():
if (a > b):
print("a is greater than b")
else:
print("a is less than b")
def if_elif_else_statment():
if (a > b):
print("a is greater than b")
elif (a == b):
print("a is equal to b")
else:
print("a is less than b")
def nested_if_statment():
if (a > b):
print("a is greater than b")
if (a == b):
print("a is equal to b")
else:
print("a is less than b")
def if_pass_statment():
if (a > b):
pass
else:
print("a is less than b")
def if_elif_else_new_sameple():
if(c >= 95):
print('Assign grade A')
elif(c >= 90 and c < 95):
print('Assign grade B')
elif(c >= 80 and c < 90):
print('Assign grade C')
else:
print('Assign grade D')
if_elif_else_new_sameple()
if_statment()
if_else_statment()
if_elif_else_statment()
nested_if_statment()
if_pass_statment()
For Loops
• A for loop is used for iterating over a sequence (that is either a list, a tuple,
a dictionary, a set, or a string).
Syntax:
for var in iterable:
# statements
Example
Ex: 1
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
Ex: 2
languages = ['Swift', 'Python', 'Go', 'JavaScript‘]
for language in languages:
print(language)
Looping Through a String
Example:
Loop through the letters in the word "banana“
for x in "banana":
print(x)
For Loop in Dictionary
d = dict()
d['xyz'] = 123
d['abc'] = 345
for i in d:
print("% s % d" % (i, d[i]))
Loop with range() function
Example:
values = range(4)
Here, 4 inside range() defines a range containing values 0, 1, 2, 3.
Example
Ex: 1
# use of range() to define a range of values
values = range(4)
for i in values:
print(i)
Ex: 2
for i in range(6):
print(i)
Using the start parameter
Example:
Example:
for x in range(2, 30, 3):
print(x)
Using a for Loop Without Accessing Items
• A for loop can have an optional else block. The else part is executed when
the loop is exhausted (after the loop iterates through every item of a
sequence).
Example:
digits = [0, 1, 5]
for i in digits:
print(i)
else:
print("No items left.")
For loop with else
• The else block will NOT be executed if the loop is stopped by
a break statement.
Example:
for x in range(6):
if x == 3: break
print(x)
else:
print("Finally finished!")
break Statement
• With the break statement we can stop the loop before it has looped
through all the items
Ex 1:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
if x == "banana":
break
Ex 2:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
break
print(x)
continue Statement
• With the continue statement we can stop the current iteration of the loop,
and continue with the next
Example:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
continue
print(x)
Nested Loops
Ex 2:
for i in range(1, 4):
for j in range(1, 4):
print(i, j)
For Loop with Zip()
• This code uses the zip() function to iterate over two lists (fruits and
colors) in parallel. The for loop assigns the corresponding elements
of both lists to the variables fruit and color in each iteration.
Example:
fruits = ["apple", "banana", "cherry"]
colors = ["red", "yellow", "green"]
for fruit, color in zip(fruits, colors):
print(fruit, "is", color)
pass Statement
Example:
for x in [0, 1, 2]:
pass
For Loop with Tuple
• This code iterates over a tuple of tuples using a for loop with tuple
unpacking
Example:
t = ((1, 2), (3, 4), (5, 6))
for a, b in t:
print(a, b)
While Loop
def calculate_sum():
total = 0
number = int(input('Enter a number: '))
while a:
print(a.pop())
Single statement while block
print('Inside loop')
counter = counter + 1
else:
print('Inside else')
While with continue
• With the continue statement we can stop the current iteration, and
continue with the next:
# Practice while with continue
def while_continue():
i=0
while i < 6:
i += 1
if i == 3:
continue
print(i)
for Vs while loops