Created for
while
loopwhile
loop
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))
i = 1
sum = 0
while i <= 100:
sum += i
i += 1
print("sum = ", sum)
sum = 5050
Same task is better to be implemented with for
loop!
sum = 2550
for
loopfor
loop
for item in sequence :
#do something with item
for
statement is different than the "C-based" for
loops in other popular languages (C#, Java, PHP, JavaScript)for
statement iterates over the items of any sequence.foreach
loop concept in above-mentioned languages Iterate over symbols in string:
for s in "ada":
print(s.capitalize())
A
D
A
Iterate over list of numbers:
for num in [1,2,3,4]:
print(num)
1
2
3
4
for i in [1,2,3]:
for j in "abv":
print(j)
print("\n") #prints new line
for
statement will be shown in Sequence data types theme!break
statementbreak
statement
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
do-while
emulationsPython did not have do-while
loop, as in other languages. But it can be easily emulated.
Reason: The Zen of Python
do-while
emulation with break
# ask user to enter a name (string), until it contains at least 3 symbols
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("Thank you, {}!".format(user_name))
do-while
emulation without break
# ask user to enter a name (string), until it contains at least 3 symbols
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))
Can you imagine how the code would look like, if the "do" block was more than 1 line long?
continue
statementcontinue
statement
while condition:
block 1
if continue_cond:
continue # go to while condition
block 2
for i in [1,2,3,4,5]:
if i == 3:
continue
print(i)
1
2
4
5
str = "alabala"
for s in str:
if s in ["a", "e", "i", "o", "u", "y"]:
continue
print(s)
l
b
l
These slides are based on
customised version of
framework