-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10_logical_operators.py
More file actions
172 lines (150 loc) · 6.67 KB
/
Copy path10_logical_operators.py
File metadata and controls
172 lines (150 loc) · 6.67 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
#======================================================================================
# LOGICAL OPERATORS
#--------------------------------------------------------------------------------------
# logical operators are used to combine multiple boolean expressions
# 1) and
# 2) or
# 3) not
# 4) in
# 5) not in
#--------------------------------------------------------------------------------------
#======================================================================================
#--------------------------------------------------------------------------------------
# 1) and--returns True if all conditions are True
#--------------------------------------------------------------------------------------
print(3 > 1 and 5 < 1)
print(3 > 1 and 5 > 1)
email = True
password = True
print(email and password)
#--------------------------------------------------------------------------------------
# 2) or --returns True if any one condition is True
#--------------------------------------------------------------------------------------
print(3 > 1 or 5 < 1)
print(3 < 1 or 5 < 1)
cpu_usage = 70
storage_usage = 50
print(cpu_usage >= 70 or storage_usage > 90)
#--------------------------------------------------------------------------------------
# 3) not--reverses the result(True<-->False)
#--------------------------------------------------------------------------------------
print(not 3 > 1)
print(not True)
print(not False)
name = ""
print(not name)
print(not 0)
print(not None)
#--------------------------------------------------------------------------------------
# execution order parentheses() first
#--------------------------------------------------------------------------------------
x = 5
y = 8
z = 6
print(x == 5 or y > 5 and z <4) # "and" has higher priority than "or"
print((x == 5 or y > 5) and z < 4) # use parentheses() to control the order
#--------------------------------------------------------------------------------------
#Exercise
# Allow access only if the user is logged in
# or they are a guest
# but they must not be banned
#--------------------------------------------------------------------------------------
is_logged_in = True
is_guest = False
is_banned = True
print((is_logged_in or is_guest) and not is_banned)
#======================================================================================
# python challenge
#--------------------------------------------------------------------------------------
#1) checks if a user's name is not empty and the age is greater than or equal to 18
#--------------------------------------------------------------------------------------
username = "David"
age = 19
if username and age >= 18:
print("Valid User")
else:
print("Invalid User")
#--------------------------------------------------------------------------------------
#2) check if the password is at least 8 characters long and does not contain spaces
#--------------------------------------------------------------------------------------
password = "Charlie2580"
if len(password) >= 8 and not " " in password:
print("Valid Password")
else:
print("Invalid Password")
#--------------------------------------------------------------------------------------
#3) check if a user's email is not empty, contains '@' and ends with '.com'
#--------------------------------------------------------------------------------------
email = "shaikmunna@gmail.com"
if email and "@" in email and email.endswith(".com"):
print("Valid email")
else:
print("Invalid email")
#--------------------------------------------------------------------------------------
#4) check if a username is a string, is not None, and is longer than 5 characters
#--------------------------------------------------------------------------------------
username = ""
if isinstance(username, str) and username is not None and len(username) > 5:
print("Valid username")
else:
print("Invalid username")
#--------------------------------------------------------------------------------------
#5) check if the user is either an admin or a moderator, and
# either they're not banned or they've verified their email
#--------------------------------------------------------------------------------------
role = "admin".lower()
is_banned = False
verified_email = True
if (role == "admin" or role == "moderator") and (not is_banned or verified_email):
print("Access granted")
else:
print("Access denied")
#======================================================================================
# MEMBERSHIP OPERATORS
#--------------------------------------------------------------------------------------
# Membership operators are used to check whether a value exists inside a collection
# (like string, list, tuple, set, dictionary).
#======================================================================================
#----------------------------------------
# operator | meaning |
#----------------------------------------
# in | value exists |
# not in | value does not exist |
#----------------------------------------
print("o" in "python") #True
print("f" in "python") #False
print("f" not in "python") #True
# Task
# Security check: ensure the domain is not banned
domain = "spam.com"
banned_domains = ["spam.com", "fake.org", "bot.net"]
print(domain not in banned_domains) # False
#======================================================================================
# IDENTITY OPERATORS
#--------------------------------------------------------------------------------------
# Identity operators are used to check whether two variables point to the same object
# in memory, NOT whether their values are equal.
#======================================================================================
#----------------------------------------
# operator | meaning |
#----------------------------------------
# is | Same object |
# is not | different object |
#----------------------------------------
a = 10
b = 10
print(a == b) # True(same value)
print(a is b) # True(same memory for small integers)
x = [1,2,3]
y = [1,2,3]
print(x == y) # True(same values)
print(x is y) # False(different memory)
k = [1,2,3]
l = k
print(k == l) # True
print(k is l) # True
#--------------------------------------------------------------------------------------
# Task: Make sure the email exists, and it's not empty.
#--------------------------------------------------------------------------------------
email = "mun@gmail.com"
print(email is not None and email != "") # use "is/is not" instead of "==/!=" when checking for None