Select Page

## Data Types in Python

Every Programming Language supports Data Types, Data types in Python allow you to handdle fifferent types of data and validate them before they enter into your computer system. Python Data types can broadly be categorized as mutable and immutable types. uale types are hose which can be altered (modified) in place (i.e memory location where they are stored.

Depending upon what kind of value is stored in a variable, they can further be classified as discussed below:

## Different Python Data Types

### Numeric

In Python Integers, Floating point numbers, and Complex numbers fall into this category. In python all three of them are implemented using class int, float and complex. We can always use a type() function to find out a variable or a value passed to this function as arguement – belongs to which class. For example:

x=10
y=23.5
z=34.7j
print(type(10))
print(type(x))
print(type(23.5))
print(type(z))

Gives the output:

<class ‘int’>
<class ‘int’>
<class ‘float’>
<class ‘complex’>

#### Integers

Integer is another data type in Python.  In Python 3.x there is no upper limit to how big number can be stored in an integer variable. This totally depends upon the memory of your computer system. Only whole numbers which can be either negative or positive values are allowed.

For example, the statement below would run hassle free in python.

print(123456789123456789123456789123456789+ 1)

Output – 123456789123456789123456789123456790

#### Floats

Float type in python allows floating point numbers or numbers with decimal point.

Optionaly a nmber followe by character e or E, followed by a positive or negative number is also allowed to specify number is scientific notation. Obbserve the example below:

#defining float type of values
amt=3456.67
val=4.5e-3

#cross-checking
print(type(amt))
print(type(val))

output:

<class ‘float’>
<class ‘float’>

#### Complex Numbers

In Python Complex numbers are yet another Data type in Python, specified as combination of real part and imaginary part. Where imaginary part is followed by letter j.

Look at the example below:

cn=3.4j
print(type(cn))

output: <class ‘complex’>

### Sequences

In Python following sequence types are used:

#### Strings

In Python Strings is a sequence of one or more characters placed inside single quotes for example ‘hello’ or in double quotes for example “Welcome”.

Important: Anything placed inside single or double quotes is considered as string. Even numbers placed inside quotes are considered as strings.

Observe the examples below:

# Note single quotes or double quotes are same
# But you can not begin with single quote and end with double quote or vice versa

str1="Hello"
str2="A"
str3='Y'
str4="89"
str5='45.34'

print(type(str1))
print(type(str2))
print(type(str3))
print(type(str4))
print(type(str5))

Output:

<class ‘str’>
<class ‘str’>
<class ‘str’>
<class ‘str’>
<class ‘str’>

#### Lists

List is another Data Type in Python. List are quite flexible, very useful and mutable comma separated values placed inside square brackets [ ]. Important to note here is that these values need not be of same data types. You can have these values as combination of integers, floats, strings, even list inside list is also allowed along with many other complex data types that you will learn in the coming sections, for example you can also create a list containing tuples, dictionary etc.

Observe the examples below:

MarksList=[89,45,67,77,89.95]
Listofvalues=["Gaurav",99,97.5,89,89.5,94.5]

print(type(MarksList))
print(type(Listofvalues))

Output:

<class ‘list’>
<class ‘list’>

#### Tuples

Tuples are quite siimilar to Lists. The only difference is tuple is not mutable. Tuples are also comma separated values but these values are placed inside round brackets i.e. ( ). Important to note here is that these values need not be of same data types. You can have these values as combination of integers, floats, strings, even list inside list is also allowed along with many other complex data types that you will learn in the coming sections, for example you can also create a list containing tuples, dictionary etc.

Observe the examples below:

MarksList=(89,45,67,77,89.95)
Listofvalues=("Gaurav",99,97.5,89,89.5,94.5)

print(type(MarksList))
print(type(Listofvalues))

Output:

<class ‘tuple’>
<class ‘tuple’>

### Sets

In Python sets is a collection of values which is unordered and unindexed. When you display sets the values may appear in any order. Since values are not indexed you cannot use an index to display the particular value in a set.

### Maps

Dictionary is another Data Type in Python that belongs to this category. In dictionary we can store comma separated elements, which are stored as key:value pairs. Both key and value is separated by a colon (:). These key:value pairs are also known as key:value mappings.

• Dictionary is unordered (i.e. you don’t know in which order elements are stored.
• In a Dictionary, in a key:value pair – here key is immutable (cannot be altered or modified), where as value is mutable.
• Dictionary is indexed, hence its individual values can be accessed using the key as an index.

Observe the example below:

D={"Maths":89,"Science":78,"CS":94,"English":88}
print(D)
print(D["Science"])

Output:

{‘Maths’: 89, ‘Science’: 78, ‘CS’: 94, ‘English’: 88}
78

Observe by using the index i.e. D[‘Science’] we are able to extract it’s value (marks) 78.