Datatypes in Python
Datatypes in Python
In the Python programming language, if we have to store any value in a variable, then
the data type role comes into play.
when we are storing a value in a variable, we have to use the same types of data as the
type of variable. Each value belongs to some data type in Python.
Data type identifies the type of data which a variable can hold and the operations that
can be performed on those data.
Mutable:- Mutable data types are objects whose value can be changed once it is created. This
type of object allows changing the value in place.
Ex:- List, Dictionary, Set
>>> v = 10
>>> type(v)
<class 'int'>
>>> id(v)
140731797110112
>>> v = 20
>>> id(v)
140731797110432
Immutable:- Immutable data types are objects whose value cannot be changed once it is
created.
Ex:- int, float, bool, string, tuple
1 >>> l = [1,2,3]
2 >>> type(l)
3 <class 'list'>
4 >>> id(l)
5 2413299978760
6 >>> l[0] = 99
7 >>> l
8 [99, 2, 3]
9 >>> id(l)
10 2413299978760
Data Types In Python -:
There are many data types available in python like character, integer, real, string, etc
Before you learn how can process different types of data in Python, let us discuss various
data types supported by Python. Everything in the Python programming language is an
object, the data types are actually classes and variables are objects of these classes.
S.
Data types Description
No.
Integer Number-:
-It is of unlimited range, which depends on the availability of memory.
-We can not use the Decimal number in it.
Example -: 5, 32, 1876,0
Boolean Value -:
Two value TRUE (1) / FALSE (0)
Floating Point Number -:
1 Numbers -It is of unlimited range, range depends upon the memory in the machine
architecture.
-It is used to store a decimal number.
Example -: 5.0, 32.0
Complex Number -:
Same as floating-point numbers, because the real and imaginary parts are
represented as floats.
Example-: A+Bj
It is a sequence of Unicode characters.
It can hold any type of known characters, like letters, numbers.
2 String
It is a derived data type.
Example-: 'abcd', '1234', '????'
It represents a list of comma-separated values of any datatype between
square brackets.
It is a compound data types.
3 List
The list is changeable means one can change/add/delete the elements of a
list.
Example -:[1, 2, 3, 4] ['a', 'b', 'c', 'd']
the tuple is immutable/non-changeable that means one can not make a
change to a tuple.
Example :
4 Tuple p= (1, 2, 3, 4,5)
q = (2, 4, 6, 8)
r = ('a', 'b', 'c', 'd')
r = ('2', '5', '7', 'a', 'b', 'c')
It is in an unordered set of comma-separated key: value pairs.
5 Dictionary
No two keys can be the same (unique keys within a dictionary).
When a number is assigned to a variable, then Python creates an object of Number Class.
Now, we will understand which numeric data type does Python support. We will tell you
about it below -:
1. Integer Numbers
2. Boolean data type
3. Floating Point Numbers
4. Complex Numbers
Integer Numbers -:
Integers are whole numbers such as 2, 43, 2356, 0, etc.
They do not have any fraction parts. In Python, Integers are represented by numeric
value with no decimal point.
Integers can be positive or negative. Positive numbers are represented with a + (plus)
sign, and negative numbers are represented by - (minus) sign.
Integers in Python 3.x can be of any length, it is limited by the memory available.
Python provides single data type integer to store an integer, whether big or small.
Example -:
##Output
<class 'int'>
Boolean Value -:
Just like in real life some questions are answered in yes or no, similarly, the computer also
gives answers in yes or no. To store this type of value in a computer, we use boolean data
type. A boolean value is represented by True or False.
It represents the value only in True and False.
The boolean type is a subtype of plain integers.
True and False behave like (1) and (0).
When user type bool(0) or bool(1), Then Python will return a value False and True
respectively. bool(0) will always give False. Any number other than 0, weather +ve or -ve
will give True if passed inside bool().
There is no conversion between the Boolean Data Type and Integer Data Type.
We can directly use Boolean value True/False in any kind of conditional statement.
Example -:
## var1 and var2 is an boolean variable
var1 = True
type(var1) ##Output: <class 'bool'>
var2 = False
type(var2) ##Output: <class 'bool'>
Complex Number -:
Python represents complex numbers as a pair of floating-point numbers.
A complex number is a composite quantity made of two parts -: the Real part and
the Imaginary part. Both of which are represented internally as Float values (floating-point
numbers).
A complex number consists of both real and imaginary components.
In complex number A + Bi. where A is a real number and B is an imaginary part. i is
used for denoting the imaginary part.
Example -:
## var1 is a complex data type variable
var1 = 3 + 5j
type(var1) ##Output: <class 'complex'>
In Python 3.x, each character stored in a string is a Unicode character, which means all string
in Python 3.x is a sequence of pure Unicode characters. Unicode is a system which is
designed to represent every character from every language.
A string is the Group of characters. These characters may be digits alphabets or special
characters including spaces.
A string data type lets you hold string data like any number, of valid characters into a
set of quotation (" ") marks.
A string can hold any type of known characters like letters, numbers, and special
characters.
Following are string in python
'abcd' , '1234'
Example -:
## var1,var2,var3 are string data type variables
var3 = '12345'
type(var3) ## <class 'str'>
A list is an important data type of Python. Many values are kept in it. Each value is separated
by a comma, And all the values are placed inside a square bracket {'[', ']'}.
A list is a python compound data type. It can have different data types of data inside
it.
Lists can be changed/modified/mutate. This means List is a Mutable Data type.
A list in python represents a comma-separated value of any datatype between the
square bracket.
Example -:
## var1,var2,var3 are list data type variables
var2 = ['A','B','C','D']
type(var2) ## <class 'list'>
## with different data type variables
var3 = ["Hello",1,2,3.5]
type(var3) ## <class 'list'>
var2 = ('A','B','C','D')
type(var2) ## <class 'tuple'>