3. Python Data Types and Operations

Different Data types and operations

  1. Number

  2. String

  3. List

  4. Tuple

  5. Set

  6. Dictionary


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

  1. abs(x) : return absolute value of x

Example :


Output:


  1. sqrt(x): find the square root of x

Example :


Output:


  1. ceil(x) : find smallest integer, not less than x

Example :


Output:


  1. floor(x): find the largest integer , not greater than x

Example :


Output:


  1. pow(x,y) : finds x raised to y

Example :


Output:


  1. exp(x): return exponential of x

Example :


Output:


  1. fabs(x): return absolute value of x

Example :


Output:


  1. log(x): finds the log to the base 10 for x>0

Example :


Output:


  1. max(x1,x2,x3,x4, ......., xn) : return largest of its arguments.

Example :


Output:


  1. min(x1, x2, ......., xn) : returns the smallest of its arguments.

Example :


Output:


  1. round(x,[n]) : in case of decimal numbers, x will rounded to n digits.

Example :


Output:


  1. modf(x) : return integer and decimal part as a tuple.

Example :


Output:


Example :


import math

print(math.floor(12.3))

Output

12

Trignometric Functions

  1. sin(x)

  2. cos(x)

  3. tan(x)

  4. asin(x)

  5. acos(x)

  6. atan2(y,x)

  7. hypot(x,y)

  8. degrees(x)

  9. 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:

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

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

  1. random() : Return a random number between 0 to 1.

import random

print("Random():", random.random())

Ouput:

Random(): 0.48503448797763093

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

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

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