Python - NUMBERS
Number data types store numeric values.There are three numeric types in Python :
Number objects are created when you assign a value to them. For example −
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(Number objects are created by assigning values to them and further performs required calculations as follows)
>>> var_x=1 (int)
>>> var_x
1
>>> var1=25
>>> var1
25
>>> var=2
>>> var
2
>>> x=1
>>> x
1
>>> y=2.5 (float)
>>> y
2.5
>>> z='freedom' (string)
>>> z
'freedom'
>>> x+y (operation based on assigned values)
3.5
>>> z=1j (complex)
>>> z
1j
You can convert from one type to another with the int(), float(), and complex() methods :
>>> y=2.5
>>> x=int(y) (conversion of float to integer)
>>> x = int(2.5)
>>> print (x)
2
>>> x=1
>>> y=float(x) (conversion of integer to float)
>>> print(y)
1.0
>>> x=1
>>> z=complex(x) (conversion of int to complex)
>>> print(z)
(1+0j)
Python does not have a random() function to make a random number, but Python has a built-in module called random that can be used to make random numbers.
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> print(random.randrange(1,10))
6
>>> import random
>>> print(random.randrange(1,6))
1