-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path07_loopsExample.py
More file actions
executable file
·47 lines (38 loc) · 1.15 KB
/
07_loopsExample.py
File metadata and controls
executable file
·47 lines (38 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python3
"""
Examples for loops
https://docs.python.org/3/reference/compound_stmts.html#for
https://docs.python.org/3/reference/compound_stmts.html#while
Jens Dede, 2019, jd@comnets.uni-bremen.de
"""
# Print numbers from 1 to 10
print("For loop")
for number in range(1,11):
print(number)
# Print numbers from 1 to 10 but break loop under certain conditions
print("Break loop")
for number in range(1,11):
if number == 5:
break # Stop the loop
print(number)
# Print numbers from 1 to 10 but continue loop under certain conditions
print("Continue loop")
for number in range(1,11):
if number % 2: # Modulo operator: %
continue # Next iteration
print(number)
# Iterate over items
print("Iterate loop")
items = ["Book", "Banana", "Computer"]
for item in items:
print("I will take my", item, "with me...")
# While loop
print("While loop")
number = 1
while (number < 10):
print(number)
number += 1 # Short form for number = number + 1, number++ does not exist
# What to try out
#################
# - You can user several loops. Write a program which outputs a multiplication
# table from 1 to 100 (1x1=1, 1x2=2, etc.)