-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11_conditional_statements.py
More file actions
307 lines (283 loc) · 11.1 KB
/
Copy path11_conditional_statements.py
File metadata and controls
307 lines (283 loc) · 11.1 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#======================================================================================
# CONDITIONAL STATEMENTS
#--------------------------------------------------------------------------------------
# A conditional statement lets python make decisions.
# If condition is true, do this--otherwise do something else.
# In simple words it a checkpoint that checks a condition is True or False.
#======================================================================================
#--------------------------------------------------------------------------------------
# 1) simple if statement
#--------------------------------------------------------------------------------------
# Defines the first condition
# if the condition is true, do this
# otherwise, do nothing
#--------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------
# if rules
#--------------------------------------------------------------------------------------
# --only one if
# --starts with if
# --it is required not optional
# --can standalone
#-------------------------------------------------------------------------------------
# without indentation
#score = 100
#if score >= 90:
#print("A") throws an error
#-------------------------------------------------------------------------------------
# Indentation
#-------------------------------------------------------------------------------------
# adding spaces at the beginning of a line to show the line
# belongs to a code block
#-------------------------------------------------------------------------------------
score = 100
if score >= 90:
print("A")
#--------------------------------------------------------------------------------------
# 3) if-else statement
#--------------------------------------------------------------------------------------
# runs only if all previous conditions are false
# if nothing was true, do this instead
#--------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------
# else rules
#--------------------------------------------------------------------------------------
# --comes at the end
# --no conditions
# --its optional
# --cannot standalone
# --only one else
#-------------------------------------------------------------------------------------
score = 50
if score >= 90:
print("A")
else:
print("F")
#--------------------------------------------------------------------------------------
# 3) if-elif-else statement
#--------------------------------------------------------------------------------------
# asks a follow-up question
# only runs if previous conditions were false
# if the first wasn't true, try this one
#--------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------
# elif rules
#--------------------------------------------------------------------------------------
# --comes after if
# --multiple elif
# --needs condition
# --optional
# --cannot standalone
#-------------------------------------------------------------------------------------
score = 80
if score >= 90:
print("A")
elif score >= 80:
print("B")
else:
print("F")
#--------------------------------------------------------------------------------------
# Full grading logic with multiple elif
#--------------------------------------------------------------------------------------
score = 60
if score >= 90:
print("A")
elif score >= 80:
print("B")
elif score >= 70:
print("C")
elif score >= 60:
print("D")
else:
print("F")
#--------------------------------------------------------------------------------------
# Nested if
#--------------------------------------------------------------------------------------
# if statement inside another if statement
#--------------------------------------------------------------------------------------
score = 90
submitted_project = True
if score >= 90:
if submitted_project:
print("A+")
else:
print("A")
elif score >= 80:
print("B")
elif score >= 70:
print("C")
elif score >= 60:
print("D")
else:
print("F")
#--------------------------------------------------------------------------------------
# Combined statements with logical operators and/or
#--------------------------------------------------------------------------------------
score = 10
submitted_project = True
if score >= 90 and submitted_project:
print("A+")
elif score >= 90:
print("A")
elif score >= 80:
print("B")
elif score >= 70:
print("C")
elif score >= 60 or submitted_project:
print("D")
else:
print("F")
#--------------------------------------------------------------------------------------
# Independent conditions
#--------------------------------------------------------------------------------------
score = 80
submitted_project = True
if score >= 90:
print("High Score")
else:
print("Low Score")
if submitted_project:
print("Project is Submitted")
else:
print("Project is not Submitted")
#--------------------------------------------------------------------------------------
# #1 Challenge
# validate the quality and correctness of email values
#--------------------------------------------------------------------------------------
# --Email must not be empty
# --Email must contain "." and "@"
# --Email must contain exactly one "@" symbol
# --Email must end with ".com", ".org", ".net"
# --Email must not be longer than 254 characters
# --Email must start and end with a letter or digit
#--------------------------------------------------------------------------------------
email = "munna@gmail.com"
email = email.strip()
# --Email must not be empty
if email == "":
print("Email cannot be empty.")
# --Email must contain "." and "@"
elif not ("." in email and "@" in email):
print("Email must contain \".\" and \"@\" symbol.")
# --Email must contain exactly one "@" symbol
elif email.count("@") != 1:
print("Email must contain exactly one \"@\" symbol")
# --Email must end with ".com", ".org", ".net"
elif not email.endswith((".com", ".org", ".net")):
print("Email must end with .com, .org, or .net")
# --Email must not be longer than 254 characters
elif len(email) > 254:
print("Email must not be longer than 254 characters")
# --Email must start and end with a letter or digit
elif not (email[0].isalnum() and email[-1].isalnum()): # isalnum()--checks if the string contains only letters and digits
print("Email must start and end with a letter or digit")
else:
print("Valid Email")
#--------------------------------------------------------------------------------------
# #2 Challenge
# validate the quality and correctness of passwords
#--------------------------------------------------------------------------------------
# --Password must not be empty
# --Password must be at least 8 characters
# --Password include at least 1 uppercase
# --Password include at least 1 lowercase
# --Password must not be same as email
# --Password must not contain any spaces
# --Password must start and end with a letter or digit
#--------------------------------------------------------------------------------------
password = "David@Wilson"
# --Password must not be empty
if password == "":
print("password cannot be empty")
# --Password must be at least 8 characters
elif len(password) <= 8:
print("password must be at least 8 characters")
# --Password include at least 1 uppercase
elif not any((char.isupper() for char in password)):
print("password must include at least 1 uppercase letter")
# --Password include at least 1 lowercase
elif not any(char.islower() for char in password):
print("password must include at least 1 lowercase letter")
# --Password must not be same as email
elif password == email:
print("password must not be same as email")
# --Password must not contain any spaces
elif " " in password:
print("password must not contain spaces")
# --Password must start and end with a letter or digit
elif not (password[0].isalnum() and password[-1].isalnum()):
print("Password must start and end with a letter or digit")
else:
print("Valid password")
#--------------------------------------------------------------------------------------
# Normal if statement | inline if statement(ternary operator)
#--------------------------------------------------------------------------------------
# if condition1: | do A if condition1 else do B
# do A |
# else: |
# do B |
##--------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------
# Simple Logic (Use inline if for simple logic only)
#--------------------------------------------------------------------------------------
score = 90
print("A" if score >= 90 else "F")
# we can also assign the output of if statement to a variable
grade = "A" if score >= 90 else "F"
print(grade)
#--------------------------------------------------------------------------------------
# Complex Logic (for complex conditions, normal if blocks are clearer.)
#--------------------------------------------------------------------------------------
# inline-if statement(nested ternary) --> it hurts readability
grade ="A" if score >= 90 else "B" if score >= 80 else "C" if score >= 70 else "D" if score >= 60 else "F"
print(grade)
# Normal if statement --> easy to read
if score >= 90:
print("A")
elif score >= 80:
print("B")
elif score >= 70:
print("C")
elif score >= 60:
print("D")
else:
print("F")
#--------------------------------------------------------------------------------------
# Task
# Convert the full country names into 2-letter abbreviations
#--------------------------------------------------------------------------------------
country = "United States"
if country == "United States":
print("US")
elif country == "India":
print("IN")
elif country == "Egypt":
print("EG")
elif country == "Germany":
print("DE")
else:
print("Unknown Country")
#--------------------------------------------------------------------------------------
# MATCH-CASE
#--------------------------------------------------------------------------------------
# match-case is used to compare a value against multiple patterns
# and execute the matching block.
#--------------------------------------------------------------------------------------
# match-case rules
#--------------------------------------------------------------------------------------
# works only in python 3.10+
# _ (under score) is mandatory for default case(recommended)
# do not works on complex conditions(x > 10 and x < 20)
#--------------------------------------------------------------------------------------
country = "Am"
match country:
case "United States" | "USA" |"America":
print("US")
case "India" | "Bharat":
print("IN")
case "Egypt":
print("EG")
case "Germany":
print("DE")
case _:
print("Unknown Country")