-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13_loop_control_statements.py
More file actions
77 lines (69 loc) · 2.48 KB
/
Copy path13_loop_control_statements.py
File metadata and controls
77 lines (69 loc) · 2.48 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
70
71
72
73
74
75
76
77
#======================================================================================
# LOOP CONTROL FLOW STATEMENTS
#--------------------------------------------------------------------------------------
# Python provides 3 main loop control flow statements:
# 1) break --> stop the loop
# 2) continue --> skip the current iteration
# 3) pass --> do nothing(placeholder)
#=====================================================================================
#--------------------------------------------------------------------------------------
# 1) break-->control flow by stopping loop
#--------------------------------------------------------------------------------------
for i in range(1,11):
if i == 6:
break
print(i)
names = ["charlie", "john", "maria", " ", "emma", "Raja"]
for name in names:
if name == " ":
print("Empty name detected")
break
print(f"Name = {name}")
#--------------------------------------------------------------------------------------
# 2) continue-->control flow by skipping iteration
#--------------------------------------------------------------------------------------
for i in range(1,11):
if i == 6:
continue
print(i)
names = ["charlie", "john", "maria", " ", "emma", "Raja"]
for name in names:
if name == " ":
print("Empty name detected")
continue
print(f"Name = {name}")
#--------------------------------------------------------------------------------------
# 3) pass-->control flow placeholder(do nothing)
#--------------------------------------------------------------------------------------
for i in range(1,11):
if i == 6:
pass
print(i)
names = ["charlie", "john", "maria", " ", "emma", "Raja"]
for name in names:
if name == " ":
#print("Empty name detected")
pass
print(f"Name = {name}")
# Real world use cases
# loop through a list of days and print only the working days,
# skip the weekends
days = ["Sun","Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
for day in days:
if day in ["Sun", "Sat"]:
continue
print(f"Workday:{day}")
#scan emails to block unsafe data from entering your system
emails = [
"shaikmunna@gmail.com",
"abdulla@gmail.com",
"DROP TABLE USERS;",
"rasheed@outlook.in",
"john@gmail.com"
]
for email in emails:
if ";" in email:
print("SQL injection: Hacker attack")
print("Email Processing is Stopped")
break
print(f"processing email: {email}")