-
Notifications
You must be signed in to change notification settings - Fork 0
/
loops.py
54 lines (34 loc) · 897 Bytes
/
loops.py
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
48
49
50
51
52
53
54
# loops
# Repetitive:
# for loop, while
# I want to sum the odd numbers and even numbers separately from 0 to 100
all_evens = 0
all_odds = 0
for x in range(101):
if x % 2 == 0:
all_evens = all_evens + x
print('Even', x)
else:
all_odds = all_odds + x
print('Odd', x)
print('All evens:', all_evens, 'All odds:', all_odds)
nums = [1, 2, 3, 4, 5] # [2, 4, 6, 8, 10]
new_nums = []
for num in nums:
new_nums.append(num ** 2)
print(new_nums)
countries = ['Finland', 'Sweden', 'Estonia', 'Norway']
new_countries = []
for country in countries:
new_countries.append(country.upper())
print(new_countries)
# while loop
x = 0
while x < 6:
print(x)
x = x + 1
countries = ['Finland', 'Sweden', 'Estonia', 'Norway']
new_countries = []
for i in range(len(countries), 0, -1):
new_countries.append(countries[i-1])
print(new_countries)