-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcasting.py
44 lines (34 loc) · 877 Bytes
/
casting.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
"""
Demonstrates casting of data types
"""
# casting with int
a = int(10)
b = int('10')
# c = int('10.10') # error, invalid literal for int() with base 10: '1.0', use float
d = int(10.10)
print('\ncasting with int()')
print(a, b, d)
# casting with float
a = float(10)
b = float('10')
c = float('10.10')
d = float(10.10)
print('\ncasting with float()')
print(a, b, c, d)
# casting with str
a = str(10)
b = str(10.10)
c = str("10.10")
d = str(True)
e = str([1, 2, 3])
f = str({'1': 'green', '2': 'blue', '3': 'green'})
print('\ncasting with str()')
print(a, b, c, d, e, f)
# Returns the boolean value of the specified object
print('\ncasting with bool()')
print('bool', bool(False)) # False
print('bool', bool(0)) # False
print('bool', bool(None)) # False
print('bool', bool(True)) # True
print('bool', bool(1)) # True
print('bool', bool('0')) # True