-
Notifications
You must be signed in to change notification settings - Fork 0
/
loops.py
101 lines (71 loc) · 1.75 KB
/
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from random import shuffle
theList = [1, 4, 5, 6, 3, 6, 64]
# for
for num in theList:
if(num % 2 == 0):
print(f'{num} is even')
# traverse the strings
theString = "Hey there"
for _ in theString:
print("for string")
# tuple unpacking
tupList = [(1, 2), (3, 4)]
for (a, b) in tupList:
print(a)
print(b)
# dictionaries
ex_mixed = {'k1': 425, 'k2': ["Gersh", "Nesta", 499], 'k3': {'insideKey': 100}}
print("\n\n dic keys")
for item in ex_mixed:
print(item)
print("\n\n")
for k, v in ex_mixed.items():
print(f"{k}-> {v}")
for vals in ex_mixed.values():
print(vals)
# while loops
x = 0
while x < 3:
print(f'current value : {x}')
x = x+1
else:
print(f'{x} is not less than 3')
# ... break, continue, pass
print("\n\n")
for item in ex_mixed:
# comment
pass
print("end the script, if any")
name = 'Sammy'
for letter in name:
if letter == 'a': # go back top to the enclosing loop
continue
# break
print(letter)
# other operators
for num in range(10):
# or for num in range(3,10) starting from 3
# or for num in range(0,10,2): // always skip 2
print(num)
print(list(range(0, 11, 2)))
# ...
word = 'abcde'
for item in enumerate(word):
print(item) # tuple
for index, letter in enumerate(word):
print(f'{index} -> {letter}')
# zip,...
print(f"{'a' in 'a world'}")
print(f"{'theKey' in {'theKey':453}} or \n {453 in {'theKey':453}.values()}")
# ...
print(max(theList))
# using the imports
print("\n\n shuffled list")
shuffle(theList)
print(theList)
print(type(shuffle(theList))) # none bcz returns nothing
# inputs
favorite = input("What is your fav number:")
# print(favorite)
print(float(favorite)) # casting
favorite2 = int(input("What is your fav number:")) # casting 2