3. Python Data Types and Operations
Different Data types and operations
Number
String
List
Tuple
Set
Dictionary
- Numbers
This data type stores numeric values.
python support different types numerical data types
a) int
b) float
c) long
d) complex
a) int
x=10
print("The value of X= %d and Data Type =%s" %(x,type(x)))
Output:
The value of X= 10 and Data Type =<class 'int'>
b)float
y=1.6
print("The value of X= %f and Data Type =%s" %(y,type(y)))
Output:
The value of X= 1.600000 and Data Type =<class 'float'>
c) long
l=345678976
print("The value of X= %u and Data Type =%s" %(l,type(l)))
Output:
The value of X= 345678976 and Data Type =<class 'int'>
d) Complex
comp=2+9j
print("The value of X= %s and Data Type =%s" %(comp,type(comp)))
Output:
The value of X= (2+9j) and Data Type =<class 'complex'>
Mathematical Function
abs(x) : return absolute value of x
Example :
Output:
sqrt(x): find the square root of x
Example :
Output:
ceil(x) : find smallest integer, not less than x
Example :
Output:
floor(x): find the largest integer , not greater than x
Example :
Output:
pow(x,y) : finds x raised to y
Example :
Output:
exp(x): return exponential of x
Example :
Output:
fabs(x): return absolute value of x
Example :
Output:
log(x): finds the log to the base 10 for x>0
Example :
Output:
max(x1,x2,x3,x4, ......., xn) : return largest of its arguments.
Example :
Output:
min(x1, x2, ......., xn) : returns the smallest of its arguments.
Example :
Output:
round(x,[n]) : in case of decimal numbers, x will rounded to n digits.
Example :
Output:
modf(x) : return integer and decimal part as a tuple.
Example :
Output:
Example :
import math
print(math.floor(12.3))
Output
12
Trignometric Functions
sin(x)
cos(x)
tan(x)
asin(x)
acos(x)
atan2(y,x)
hypot(x,y)
degrees(x)
radians(x)
Random Number Functions
random number generators used in research, games, cryptography, simulation and different kind of other applications.
You need to import random package:
choice(sequence): return a random values from a sequence like list, tuple, string, etc
import random
s='abcdefg'
print("choice(abcdefg):", random.choice(s))
Output:
choice(abcdefg): g
shuffle(list) : Shuffle the items randomly in the list and return a none value.
import random
l=[10,4,5,6,7,9]
print("Shuffle(l)::", random.shuffle(l))
output:
Shuffle(l):: None
random() : Return a random number between 0 to 1.
import random
print("Random():", random.random())
Ouput:
Random(): 0.48503448797763093
randrange([start], stop[,step]) : returns a randomly selected number from a range where start shows the starting of the range, stop shows the end of the range and step decides the number to be added to decide a random number.
import random
print("Randrange():", random.randrange(2,10,2))
Output:
Randrange(): 8
seed(x) : Give the starting value for generating a random number, return none. This function is called before calling any other random module function.
import random
print("Seed():", random.seed(20))
Output> Seed(): None
uniform(x,y) : Generates a random floating-point number n such that n>x and n<y
import random
print("uniform():", random.uniform(2,3))
Output:
uniform(): 2.9049456946664787