-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPython Casting.py
More file actions
45 lines (40 loc) · 869 Bytes
/
Python Casting.py
File metadata and controls
45 lines (40 loc) · 869 Bytes
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
# Python Casting and Type Conversion
# Integers:
x = int(21) # x will be 21
y = int(3.8) # y will be 3
z = int("33") # z will be 33
print(x)
print(y)
print(z)
# Floats:
num1 = float(3) # num1 will be 3.0
num2 = float(5.8) # num2 will be 5.8
num3 = float("44") # num3 will be 44.0
num4 = float("22.2") # num4 will be 22.2
print(num1)
print(num2)
print(num3)
print(num4)
# Strings:
num1 = str("22") # num1 will be "22"
num2 = str("21") # num2 will be "21"
num3 = str("name") # num3 will be "name"
print(num1)
print(num2)
print(num3)
# Type Conversion
num1 = 11 # int
num2 = 3.3 # float
num3 = 12j # complex
#convert from int to float:
x = float(num1)
# convert from float to int:
y = int(num2)
# convert from int to complex:
z = complex(num1)
print(x)
print(y)
print(z)
print(type(x))
print(type(y))
print(type(z))