Numbers
int
float
complex
The basics about numbers
Example
1 #!/usr/bin/env python3
2
3 def main():
4 q = -1
5 x = 1
6 y = 3.14
7 z = 1j
8 print(type(q))
9 print(type(x))
10 print(type(y))
11 print(type(z))
12
13
14 if __name__ == "__main__":
15 main()
Output
1 <class 'int'>
2 <class 'int'>
3 <class 'float'>
4 <class 'complex'>
Type conversion
Complex numbers cannot be converted into another number type
Example
1 #!/usr/bin/env python3
2
3 def main():
4 x = 1
5 y = 3.14
6 z = 1j
7
8 a = float(x)
9 b = float(y)
10
11 print(a)
12 print(b)
13
14 print(type(a))
15 print(type(b))
16
17
18 if __name__ == "__main__":
19 main()
Generate a random number
The module random
can be used to genera
Example
1 #!/usr/bin/env python3
2
3 import random
4
5 def main():
6 print(random.randrange(1, 10))
7
8
9 if __name__ == "__main__":
10 main()
randint and seed
Note
The behavior of random.randrange()
has changed and will only accept integer paramaters since Python 3.10 otherwise a ValueError
or TypeError
will be generated.
Absolute value
Example
1 #!/usr/bin/env python3
2
3 import random
4
5 def main():
6 print(abs(-1))
7
8
9 if __name__ == "__main__":
10 main()
Output
1 1
Using the math module
A [math](https://docs.python.org/3/library/math.html) modules ships with Python for advanced calculations.
Example
1 #!/usr/bin/env python3
2
3 import math
4
5 def main():
6 # print Pi
7 print(math.pi)
8 # Determine the smallest integer greater than or equal
9 print(math.ceil(8.3))
10 # Determine the largest integer smaller than or equal
11 print(math.floor(8.3))
12 # Print the square root
13 print(math.sqrt(9))
14
15
16 if __name__ == "__main__":
17 main()
Output
1 3.141592653589793
2 9
3 8
4 3.0
Converting between decimal, binary, octal, and hexadecimal
Example
1 #!/usr/bin/env python3
2
3 import math
4
5 def main():
6 value_dec = 128
7 value_bin = 0b10000000
8 value_oct = 0o200
9 value_hex = 0x80
10
11 print("The value_decimal value of", value_dec, "is:")
12 print(bin(value_dec), "in binary.")
13 print(oct(value_dec), "in octal.")
14 print(hex(value_dec), "in hexadecimal.")
15
16 print("The binary value of", (value_bin), "is:")
17 print(int(value_bin), "in decimal.")
18 print(oct(value_bin), "in octal.")
19 print(hex(value_bin), "in hexadecimal.")
20
21 print("The octal value of", oct(value_oct), "is:")
22 print(int(value_oct), "in decimal.")
23 print(bin(value_oct), "in binary.")
24 print(hex(value_oct), "in hexadecimal.")
25
26 print("The hexvalue_decimal value of", hex(value_hex), "is:")
27 print(int(value_hex), "in decimal.")
28 print(bin(value_hex), "in binary.")
29 print(oct(value_hex), "in octal.")
30
31
32 if __name__ == "__main__":
33 main()
Output
1 The value_decimal value of 128 is:
2 0b10000000 in binary.
3 0o200 in octal.
4 0x80 in hexadecimal.
5 The binary value of 128 is:
6 128 in decimal.
7 0o200 in octal.
8 0x80 in hexadecimal.
9 The octal value of 0o200 is:
10 128 in decimal.
11 0b10000000 in binary.
12 0x80 in hexadecimal.
13 The hexvalue_decimal value of 0x80 is:
14 128 in decimal.
15 0b10000000 in binary.
16 0o200 in octal.