Unit 3
Unit 3
Performing Loops
Unit 3
CC S 1 5 0 0
Objectives
❑ Identify when to use an indentation
❑ Differentiate if, if-else, if-elif-else, and nested
if
❑ Describe the while loop and for loop
❑ Differentiate break, continue, and else statements
❑ Create a program implementing selection and iteration
2
Making Decisions and
Performing Loops
3
❑ Running a block of code based on a particular decision
− decisions based on a condition
❑ Indentation
− used to declare a block
− if the statements are on the same level of indentation, then
they are on the same block
if, elif, and else
Statements
5
if Statement
− used to test a specific condition, if the condition is true, a
block of code (if-block) will be executed
− condition can be any valid logical expression
if condition: True
condition if block
statement(s)
False
x = 100
y = int(input("Enter a number: "))
if y > x:
print(y, " is larger than ", x)
if-else Statement
− if the condition in the if statement is false, it will execute the
else statement
if condition: true
statement(s) condition if block
else:
statement(s)
false
else block
x = int(input("Enter a number: "))
y = int(input("Enter another number: "))
if y > x:
print(y, " is larger than ", x)
else:
print(x, " is larger than ", y)
late = True
if late:
print('I need to call my manager!')
else:
print('no need to call my manager...')
elif Statement
− checks multiple conditions and executes the block of code of
the condition that evaluated to true
− optional like else
− can contain multiple elif after an if
if condition1:
statement(s)
elif condition2:
statement(s)
elif condition3:
statement(s)
else:
statement(s)
false
condition
true false
condition
if block
true false
condition
elif block
true
elif block else block
x = int(input("Enter a number: "))
y = int(input("Enter another number: "))
z = int(input("Enter another number: "))
if condition:
statement(s)
if condition:
statement(s)
x = int(input("Enter a number: "))
if x > 0:
print(x, " is a positive number.")
if x > 100:
print("It is greater than 100.")
else:
print("The number is ", x)
for Loop
17
The for Loop
− used for iterating on a sequence (either a list, a tuple, a
dictionary, a set, or a string)
− a set of statements may be executed once for each item,
tuple, set, etc.
− does not require an indexing variable to set beforehand
fruits = ["orange", "grapes", "apple"]
for x in fruits: List
print(x)
for x in "Python":
String print(x)
The break Statement
− can stop the loop before it has looped through all the items
for x in range(10):
print(x)
for x in range(5, 10):
print(x)
for x in range(10):
print(x)
else:
print("Completed!")
for x in desc:
for y in fruits:
print(x, y)
The pass Statement
− for loop cannot be empty
28
The while Loop
− as long as the condition is true, it can execute a block of
statements
− needs to define an indexing variable
• increment/decrement this variable or the loop will not end
while condition:
action while condition is true
i = 1
while i <= 10: True
condition action if true
print(i)
i += 1
False
The break Statement
− the loop can be stopped even if the while condition is still
true
i = 10
while i <= 10:
print(i)
if i == 1:
break
i -= 1
The continue Statement
− stop the current iteration and continue with the next
i = 0
while i < 10:
i += 1
if i == 7:
continue
print(i)
The else Statement
− can run a block of code when the condition is no longer true
i = 1
while i < 10:
print(i)
i += 1
else:
print(i, " is greater than 9")
The do-while Loop
− can run at least once
− Python does not have a built-in do-while loop
• but possible to emulate
secret_word = "python"
counter = 0
while True:
word = input("Enter the secret word: ").lower()
counter = counter + 1
if word == secret_word:
break
if word != secret_word and counter > 7:
print("The secret word is ", secret_word)
break
Usual Logical Conditions
❑ x == y
❑ x != y
❑x<y
❑x>y
❑ x <= y
❑ x >= y
Using the Conditional
Operator
Ternary Operator or Conditional Operator
− tests if a condition is true or false and returns the
corresponding value, all in one line of code
if condition:
a
else:
b
cust_age = int(input("Enter the customer's age: "))
amount = 1000
if y >= x:
print(“y is greater than or equal to x")
else:
print(“x is greater than y")
x = 5
y = 3
"y is greater than or equal to x" if y>=x else "x is greater than y"
Using Tuples
(b, a)[condition]
x = 5
y = 3
if y >= x:
print(“y is greater than or equal to x")
else:
print(“x is greater than y")
x = 5
y = 3
x = 5
y = 3
if y >= x:
print(“y is greater than or equal to x")
else:
print(“x is greater than y")
x = 5
y = 3
{True: "y is greater than or equal to x", False: "x is greater than y"}[y >= x]
Using Lambda Functions
x = 5
y = 3
if y >= x:
print(“y is greater than or equal to x")
else:
print(“x is greater than y")
x = 5
y = 3
(lambda: "x is greater than y", lambda: "y is greater than or equal to x")[y >= x]()
Using the Logical and and
or Operators
❑ and and or
− used to combine conditional statements
❑ not
− used to reverse the result of the conditional statement
Truth Table for and and or
x y x and y x or y
T T T T
T F F T
F T F T
F F F F
x = 78
y = 30
z = 54
x = 54
y = 150
if not x > y:
print(x, " is NOT greater than ", y)
x = 78
y = 30
z = 54
if x>y or z>y:
print(“At least one of the conditions is True")