x = 10 # int
y = 3.28 # float
z = 1j # complex
print(type(x))
print(type(y))
print(type(z))
Integer Numbers¶
Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length.
x = 10
y = 754562722554887711
z = -75522
print(type(x))
print(type(y))
print(type(z))
Float Numbers¶
Float, or “floating point number” is a number, positive or negative, containing one or more decimals.
x = 7.10
y = 7.0
z = -35.59
print(type(x))
print(type(y))
print(type(z))
Float can also be scientific numbers with an “e” to indicate the power of 10.
x = 35e31
y = 12E5
z = -97.7e100
print(type(x))
print(type(y))
print(type(z))
Complex Numbers¶
Complex numbers are written with a “j” as the imaginary part:
x = 3+5j
y = 5j
z = -5j
print(type(x))
print(type(y))
print(type(z))
Type Conversion¶
You can convert from one type to another with the int(), float(), and complex() methods:
x = 10 # int
y = 3.14 # float
z = 5j # complex
#convert from int to float:
a = float(x)
#convert from float to int:
b = int(y)
#convert from int to complex:
c = complex(x)
print(a)
print(b)
print(c)
print(type(a))
print(type(b))
print(type(c))
Max and Min Number¶
max(120,220,330,)
min(120,220,330,)
Random Numbers in Python¶
Python defines a set of functions that are used to generate or manipulate random numbers. This particular type of functions are used in a lot of games, lotteries, OTP – (One Time Password), 2 Factor Authentication, Securtiy Lock or any application requiring random number generation.
1. random() :- This number is used to generate a float random number less than 1 and greater or equal to 0.
2. randint(start, end) :- This function is also used to generate random number but within a range specified in its arguments.
3. randrange(start, end, step) :- This function is also used to generate random number but within a range specified in its arguments.
4. uniform(a, b) :- This function is used to generate a floating point random number between the numbers mentioned in its arguments.
5. choice() :- This function is used to generate 1 random number from a container.
6. shuffle() :- This function is used to shuffle the entire list to randomly arrange them.
7. sample() :- This function Returns randomly selected items from a sequence
8. seed(): :- This function takes a number as a parameter called the seed and produces same random numbers each time you call this function with that number.
import random
print(random.random())
print(random.randint(1000,9999))
print(random.randrange(1000))
print(random.randrange(1000,9999))
print(random.randrange(1000,9999,5))
print(random.uniform(1000,9999))
print(random.choice('Machine Learning'))
L = [1,2,3,4,5,6]
print(random.choice(L))
random.shuffle(L)
print(L)
print(random.sample(L,3))
print(random.random())
print(random.random())
print(random.random())
random.seed(3)
print(random.random())
print(random.random())
print(random.random())
random.seed(3)
print(random.random())
print(random.random())
print(random.random())
you can also import python module as
import random as r
print(r.random())
print(r.randint(1000,9999))
print(r.randrange(1000))
print(r.randrange(1000,9999))
print(r.randrange(1000,9999,5))
print(r.uniform(1000,9999))
print(r.choice('Machine Learning'))
Note: import selected module from python random package
- from random import module_name
from random import random,randint,uniform
print(random())
print(randint(1000,9999))
print(uniform(1000,9999))
Note: import all the random modules as
- from random import *
from random import *
print(random())
print(randint(1000,9999))
print(randrange(1000))
print(randrange(1000,9999))
print(randrange(1000,9999,5))
print(uniform(1000,9999))
print(choice('Machine Learning'))