Repository navigation
Expand file tree
/
Copy path6-boleans.py
More file actions
78 lines (61 loc) · 1.64 KB
/
Copy path6-boleans.py
File metadata and controls
78 lines (61 loc) · 1.64 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
"""
PYTHON BOLEANS - True or False
"""
# Boolean Values
print(10 > 9) # True
print(10 == 9) # False
print(10 < 9) # False
print("---")
## Print a message based on whether the condition is True or False:
a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
print("---")
# Evaluate Values and Variables - bool() function returns True or False
## Evaluate a string and a number:
print(bool("Hello"))
print(bool(15))
print("---")
## Evaluate two variables:
x = "Hello"
y = 15
print(bool(x))
print(bool(y))
print("---")
"""
Most Values are True
Almost any value is evaluated to True if it has some sort of content.
Any string is True, except empty strings.
Any number is True, except 0.
Any list, tuple, set, and dictionary are True, except empty ones.
Some Values are False
In fact, there are not many values that evaluate to False, except empty values, such as (), [], {}, "", the number 0, and the value None. And of course the value False evaluates to False.
"""
print(bool(False))
print(bool(None))
print(bool(0))
print(bool(""))
print(bool(()))
print(bool([]))
print(bool({}))
print("---")
# Functions can Return a Boolean
def myFunction() :
return True
print(myFunction()) # True
print("---")
## Code based on the Boolean answer of a function
def myFunction():
return True
if myFunction():
print("YES!")
else:
print("NO!")
print("---")
# Python also has many built-in functions that return a boolean value, like the isinstance() function, which can be used to determine if an object is of a certain data type:
## Check if an object is an integer or not:
x = 200
print(isinstance(x, int))