-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBooleanTruthValue.py
executable file
·64 lines (45 loc) · 1006 Bytes
/
BooleanTruthValue.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 25 20:36:03 2021
@author: maherme
"""
#%%
# The constructor of bool is really calling __bool__ method when a number is
# passed:
print(bool(100), (100).__bool__())
print(bool(0), (0).__bool__())
# In the case of a list the method called is __len__ :
a = []
print(bool(a), bool(a.__len__()))
#%%
# Let's see numerical types:
from decimal import Decimal
from fractions import Fraction
print(bool(Fraction(0, 1)), bool(Decimal('0.0')))
print(bool(10.5), bool(1j), bool(Fraction(1, 2)), bool(Decimal('10.5')))
#%%
# Let's see sequence types:
a = [1, 2]
b = 'abc'
c = (1, 2)
print(bool(a), bool(b), bool(c))
#%%
# Let's see map types:
a = {'a' : 1}
b = {1, 2}
print(bool(a), bool(b))
#%%
a = [1, 2, 3]
if a is not None and len(a) > 0:
print(a[0])
else:
print('Nothing to see here...')
#%%
# This code works in the same way than the above
a = None
if a:
print(a[0])
else:
print('Nothing to see here...')
#%%