# Python Data Types

This lesson will teach you about the different data you can use in Python, with a few examples to understand the topic better.

## Data Types in Python

In Python, each value has a datatype, and everything is an object; hence variables and data types are both instances (and objects) of the same classes.

The following list includes a few of the key data types.

• Python Numbers
• Python List
• Python Tuple
• Python String
• Python Set
• Python Dictionary

### Python Numbers

Python numbers include complex numbers, floating point numbers, and integers. In Python, they are referred to as int, float, and complicated classes. Also, you can use the type() method to determine the class to which a variable or value belongs. Similarly, an object’s class membership can be determined using the isinstance() function.

Example

mathMarks = 70
print (mathMarks, 'is of type', type(mathMarks))
mathMarks = 70.0
print (mathMarks, 'is of type', type(mathMarks))
mathMarks = 1 + 2j
print (mathMarks, 'is it a complex number?', isinstance(1 + 2j,complex))

Output

70 is of type <class 'int'>
70.0 is of type <class 'float'>
(1+2j) is it a complex number? True
1. Only the amount of memory available restricts the length of integers.
2. Decimal points distinguish between floating and integer numbers. A floating-point number is 1.0, and an integer is 1. There are 15 decimal places in a floating-point number.
3. Complex numbers are a + b, where a denotes the real component and b is the imaginary component.

Let’s look at a few instances.

Example

mathMarks = 701352132145761132
engMarks = 0.701352132145761132
hisMarks = 1+2j
> engMarks
0.7013521321457611
> mathMarks
701352132145761132
> hisMarks
(1+2j)

### Python List

A list is an orderly collection of things; a list does not necessarily need to include just items of the same type. It is a highly flexible datatype and one of the most popular in Python. Declaring a list is a relatively simple process, and commas delimit the spaces between items.

Example

Python: a = [1, 2.2]

In Python, you can utilize the slicing operator [] to extract one or more items from a list.

Important Note
In Python List, the index always starts at 0.

Example

mathMarks = [5,10,15,20,25,30,35,40]
# mathMarks = 15
print("mathMarks = ", mathMarks)
# mathMarks[0:3] = [5, 10, 15]
print("mathMarks[0:3] = ", mathMarks[0:3])
# mathMarks[5:] = [30, 35, 40]
print("mathMarks[5:] = ", mathMarks[5:])

Output

mathMarks = 15
mathMarks[0:3] = [5, 10, 15]
mathMarks[5:] = [30, 35, 40]
You can change the value of a list's elements since lists are mutable.
mathMarks = [5,10,15]
mathMarks = 150
print("mathMarks = ", mathMarks)

### Python Tuple

Similar to a list, a tuple is an ordered series of elements. The fact that tuples are immutable is the only distinction. Once formed, you can’t change the tuples. Because they cannot alter dynamically, tuples are used to write-protect data and are typically faster than lists.

It is defined between parentheses (), with commas between each component.

total = (5,'Six', 1+3j)

In Python, you can utilize the slicing operator [] to remove objects but cannot alter their value.

Example

total = (5,'Six', 1+3j)
# total = 'Six'
print("total = ", total)
# total[0:3] = (5, 'Six', (1+3j))
print("total[0:3] = ", total[0:3])
# Generatotales error
# totaluples are immutable
total = 10

Output

total = Six
total[0:3] = (5, 'Six', (1+3j))

Traceback (most recent call last):
File "<string>", line 9, in <module>

TypeError: 'tuple' object does not support item assignment

Now let’s move to another Data type in Python, string!

### Python String

An array of Unicode characters is a string. To represent strings, You can use single or double quotations, and triple quotes” or “” can indicate multiline strings.

Example

stringVar = "String Example"
print(stringVar)
stringVar = '''A multiline
string'''
print(stringVar)

Output

String Example
A multiline
string

The slicing operator [] can be applied to strings like lists and tuples. But you can never change strings.

Example

stringVar = 'Hellu world!'
# stringVar = 'u'
print("stringVar = ", stringVar)
# stringVar[6:11] = 'world'
print("stringVar[6:11] = ", stringVar[6:11])

# Generate string Var error
# string are immutable in Python
stringVar ='f'

Output

stringVar = u
stringVar[6:11] = world
Traceback (most recent call last):
File "<string>", line 10, in <module>
TypeError: 'str' object does not support item assignment

Now, let’s move to the Set data type in Python.

### Python Set

A set is a grouping of distinct elements that are not arranged. A set’s components don’t follow any particular order, and values separated by commas are used to define sets.

Example

set = {50,42,53,81,4}
# printing set variable
print("set = ", set)
# data type of variable set
print(type(set))

Output

set = {4, 42, 81, 50, 53}
<class 'set'>
>

On two sets, we can perform set operations like union and intersection. Sets contain distinct values, and Duplicates are eliminated. See the example below:

Example

set = {50,42,53,81,4,50,42,53,81,4}
# printing set variable
print("set = ", set)
# data type of variable set
print(type(set))

Output

set = {4, 42, 81, 50, 53}
<class 'set'>
>

Sets are unordered collections, so indexing is useless. As a result, the slicing operator [] is useless.

Example

set = {50,42,53,81,4,50,42,53,81,4}
set

Output

Traceback (most recent call last):
File "<string>", line 3, in <module>
TypeError: 'set' object is not subscriptable
>

### Python Dictionary

Dictionaries are designed with data retrieval in mind. A dictionary is a collection of key and value pairs that are not ordered. It is typically utilized when there is a ton of data. To retrieve the value back, we need to know the key.

Accessing Values in Python Dictionary

You can use the well-known square brackets and the key to access dictionary items. Here is a simple illustration:

Example

dict = {'Name': 'John', 'Age': 20}
print ("dict['Name'] ", dict['Name'])
print ("dict['Age'] ", dict['Age'])

Output

dict['Name'] John
dict['Age'] 20

#### Updating Values in Python Dictionary

As shown in the simple example below, you can update a dictionary by adding a new item or key-value pair, changing an existing entry, or removing an existing one.

Example

dict = {'Name': 'John', 'Age': 20}
print ("dict['Name'] ", dict['Name'])
print ("dict['Age'] ", dict['Age'])
dict = {'Name': 'Humna', 'Age': 17}
print ("dict['Name'] ", dict['Name'])
print ("dict['Age'] ", dict['Age'])

Output

dict['Name'] John
dict['Age'] 20
dict['Name'] Humna
dict['Age'] 17

#### Delete Values from Python Dictionary

You can either erase individual dictionary entries or clear the dictionary’s complete contents. Additionally, you can erase an entire dictionary with a single operation.

To delete an entire dictionary, use the del statement. The following is a simple illustration:

Example

dict = {'Name': 'John', 'Age': 20}
print ("dict['Name'] ", dict['Name'])
print ("dict['Age'] ", dict['Age'])
del dict['Name']; # remove entry with key 'Name'
dict.clear(); # remove all entries in dict
del dict ;
print ("dict['Age']: ", dict['Age'])

Output

dict['Name'] John
dict['Age'] 20
Traceback (most recent call last):
File "<string>", line 7, in <module>
TypeError: 'type' object is not subscriptable

### Characteristics of Dictionary Keys

No limitations apply to dictionary values, and they may be either standard Python objects or user-defined objects. However, the same cannot be said for the keys.

There are two essential considerations regarding dictionary keys.

1. Multiple entries per key are not permitted. Therefore, duplicate keys are prohibited. When duplicate keys are assigned, the most recent assignment takes precedence.
2. A key must be unchangeable. Therefore, you may use strings, numbers, or tuples as dictionary keys, but [‘key’] is not permitted.

Dictionary definitions in Python are enclosed in brackets. Each dictionary entry is a pair of the form key: value. Any type of value or key is acceptable.

dVar = {1:'value','key':2}
> type(dVar)
<class 'dict'>

We use the key to retrieve the respective value. But not the other way around.

Example

dVar = {1:'value','key':2}
print(type(dVar))
print("dVar = ", dVar)
print("dVar['key'] = ", dVar['key'])
# Generates error
print("dVar = ", dVar)

Output

<class 'dict'>
dVar = value
dVar['key'] = 2
Traceback (most recent call last):
File "<string>", line 9, in <module>
KeyError: 2
>

This concludes the Python Data Type lesson. In the next lesson, you will learn the type conversion operations in Python.