?print-pdf
' Created for
while
loopwhile
loopwhile loop
in Python repeatedly executes a block of code as long as a specified condition is True. It’s useful when the number of iterations is not known in advance and depends on some condition being met
while condition :
block
break
statement (discussed further)
i = 1
while i<=5 :
print(i)
i += 1
1
2
3
4
5
This is just a simple example. For fixed numbers of times loops, it is better to user for
with range
loop, which will be discussed next
If you run next code, your Python will run an endless loop. Use CTRL+C or CTRL+Z to stop it
# print the numbers from 10 to 1:
i = 10
while i>=1 :
print(i)
i = 1
While
loop is suitable, when we do not know in advance the number of iterations needed
# ask user to enter a name (string), until it contains at least 3 symbols
# the len function on string returns the number of symbols in a string
user_name = input("Enter a name, please: ")
user_name_length = len(user_name)
while user_name_length < 3:
user_name = input("Enter a name (at least 3 symbols): ")
user_name_length = len(user_name)
print("Thank you, {}!".format(user_name))
while
block code is not ideal. We could refactor this by using an endless loop with while True
and a break
statement, which we will explore further.for
loopfor
loop
for variable in sequence:
# code to execute for each item
for letter in "ada":
print(letter.capitalize())
# A
# D
# A
### loop on list items:
for item in [1,2,3]:
print(item, end=",") # 1,2,3,
### loop on tuple items:
for item in (10, "December", 1985):
print(item, end=",") # 10,December,1985,
### loop on string items:
for item in "byron":
print(item, end=",") # b,y,r,o,n,
### loop on range items:
for item in range(1,3):
print(item, end=",") # 1,2,
for n in range(1,6):
print(n, end=",") # 1,2,3,4,5,
total_sum = 0
for n in range(1,11):
total_sum+=n
print(f'total_sum={total_sum}') # total_sum=55
person = {"name": "John", "age": 30, "country": "UK"}
for key, value in person.items():
print(f"{key}: {value}")
# name: John
# age: 30
# country: UK
pairs = [(1, 'one'), (2, 'two'), (3, 'three')]
for number, name in pairs:
print(f"{number} is {name}")
# 1 is one
# 2 is two
# 3 is three
range(len(sequence))
to loop over the indexes of the sequence.
colors = ["red", "green", "blue"]
for index in range(len(colors)):
print(f"{index}: {colors[index]}")
# 0: red
# 1: green
# 2: blue
colors = ["red", "green", "blue"]
for index, color in enumerate(colors):
print(f"{index}: {color}")
# 0: red
# 1: green
# 2: blue
for i in [1,2,3]:
for j in "abv":
print(f'{i}:{j}', end=" ")
print() # prints new line
# 1:a 1:b 1:v
# 2:a 2:b 2:v
# 3:a 3:b 3:v
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
for row in matrix:
for element in row:
print(element, end=' ')
print() # new line after each row
# 1 2 3
# 4 5 6
# 7 8 9
break
statementbreak
statementfor
or while
loop.break
is when some external condition is triggered to exit immediately from a loop.break
statement can be used in both while
and for
loops.
while condition:
block 1
if break_cond:
break # loop is terminated, block 2 is skipped
block 2
for item in sequence :
block 1
if break_cond:
break # loop is terminated, block 2 is skipped
block 2
str = "alibaba"
for s in str:
if s == "i":
break
print(s)
# a
# l
elements = ["apple", "banana", "cherry"]
target = "banana"
for el in elements:
if el == target:
print(f"Found {target}")
break
# Found banana
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
search_value = 4
for row in matrix:
print(f'Processing row: {row}')
for element in row:
print(element, end=',')
if element == search_value:
break # Breaks out of the inner loop
print() # print new line
# Processing row: [1, 2, 3]
# 1,2,3,
# Processing row: [4, 5, 6]
# 4,
# Processing row: [7, 8, 9]
# 7,8,9,
do-while
emulation with break
do-while
loop, as in other languages (reason: "There should be one - and preferably only one - obvious way to do it.", The Zen of Python)do-while
structure is useful when we need the body of the while
loop to be executed at least one.
do {
# will be executed at least one
block
} while (condition);
while True:
block
if (condition): break
do-while
emulation with break
while True:
user_name = input("Enter a name (at least 3 symbols): ")
user_name_length = len(user_name)
if user_name_length > 3: break
print(f"Thank you, {user_name}!")
# Enter a name (at least 3 symbols): iv
# Enter a name (at least 3 symbols): a
# Enter a name (at least 3 symbols): ivan
# Thank you, ivan!
if-elif-break
blocks
# print program menu:
print("Select an action:")
print("1. Action 1")
print("2. Action 3")
print("3. Action 3")
print()
while True:
user_choice = int(input("Enter a number [1-3]: "))
if user_choice == 1:
print("Action 1 fired!")
break
elif user_choice == 2:
print("Action 2 fired!")
break
elif user_choice == 3:
print("Action 3 fired!")
break
else:
print('Wrong input!')
# Select an action:
# 1. Action 1
# 2. Action 3
# 3. Action 3
# Enter a number [1-3]: 7
# Wrong input!
# Enter a number [1-3]: 2
# Action 2 fired!
match
takes an expression and compares its value to successive patterns given as one or more case
blocks.
# print program menu:
print("Select an action:")
print("1. Action 1")
print("2. Action 3")
print("3. Action 3")
print()
while True:
user_choice = int(input("Enter a number [1-3]: "))
match user_choice:
case 1:
print("Action 1 fired!")
case 2:
print("Action 2 fired!")
case 3:
print("Action 3 fired!")
case _
# default case that matches anything not handled by previous cases.
print('Wrong input!')
# Select an action:
# 1. Action 1
# 2. Action 3
# 3. Action 3
# Enter a number [1-3]: 7
# Wrong input!
# Enter a number [1-3]: 2
# Action 2 fired!
continue
statementcontinue
statement
while condition:
block 1
if continue_cond:
continue # go to while condition
block 2
for item in sequence :
block 1
if continue_cond:
continue # continue loop with next item
block 2
for i in range(1,6):
if i == 3:
continue
print(i, end=',') # 1,2,4,5,
word = 'abcdefghi'
vowels = ['a', 'e', 'i', 'o', 'u']
for l in word:
if l in vowels:
continue
print(l, end=",")
# b,c,d,f,g,h,
new_list = [expression for item in iterable if condition]
# Map letters to its uppercase
letters = ['a', 'b', 'c']
upper_letters = [l.upper() for l in letters]
print(upper_letters) #['A', 'B', 'C']
# Filtering Even Numbers:
numbers = [1,2,3,4,5]
evens = [el for el in numbers if el%2==0]
print(evens) # [2, 4]
squares = []
for x in range(5):
squares.append(x**2)
print(squares)
#[0, 1, 4, 9, 16]
squares = [x**2 for x in range(5)]
print(squares)
#[0, 1, 4, 9, 16]
even_numbers = []
for x in range(10):
if x % 2 == 0:
even_numbers.append(x)
print(even_numbers)
# [0, 2, 4, 6, 8]
even_numbers = [x for x in range(10) if x%2==0]
print(even_numbers)
# [0, 2, 4, 6, 8]
# Map letters to its uppercase
letters = ['a', 'b', 'c']
upper_letters = [l.upper() for l in letters]
print(upper_letters)
#['A', 'B', 'C']
# Map Celsius temperatures to Fahrenheit
celsius = [0, 20, 37, 100]
fahrenheit = [((temp * 9/5) + 32) for temp in celsius]
print(fahrenheit)
#[32.0, 68.0, 98.6, 212.0]
# Filtering Even Numbers:
numbers = [1,2,3,4,5]
evens = [num for num in numbers if num % 2 == 0]
print(evens)
#[2, 4]
# Filtering email addresses
emails = ["user1@gmail.com", "user2@yahoo.com", "user3@gmail.com"]
gmail_users = [email for email in emails if "@gmail.com" in email]
print(gmail_users)
#['user1@gmail.com', 'user3@gmail.com']
# Flattening a nested list
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flat_list = [num for row in matrix for num in row]
print(flat_list)
#[1, 2, 3, 4, 5, 6, 7, 8, 9]
new_dict = {key: value for item in iterable if condition}
eng_bul = {
'apple':'ябълка',
'orange':'портокал',
'banana':'банан'
}
# swapping keys and values
bul_eng = {v:k for k,v in eng_bul.items()}
print(bul_eng)
# {'ябълка': 'apple', 'портокал': 'orange', 'банан': 'banana'}
student_scores
, containing only the items which has values > 4.0
student_scores = {
'ivan':4.5,
'maria':5.0,
'asen':3.5
}
best_students = {k:v for k,v in student_scores.items() if v>4}
print(best_students)
# {'ivan': 4.5, 'maria': 5.0}
even_squares = {x: x**2 for x in range(10) if x % 2 == 0}
print(even_squares)
# {0: 0, 2: 4, 4: 16, 6: 36, 8: 64}
keys = ['a', 'b', 'c']
values = [1, 2, 3]
dictionary = {keys[i]: values[i] for i in range(len(keys))}
print(dictionary)
# {'a': 1, 'b': 2, 'c': 3}
#Example: Counting word frequency in a text
text = "apple banana apple apple banana plum"
words = text.split()
word_frequency = {word: words.count(word) for word in set(words)}
print(word_frequency)
# {'apple': 3, 'banana': 2, 'plum': 1}
# Filtering sales data for a quarterly report
sales_data = {
'Q1': {'Jan': 10000, 'Feb': 12000, 'Mar': 15000},
'Q2': {'Apr': 14000, 'May': 16000, 'Jun': 18000},
'Q3': {'Jul': 17000, 'Aug': 19000, 'Sep': 21000},
'Q4': {'Oct': 20000, 'Nov': 22000, 'Dec': 25000}
}
# Get quarters with average sales above 18000
high_performing_quarters = {q: sum(months.values())/len(months)
for q, months in sales_data.items()
if sum(months.values())/len(months) > 18000}
pprint(high_performing_quarters, indent=4)
# {'Q3': 19000.0, 'Q4': 22333.333333333332}
random.randint(1, 10)
method, after including in your code import random
Welcome to Guess the Number!
********************* Levels *********************
* 1. EASY, range: (1, 10), max moves: 7 *
* 2. MEDIUM, range: (1, 10), max moves: 5 *
* 3. HARD, range: (1, 100), max moves: 10 *
**************************************************
>>> Select level: 5
( Enter a number between 1 and 3 )
>>> Select level: 2
*** Let's play MEDIUM. Good Luck! ***
>>> Guess the number (between 1 and 10): 5
Too high! Moves left: 4
>>> Guess the number (between 1 and 10): 1
Too low! Moves left: 3
>>> Guess the number (between 1 and 10): 2
*** Congratulations! You guessed it right in 3 moves!. ***
These slides are based on
customised version of
framework