CBSE Class 11 & 12 Computer Science and Informatics Practices Python Materials, Video Lecture

Data Types In Python

Data Types in Python

In this post, I am going to discuss various types of data that you can store in Python. The data can be stored in mutable or immutable types of variable.

Let us now discuss CBSE class 11 subject Informatics practices and Computer Science chapter Data Types in Python.

What is Data type?

A data type or simply a type is a property of data, that tells the language processor (compiler, interpreter) how we are going to use this data. 
  • Data types tell the meaning of data, how that data is going to store in memory and what different operations can be performed on it. 
  • 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 vs Immutable Data Types

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)
>>> v = 20
>>> id(v)

Immutable:- Immutable data types are objects whose value cannot be changed once it is created. 
Ex:- int, float, bool, string, tuple
>>> l = [1,2,3]
>>> type(l)
<class 'list'>
>>> id(l)
>>> l[0] = 99
>>> l
[99, 2, 3]
>>> id(l)

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. 

Python offers the following built-in core data types -: 
  1. Numbers
  2. String
  3. List
  4. Tuple
  5. Dictionary
See the description of these data types in the following table:

S. No. Data types Description
1 Numbers 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 -:
-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
2 String It is a sequence of Unicode characters.
It can hold any type of known characters, like letters, numbers.
It is a derived data type.
Example-: 'abcd', '1234', '????'
3 List It represents a list of comma-separated values of any datatype between square brackets.
It is a compound data types.
The list is changeable means one can change/add/delete the elements of a list.
Example -:[1, 2, 3, 4] ['a', 'b', 'c', 'd']
4 Tuple the tuple is immutable/non-changeable that means one can not make a change to a tuple.
Example :
p= (1, 2, 3, 4,5)
q = (2, 4, 6, 8)
r = ('a', 'b', 'c', 'd')
r = ('2', '5', '7', 'a', 'b', 'c')
5 Dictionary It is in an unordered set of comma-separated key: value pairs.
No two keys can be the same (unique keys within a dictionary).

The type() function is used to check the data type


As it is clear by the name the number data types is used to store numeric values in Python, which means Number data type stores only numerical values.

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

## var1 is an integer variable
var1 = 10

<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'>

Floating Points Number -:

If we have to declare the variables in our program which are to be used to stores decimal numbers, then we declare such variables as a float data type. 
  • A number which has some parts as fractional is called floating-point number.
  • The decimal point (i.e. a dot ".") signals that this number is a floating-point number, not an Integer.
  • Floating numbers can be positive or negative.
  • Floating points variables represent real numbers. Which are used for measurable quantities like distance, area, temperature, etc. and typically have a fractional part.
  • Floating numbers represent machine-level double precision floating point numbers (15 digit precision). 


  • They can represent a value between the Integers.
  • We can represent a large range of values using it.

Disadvantage -:

  • Floating points operations are usually slower than integer operations.
Example -:
## var1 is a floating type variable
var1 = 5.75
type(var1)   ##Output: <class 'float'>

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'>

var1.real    ##Output: 5.0
var1.imag    ##Output: 3.0

String Data type

The string is written within a single(' ') or doubles (“ “) quotes.

When we provide information to the user, then the information is always represented as a group of characters, the group of characters is called String.

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

## with double quotes
var1 = "Hello"
type(var1)     ## <class 'str'>

## with single quotes
var2 = 'Hello'
type(var2)     ## <class 'str'>

var3 = '12345'
type(var3)     ## <class 'str'>

List Data type

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

## with similar data type variables 
var1 = [1,2,3,4]
type(var1)     ## <class 'list'>

var2 = ['A','B','C','D']
type(var2)     ## <class 'list'>

## with different data type variables
var3 = ["Hello",1,2,3.5]
type(var3)     ## <class 'list'>

Tuples Data type

  • A tuple is a sequence of items separated by commas and items are enclosed in parenthesis { '(' and ')'}. This is unlike the list, where values are enclosed in square brackets {'[', ']'}.
  • The difference between List and Tuple is that the Tuples can not be changed or modified once created. That means we can not change items in the tuple. The similarity to a list is that the items of Tuples may be of different data types.
Example -:
## var1,var2,var3 are list data type variables

## with similar data type variables 
var1 = (1,2,3,4)
type(var1)     ## <class 'tuple'>

var2 = ('A','B','C','D')
type(var2)     ## <class 'tuple'>

## with different data type variables
var3 = ("Hello",1,2,3.5)
type(var3)     ## <class 'tuple'>


No comments:

Post a Comment