Breaking News # Python Numbers

Python Numbers

### Python Numbes¶

Number data types store numeric values. They are immutable data types, means that changing the value of a number data type results in a newly allocated object.

### Python supports four different numerical types −¶

1. int
2. long
3. float
4. complex
In :
x = 10    # int
y = 3.28  # float
z = 1j   # complex

In :
print(type(x))
print(type(y))
print(type(z))

<class 'int'>
<class 'float'>
<class 'complex'>


### Integer Numbers¶

Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length.

In :
x = 10
y = 754562722554887711
z = -75522

print(type(x))
print(type(y))
print(type(z))

<class 'int'>
<class 'int'>
<class 'int'>


### Float Numbers¶

Float, or “floating point number” is a number, positive or negative, containing one or more decimals.

In :
x = 7.10
y = 7.0
z = -35.59

print(type(x))
print(type(y))
print(type(z))

<class 'float'>
<class 'float'>
<class 'float'>


Float can also be scientific numbers with an “e” to indicate the power of 10.

In :
x = 35e31
y = 12E5
z = -97.7e100

print(type(x))
print(type(y))
print(type(z))

<class 'float'>
<class 'float'>
<class 'float'>


### Complex Numbers¶

Complex numbers are written with a “j” as the imaginary part:

In :
x = 3+5j
y = 5j
z = -5j

print(type(x))
print(type(y))
print(type(z))

<class 'complex'>
<class 'complex'>
<class 'complex'>


### Type Conversion¶

You can convert from one type to another with the int(), float(), and complex() methods:

In :
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))

10.0
3
(10+0j)
<class 'float'>
<class 'int'>
<class 'complex'>


### Max and Min Number¶

In :
max(120,220,330,)

Out:
330
In :
min(120,220,330,)

Out:
120

### 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.

In :
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'))

0.34024791668795384
9491
517
8553
9180
6711.456883020833
i

In :
L = [1,2,3,4,5,6]
print(random.choice(L))

5

In :
random.shuffle(L)
print(L)

[2, 3, 1, 4, 5, 6]

In :
print(random.sample(L,3))

[5, 6, 2]

In :
print(random.random())
print(random.random())
print(random.random())

0.702233029902215
0.2839544987566254
0.8693256812011598

In :
random.seed(3)

In :
print(random.random())
print(random.random())
print(random.random())

0.23796462709189137
0.5442292252959519
0.36995516654807925

In :
random.seed(3)

In :
print(random.random())
print(random.random())
print(random.random())

0.23796462709189137
0.5442292252959519
0.36995516654807925


you can also import python module as

In :
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'))

0.6039200385961945
2073
620
1215
9575
5222.621806233576



Note: import selected module from python random package

• from random import module_name
In :
from random import random,randint,uniform
print(random())
print(randint(1000,9999))
print(uniform(1000,9999))

0.1917441039952995
8704
5868.2239967757205


Note: import all the random modules as

• from random import *
In :
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'))

0.5496311670270055
7506
654
3467
3370
6713.111063908411
n

In [ ]: 