-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathbooleans.py
More file actions
105 lines (85 loc) · 2.7 KB
/
booleans.py
File metadata and controls
105 lines (85 loc) · 2.7 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
################################################################################
#
# Program: Booleans
#
# Description: Example of using boolean data type values in Python.
#
# YouTube Lesson: https://www.youtube.com/watch?v=TB9pLwACG5Y
#
# Author: Kevin Browne @ https://portfoliocourses.com
#
################################################################################
# Boolean values are either True or False
x = True
y = False
# We can use type() to verify x and y are boolean values
print(type(x))
print(type(y))
# Many operators such as the comparison operators like less than '<' will
# produce a boolean values as a result
result = 3 < 5
print("result:", result)
# Logical operators like 'or' produce a boolean as a result
result = True or False
print("result:", result)
# The membership operators like 'in' produce a boolean as a result
result = 5 in [1,2,3]
print("result:", result)
# The identity operators like 'is' produce a boolean as a result
result = 1 is 1
print("result:", result)
# Booleans are used in the conditions of control structures if, while
a = 2
b = 3
# The condition expression is evaluated and if it is true the block
# of code executes (otherwise the else block executes)
if (a <= b):
print("a <= b")
else:
print("a > b")
# The condition expression is evaluated and so long as it is True the
# loop body will continue to execute
while a <= b:
print(a)
a = a + 1
# We can have functions return a boolean value
def lucky_number(number):
return number == 7
# This will result in the value True being returned
print(lucky_number(7))
# We can use bool() to convert a value to a boolean
converted = bool("abc")
print("converted:", converted)
# By default, most values are True.
#
# False values include:
#
# - the number 0 (or 0.0, 0j)
# - empty strings, lists, tuples, sets,
# dictionaries, ranges
# - None
# Non-boolean values will evaluate to a boolean when used in contexts
# like an if-statement condition or while loop condition
if (""):
print("String is not empty")
else:
print("String is empty")
# By default, an object is considered true unless its class defines
# either a __bool__() method that returns False or a __len__() method
# that returns zero, when called with the object.
# So we can define a Car class with a __bool__() method that returns True
# if the fuel is > 0
class Car:
def __init__(self):
self.fuel = 0
def __bool__(self):
return self.fuel > 0
# We can instantiate a Car object
car = Car()
# And now if we use car where a boolean is expected in the if statement
# condition, it will evaluate to True or False depending on the __bool__()
# function we have defined.
if (car):
print("Fuel is not empty")
else:
print("Fuel is empty")