# A5. Python Data Types

This tutorial will guide you through the various data types available in Python, and provide examples to help you understand their usage.

Understanding data types is crucial in programming, as they determine the kind of information that can be stored in a variable. For instance, in the following example, the variable “num” is assigned the integer value of 24:

num = 24


As a result, the data type of “num” is of the int class.

## Python Data Types #

Data TypesClassesDescription
Numericint, float, complexholds numeric values
Stringstrholds sequence of characters
Sequencelist, tuple, rangeholds collection of items
Mappingdictholds data in key-value pair form
Booleanboolholds either True or False
Setset, frozeensethold collection of unique items

In Python programming, all entities are represented as objects, with data types being classes and variables being instances of these classes. ## Python Numeric Data type #

In Python, the numeric data type is utilized to store numerical values. This includes integers, floating-point numbers, and complex numbers, which are defined as int, float, and complex classes in Python.

The int class is used to hold signed integers of unlimited length, while the float class is used to hold numbers with decimal points and has a level of accuracy of up to 15 decimal places. The complex class is used to hold complex numbers.

We can easily determine the class a variable or value belongs to by using the type() function. For example, if we want to know the class of the variable x, we can simply use the type(x) function.

For example :

a = 10
print(a, 'is type : ', type(a))

b = 354.2
print(b, 'is type : ', type(b))

c = 100+20j
print(c, 'is type : ', type(c))


The output of the above example is :

10 is type :  <class 'int'>
354.2 is type :  <class 'float'>
(100+20j) is type :  <class 'complex'>


Explanation :

The above example create 3 variables named a ,b and c, where we print the type by using the inbuilt function : type(x)

1. a is an integer, type() returns <class ‘int’> i.e Integer datatype
2. b is an float, type() returns <class ‘float’> i.e floating datatype
3. c is an complex, type() returns <class ‘complex’> i.e complex datatype

## Python List Data Type #

A List in python is a mutable sequence type, which implies you can add, remove or modify the items in the list after it is created. Lists can contain elements of different data types such as

1. Integers,
2. Floating-point numbers,
3. Strings, and othe datatypes.

For example :

my_list = [1, 2, 3, 'four', 5.0]


In the above example first three elements are integers, the fourth element is a string, and the fifth element is a floating-point number.

### How to access Python list #

You can access individual elements in a list by their index, which is the position of the element in the list. In Python, list indices start from 0, which means that the first element in the list has an index of 0. my_list = [1, 2, 3, 'four', 5.0]

# access element at index 0
print(my_list)   # Number 1 will be returned

# access element at index 2
print(my_list)   # 'four' will be returned


Output :

1
four


In the above example :

1. my_list - access first item from languages i.e. 1
2. my_list - access third item from languages i.e. four

You can also perform various operations on lists in Python, such as adding or removing elements from the list, concatenating two lists, sorting a list, and more.

## Python Tuple Data Type #

A tuple in python is a collection of ordered, immutable elements. Unlike Python list, tuples cannot be modified once they are created.

Tuples are often used to group related pieces of information together, such as a set of coordinates or a date and time.

We use the parentheses () to store items of a tuple. For example :

example_tuple = ('Elon Mush', 'bought','twitter','for',44.0,'Billion $')  Here, the example_tuple has six values, with 44.0 as a floating point datatype. ### How to access python tuple # You can access individual elements in a tuple by their index, just like in a list. example_tuple = ('Elon Mush', 'bought','twitter','for',44.0,'Billion$')

# access element at index 0
print(example_tuple,'is of type: ' ,type(example_tuple))   # Elon Musk

# access element at index 4
print(example_tuple,'is of type: ', type(example_tuple) )   # 44.0

# access element at index 5
print(example_tuple,'is of type: ', type(example_tuple))   # Billion $ The output of the above code is : Elon Mush is of type: <class 'str'> 44.0 is of type: <class 'float'> Billion$ is of type:  <class 'str'>


You can access individual elements in a tuple by their index, just like in a list. However, because tuples are immutable, you cannot modify individual elements in a tuple:

example_tuple = ('Elon Mush', 'bought','twitter','for',44.0,'Billion $') example_tuple = '100.0' # results in an error : TypeError because tuples are immutable  Tuples are often used in situations where you want to ensure that the contents of a collection cannot be changed. For example, you might use a tuple to represent the dimensions of an image file, to ensure that the dimensions cannot be accidentally changed. ⓘ Sponsored by 10xer.co ## Python String Data Type # In Python, a string is a sequence of characters. It is a data type used to represent text or any sequence of characters, including letters, numbers, and symbols. Strings in python are are enclosed in : 1. Single quotes (’…’) 2. Double quotes ("…") For example : # Using Single quotes and Double quote my_string_1 = 'Hello, world!' my_string_2 = "Python is awesome." my_string_3 = '1234' my_string_4 = '#$%&*'


In the above exmaple we create 4 strings with the values, ‘Hello, world!’, “Python is awesome.” ,‘1234’,’#\$%&*’

## Python Set Data Type #

A Python set is an unordered collection of unique elements.

It is a built-in data type that can be used to store a collection of items, much like a list or a tuple. However, unlike lists or tuples, sets do not allow duplicates.

Set is defined by values separated by commas inside curly braces { }. For example:

my_set = {1, 2, 3, 4, 5}
# create a set named my_set

# display my_set elements
print(my_set)

# display type of my_set
print(type(my_set))



Output :

{1, 2, 3, 4, 5}
<class 'set'>


Here, we have created a set named my_list with 5 integer values. Because sets are unordered collections, indexing is not meaningful.

## Python Dictionary Data Type #

A Python dictionary is a collection of key-value pairs, where each key is associated with a value.

It is an unordered, mutable data type that can be used to store a variety of data types, including strings, numbers, and other objects.

For example :

my_dict = {"name": "Elon", "age": 30, "city": "New York"}


In the above example, my_dict is a dictionary that contains three key-value pairs.

The keys are “name”, “age”, and “city”, and the values are “Alice”, 30, and “New York”, respectively.

These values can be accessed using the keys that we created a above.

print(my_dict) # Output: {"name": "Elon", "age": 30, "city": "New York"}
print(my_dict["name"])   # Output: "Elon"
print(my_dict["age"])    # Output: 30


To add a new key, we can add by using the below command :

my_dict["occupation"] = "Engineer"


Here, in the my_dict dictonary is :

1. Keys are : name, age, city, occupation
2. Values are : Elon, 30, New York, Engineer

### Access Dictionary Values Using Keys #

Keys are used to access the values, values can not be used to get Keys For example :

my_dict = {"name": "Elon", "age": 30, "city": "New York"}

print(my_dict) # Output: {"name": "Elon", "age": 30, "city": "New York"}
print(my_dict["name"])   # Output: "Elon"
print(my_dict["age"])    # Output: 30


Output :

{'name': 'Elon', 'age': 30, 'city': 'New York'}
Elon
30


If try to access a key, that does not exist in the dictionary we get an error. 