Python-a03-Numeric

This article shows the examples of Float, Integer in python

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
# different types
num1 = 4
print(type(num1))

num2 = 2.1
print(type(num2))

# arithmetic operators
print(5 + 2)
print(5 - 2)
print(5 * 2)
print(5 / 2)
print(5 // 2) # floor division
print(3 ** 2) # exponent
print(3 % 2) # modulus
print(abs(-3)) # absolute
print(round(3.6)) # round number 4
print(round(3.75, 1)) # round number 3.8

# calculation order
print(3 + 2 * 2)
print((3+2)*2)

# increment
num1 = 1
num1 = num1 + 1
print(num1)

num2 = 1
num2 += 1
print(num2)

# comparisons
print(3 == 2) # equal
print(3 != 2) # not equal
print(3 > 2) # greater than
print(3 < 2) # less than
print(3 >= 2) # greater or equal
print(3 <= 2) # less or equal

# cast
num1 = '10'
num2 = '20'
print(num1 + num2) # 1020

num3 = int(num1)
num4 = int(num2)
print(num3 + num4) # 30