-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoops.py
More file actions
69 lines (56 loc) · 2.33 KB
/
Copy pathLoops.py
File metadata and controls
69 lines (56 loc) · 2.33 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Built in print() function used to output results on terminal
# Each learning point is contained within a function that describes the code
# The # and """" syntax is used for single and multiline comments explaining the code
# Loop Types
# For Loop - runs the same code multiple times, once for each value/item in a sequence
# While Loop - keeps running same code multiple times, until something becomes false.
# Loop Control Keywords
# break - exits loop immediately
# continue - skips the rest of the current iteration and moves to the next
# pass - used as a placeholder when syntax requires a block
# For Loops
def Loop_Over_List_Strings():
"""This function is looping through a list of website strings
and sorting them into categories based on their domain endings."""
List1 = ["TDglitch.com", "TDglitch.net", "Trent.edu," "TDglitch.edu"]
DotCom = []
DotEdu = []
for item in List1:
if item.endswith(".com"):
DotCom.append(item)
elif item.endswith(".edu"):
DotEdu.append(item)
elif item.endswith(".net"):
pass
print(DotCom)
print(DotEdu)
def Loop_Over_List_Numbers():
"""In the for loop, the round function was used to limit the amount of decimals on
the float being output. Round has two arguments round(number to round, amount of decimals)"""
List1 = [799.56, 1099.89, 50.99, 60.25, 90.90, 12.10]
List2 = []
Tax = 1.0825
for number in List1:
List2.append(round(number * Tax, 2))
print(List2)
def Loop_Over_Range():
for n in range(0, 21):
print(n * 2)
def Loop_Over_String():
"""The `, end = ""` argument within the print function keeps all
of the output from the for loop on one line instead of moving
to a new line each time it loops. normally the print function ends
with `\n` which prints then moves to a new line"""
for letter in "hello trent":
print(letter.capitalize(), end="")
def Loop_over_Dict():
User_Age_Dict = {"Trent": 27, "Jerry": 12,
"Karen": 23, "Jim": 18, "Freddy": 16}
Deny_Dict = {}
Allow_Dict = {}
for key, value in User_Age_Dict.items():
if value < 18:
Deny_Dict.update({key: value})
elif value >= 18:
Allow_Dict.update({key: value})
print(f"ACCES ALLOWED{Allow_Dict}, ACCESS DENIED{Deny_Dict}")