DATATYPES IN PYTHON:
The basic data types available in Python language which is similar to other programming languages such as C, C++, Java etc., There are 14 data types in the python programming language and they are as follows:
-
INTEGER
-
FLOAT
-
COMPLEX
-
BOOLEAN
-
STRING
-
BYTES
-
BYTE-ARRAY
-
LIST
-
TUPLE
-
SET
-
FROZENSET
-
RANGE
-
DICTIONERY
-
NONE
Now these data types are classified as follows:
-
INTEGER:
“INTEGER” is one of the data type that can represents the whole numbers in Python. To represent integral values we can use integer data-type.
EX: a=10, b=20 , c=300
We can represent integer in the following forms:
- Binary form
- Octal form
- Hexa Decimal form
Now we are explaining the different forms of integers:
BINARY INTEGER:
To represent any number in binary format we can us 0’s and 1 digits.
To represent binary integer we can use 0b (or) 0B.
Ex-1:
a=0b 101
b=0B 1010
print (a) #5
print (b) #10
Ex-2:
a=0b 101
b=0B 1012
print (a) #5
print (b) #ERROR( BECAUSE 0 AND 1 ARE CONSIDERED)
OCTAL FORM:
To represent a octal number we can use 0 to 7. and to represent octal integer we can use 0o(zero-alphabet o).
Ex-1:
a=0o 123
b=0o 146
print (a) # 83
print (b) #102
Ex-2:
a= 0o 189
b=0o 149
print (a) # ERROR [ BECAUSE HERE IN OCTAL FORM THERE IS UPTO 7]
print (b) # ERROR [ BECAUSE HERE IN OCTAL FORM THERE IS UPTO 7]
HEXA DCIMAL FORM:
To represent hexa decimal number we can use 0 to 9 and A, B, C, D, E, F= 16
To represent hexa decimal number we can us X (or) x.
Ex-1:
a= 0x 1A
b=OX ace
print (a) #26
print (b) # 2766
Ex-2:
a=0X boy
b= 0X hen
print (a) #ERROR
print (b) #ERROR
-
FLOAT:
To represent floating values we can use float data type. Here some of the examples as follows:
Ex-1:
x=10.5
y=15.60
z=120.77
We can represent float values in the form of exponent form
a= 1.2e3
print (a)# 1200 [ 1.2 * 103]
-
COMPLEX:
To represent complex numbers we can use complex data-type. We can represent complex numbers in the form of (a+bj). Here a is a real part, b is a imaginary part
Ex-1:
a=10+2j
b=15-0j
print (a) # 10+2j
print (b) # 15-0j
-
BOOLEAN:
To represent Boolean data we can use bool datatype. It is indicate with bool data-type. Some of the examples as follows:
Ex-1:
a=True
b=False
print (a) # True
print ( b) # False
print (a+b) #1
Now, True is internally considered as “1” and False is considered as “ 0”.
Ex-2:
a= true
print (a) # ERROR (because we should use capital letter for “T” it is a keyword)
-
STRING:
It is indicated with “str”. To represent the string type data. We can use str data-type.
Ex-1:
a=”Purna”
b=”tech”
print (a) # Purna
print (b) # tech
print (a+b) # Purnatech
Note-1: If both operands are strings then “+” operator acts as concatenation operator. If both operands are numbers then it acts as “+” operator.
Ex-2:
a=” Purna”
b= 5
print (a+b) # ERROR( Because “a” is a string and “b” is a number)
Ex:3
a=” Purna”
b= 5
print (a*b) # PurnaPurnaPurnaPurnaPurna
Note-2: Here, first operand is string , second operand must be integer.
Ex-1:
a=’ Purna “ Tech” Solutions ‘
print (a) #Purna “ Tech” Solutions
We can assign multi level strings to a variable.
-
BYTES:
It is a predefined functions. It is a collection of numbers. The range is in between [ 0-256 ]. Bytes is immutable (once data is enter it cannot be modified). Bytes contains list of numbers.
Ex-1:
a= [ 12,13,15,25,60,100]
b= bytes(a)
print (b[0]) #12
print (b[1]) #13
-
BYTE-ARRAY:
Byte-array contains list of numbers from [0-256]. Byte-array is mutable.
Ex-1:
a= [ 12,13,15,25,60,100]
b= bytearray (a)
b[0]=100
print (b[0]) #100
print (b) # [100,13,15,25,60,100]
-
LIST:
List contains heterogeneous elements. In list there is insertion order is preserved. Duplicates are allowed. List is mutable. We can represent list of elements in []. There is no length limit. List is growable (elements to be added).
Ex-1:
l=[15, 10.5, “purna”, True, 600, 15 ]
print ( l[2]) #purna
print ( l[0:5]) # [15, 10.5, purna, True, 600] (because the elements are 5-1=6)
print ( l[ : 6]) #[15, 10.5, purna, True, 600]
-
TUPLE:
Tuple contains heterogeneous elements. In tuple there is insertion order is preserved. Duplicates are allowed. Tuple is immutable. We can represent the element within[]. There is no length limit.
Ex:
t=( 10,20,30,” Purna”,10.5)
print(t(0) #10
print(t(0:3)) # [10,20,30]
t[0]=100 # ERROR
-
SET:
Set contains hetrogeneous elements. Insertion order is preserved. Set is growable . Duplicates are not allowed. Set is mutable.
Ex-1:
x=set()
x.add(10)
x.add(“purna”)
x.add(10.5)
x={10, 10.5 ,“ purna”}
-
FROZENSET:
Example of frozenset : is Web application form of college. It is immutable. Duplicates are not allowed.
Ex:
x={2,3,4,5,10}
y=frozenset(x) #frozenset({2,3,4,5,10})
-
RANGE:
To represent series of numbers w can use range datatype.
FORM-1:
It will generates numbers from 0-(n-1)
*Write a program to print following series:
0,1,2,3,4——20
Answer:
for x in range (21)
print(x) 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
FORM-2:
range(begin, end)
for x in range(2,10)
print(x) #2,3,4,5,6,7,8,9
*Write a program to print n natural numbers(n+1)
Answer: n=int(input(“Enter n”))
for x in range(1,n+1)
print(x) #1———n
FORM-3:
range(begin ,end ,step)
for x in range(2,20,2)
print(x) #2,4,6,8,10,12,14,16,18
-
DICTIONERY:
To represent key value pairs w can use dict data type. Dict is mutable.
d={ } It refers to empty set.
Ex:
d[100]=”purna”
d[200]=”raju”
d[300]=”srinu”
print(d) #{100 :”purna” , 200:”raju” , 300:”srinu”}
-
NONE:
1)
def add(a,b) [function declaration]
print(a+b) [implementation]
add(10,20) [function calling]
2) If function does not return any value then
def add( a,b)
print(a+b)
print(add(10,30)) # 40 None.