Python data types ?


In python we do not required to define data type of variable. it automatically define types of variable according to data enter into variable. In C ,C++, JAVA we need to define types of variable.
Like :
int a; float b; char c; etc.
This is called statically defined data types or programming language.
But in python,
No need to define type just
a= 4
b= 4.5
c= "c"
this is called dynamically define data types or programming language.

Note:- If you want to see type of variable in python use type(variable name) function.

Data types


  • The data types define the types of data stored in a memory location.
  • Data types specify how we enter data into our programs and what type of data we enter.

compiler-shineskill

The None type

The None datatype represents an object that does not contain any value. In languages like Java it is called null object but in python it is called None object. In python program maximum of only one None object is provided.


int data type

The int data type represents an integer number. An integer number is a number without any decimal point or fraction part.
Example :
a= 34 b= -24
Here, a and b is called int type variable since it hold integer value i.e 34 and -24

Note:- there is no limit for the size of an int data type it can store very large integer numbers

float data types

The float data type represents floating point number . It contain a decimal point.

Example:
0.4, -5.4534, 380 that is called floating point number
p=3.14
Here, p is called floating point data type variable since it hold floating value i.e 3.14.

Complex data types

A complex number is a number that is written in the form of a+bi
Here, a is represents the real part of the number and b represents the imaginary part of the number the suffix j or J after b indicates the square root value of -1 the part a and b may contain integers or floats value.


for example :
6+3j are called complex number.

# Python program to add two complex numbers:-
C1=2.5+2.5j
C2=3.0-0.5j
C3=C1+C2
print(“sum=“,C3)
Output :
sum=(5.5+2j)

bool data types

The bool datatype in python represents boolenan values. There are only two boolean values True or False that can be represented by this datatypes python internally represents True as 1 and False as 0. A blank string like “ ” is also represented as false condition will be evaluated internally to either true or false.

Example:-
b=True
A python program
a=4
b=6
c= a<b
print(c) -->True will be output
True+True= will display 2 #true is 1 and false is 0
True-false= will display 1

Str data types

In python str represents string datatype. A string is represented by a group of characters. Strings are enclosed in single quotes or double quotes both are valid.
Example:-
str=“shineskill” # str is a string type variable that are store shineskill as a value.
str ='shineskill' #same as first

Note :
=> If we want to store multiple line string to store in a variable we have to use “””(triple double quotes) or "‟‟(triple single quotes)
str1=“”” shineskill
.....online class “””
str2="‟‟ this is computer
.....this is laptop‟‟‟
The bytes data types represents a group of byte numbers just like an array does.
A byte number is any positive integer form 0 to 255 (inclusive).
Bytes array can store numbers in the range from 0 to 255 and it cannot even store negative numbers.

Example :
elements=[20, 0, 30, 34, 23] #this is a list of byte numbers
x= bytes(elements) #convert the list into bytes array
print(x[0]) # display 0th element i.e 20

Bytes array we can not modify or edit any element in the bytes
Example :
x[0]=34 gives an error. We are trying to replace 0th element 20 by 34 which is not allowed.

bytearray data types

the bytearray datatype is similar to bytes datatype. The difference is that the bytes type array cannot be modified but bytearray type array can be modified . It means any element or all the elements of the bytearray type can be modified.

Example :

elements=[20, 0, 30, 34, 23] #this is a list of byte numbers.
x= bytearray(elements) #convert the list into bytearray.
print(x[0]) # display 0th element i.e 20
=> We can modify or edit the element of bytearray.
x[0]=50 # replace 20 by 50.

list data type

List in python are similar to array in c or java. A list represents a group of elements. The main difference between a list in python or array in c or c++ java is that.

=> A list can store different types of elements but an array can store only one type of elements.
=> Also lists can grow dynamically in memory. But the size of array is fixed and they cannot grow at runtime.
=> List are represented using square brackets[] and the elements are written in [] separated but commas.

Example :
list = [10, 20, -23, 24,-45, „raj‟, „rajesh‟]
Will create a list with different types of elements the slicing operation like [0: 3]
Represents elements from 0th to 2nd positions i.e 10 and 20

tuple datatype

A tuple is similar to a list. A tuple contains a group of elements which can be of different types. The elements in the tuple are separated by commas and enclosed in parentheses (). Whereas the list elements can be modified it is not possible to modify the tuple elements. That means a tuple can be treated as a red only list.


Example :

tp(10,40,-34,‟raj‟)
The individual elements of the tuple can be referenced using square braces as tp[0], tp[1] new if we try to modify the 0th element as
Tp[0]=99
This will give error.

range datatype

The range datatype represents a sequence of numbers. The numbers in the range are not modifiable. Generally range is used for repeating a for loop for a specific number of time to repeat a range of number we can simply write
R=range(10)
Here, the range object are created with the numbers starting from 0 to 9 we can display these numbers using a for loop as:-
For i in r:print(i)
The above statement will display number from 0 to 9 we can use a starting number an ending number and a step value in the range object as
r=range(10,20,2)
This will create a range object with a starting number 10 and an ending number 20 and the step size is 2 it means the numbers in the range will increate by 2 every time so the for loop.
For I in r:print(i) # will display 10 12 14 ,…18

set datatype

a set is an unordered collection of elements much like a set in mathematics. The order of elements is not maintained in the sets. It means the elements may not appears in the same order as they are entered into the set moreover a set does not accept duplicate elements.


There are two sub types of sets : => Set datatype
=> Frozenset datatype

Set datatype
To create a set we should enter the elements separated by commas inside curly braces{ }
s={10, 20, 30 ,20, 5, 6}
print(s)
Set s is not maintaining the order of the elements. We entered the elements in the order 10 20 30 20 and 40 but it is showing another order.
Also we repeated the element 20 in the set but it has stored only one 20. we can use the set () function to create a set as :-
Example :
ch = set(hellow)
print(ch) # display as {"h‟,‟e‟,‟l‟,‟o‟,‟w‟}

Remove function can be use to remove a particular value from a set
s.remove(20) #dispaly 10 20 30 5 6

Update method can be add elements to a set
s.update([10,60])

frozenset datatype

The frozenset datatype is same as the set datatype . The main difference is that the elements in the set datatype can be modified whereas the elements of frozenset cannot be modified we can create a frozenet by passing a set to frozenset() function

Example :
S={50,60,90}
print(s) # may display {50,60,90}
fs=frozenset(s) #create frozenset s
print(fs) # it may display frozenset({50,60,90})

Note:- Here, update and remove method will not work on frozensets since they cannot be modifiy or update.

Mapping in Python

A map represents a group of elements in the form of key value pairs so that when the key is given we an retrieve the value associated with it. The dict datatype is an example for a map. The dict represents a dictionary that contains pairs of elements such that the first element represents the key and the next one comes its value.
The key and its value should be separated by a colon ( : ) and every pair should be separated by comma all the elements should be enclosed inside curly brackets {}

Example:-
d= {10: "raj", 11: "palak"}
Here, d is a name of dictionary and 10 is a key and its associated value is raj
Also, we can create a empty dictionary
d={}
d[10]="raj"
d[11]="palak".

Sequence in python

We can perform various operation on dictionary. To retrieve value upon giving the key we can simply mention d[key].
To retrive only keys from the dictionary we can use the method keys() and to get only values we can use the method values()

We can also update the value of key as d[key]= newvalue
We can delete a key and corresponding value using del module
Example :
Del d(11)
Will delete from directory

Type() function

Type() function is use to print type of variable or data types

Example:-
a = 20
type(a)
output :
<class "int">

b = True
type(b)
output :
<class "bool‟>

Conversion

We can convert integer number into the following : binary of 65 is (1000001)2
octal of 65 is (101)8
Hexa of 65 is (41)16

Python provide in-built functions for base conversions.
bin()
we can use bin() function to convert decimal to binary format.
Example :-
>>>bin(65)
"0b1000001"

oct()
we can use oct() function to convert from any base to octal format.
Example :-
>>>oct(65) "o0101"

Hex():
>>>hex(65)
"0x41"