-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04_boolean_tutorial.py
More file actions
102 lines (82 loc) · 3.08 KB
/
04_boolean_tutorial.py
File metadata and controls
102 lines (82 loc) · 3.08 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
# 1. Boolean values
# There are only two values: True and False (capitalized!)
is_active = True
is_deleted = False
print(f"is_active: {is_active}, type: {type(is_active)}")
print(f"is_deleted: {is_deleted}")
# 2. Comparison operators (return boolean)
x = 10
y = 5
print(f"{x} == {y}: {x == y}") # Equal: False
print(f"{x} != {y}: {x != y}") # Not equal: True
print(f"{x} > {y}: {x > y}") # Greater than: True
print(f"{x} < {y}: {x < y}") # Less than: False
print(f"{x} >= {y}: {x >= y}") # Greater or equal: True
print(f"{x} <= {y}: {x <= y}") # Less or equal: False
# 3. Logical operators
a = True
b = False
print(f"{a} and {b}: {a and b}") # AND (both must be True): False
print(f"{a} or {b}: {a or b}") # OR (at least one True): True
print(f"not {a}: {not a}") # NOT (inversion): False
# 4. Combining conditions
age = 25
has_license = True
# Can drive a car if age >= 18 AND has license
can_drive = age >= 18 and has_license
print(f"Can drive: {can_drive}")
# 5. Logical operator precedence
# not > and > or
result = True or False and False
print(f"True or False and False = {result}") # True (and first, then or)
# With parentheses for clarity
result = True or (False and False)
print(f"True or (False and False) = {result}") # True
# 6. Values interpreted as False (Falsy values)
# False, None, 0, 0.0, "", [], {}, ()
print(f"bool(False): {bool(False)}") # False
print(f"bool(None): {bool(None)}") # False
print(f"bool(0): {bool(0)}") # False
print(f"bool(0.0): {bool(0.0)}") # False
print(f"bool(''): {bool('')}") # False (empty string)
print(f"bool([]): {bool([])}") # False (empty list)
print(f"bool({{}}): {bool({})}") # False (empty dictionary)
# 7. Values interpreted as True (Truthy values)
# Everything else
print(f"bool(True): {bool(True)}") # True
print(f"bool(1): {bool(1)}") # True
print(f"bool('text'): {bool('text')}") # True
print(f"bool([1,2,3]): {bool([1, 2, 3])}") # True
# 8. Using in conditions
name = "Pavel"
# Short check for emptiness
if name:
print(f"Name is specified: {name}")
else:
print("Name is empty")
# 9. Comparison chaining (unique Python feature!)
age = 25
# Check if age is in range from 18 to 65
if 18 <= age <= 65:
print(f"Age {age} is in valid range")
# Equivalent (but less readable):
if age >= 18 and age <= 65:
print("Age is in valid range")
# 10. The 'is' operator (identity check, not equality)
# 'is' checks if two variables reference THE SAME object in memory
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(f"a == b: {a == b}") # True (values are equal)
print(f"a is b: {a is b}") # False (different objects)
print(f"a is c: {a is c}") # True (same object)
# Important: for None always use 'is', not ==
value = None
if value is None:
print("Value is None")
# 11. The 'in' operator (membership check)
fruits = ["apple", "banana", "orange"]
print(f"'banana' in fruits: {'banana' in fruits}") # True
print(f"'pear' not in fruits: {'pear' not in fruits}") # True
text = "Python"
print(f"'Py' in text: {'Py' in text}") # True