Python Data Types

By Kamila

By Kamila

Kamila is an AI-based technical expert, author, and trainer with a Master’s degree in CRM. She has over 15 years of work experience in several top-notch IT companies. She has published more than 500 articles on various Software Testing Related Topics, Programming Languages, AI Concepts,…

Learn about our editorial policies.
Updated March 7, 2024

Introduction to Python Data Types:

We learned about Python variables in detail in our previous tutorial.

In this tutorial, we will explore the various classifications of Python Data Types along with the concerned examples for your easy understanding.

An explicit variety of Python Training tutorials are presented to you in this series for enriching your knowledge on Python.

Python data types

Watch the VIDEO Tutorials

Python Data Types: Numbers, Strings and List:

Python Data Types: Tuple, Set, and Dictionary:

Python Data Types

A Data Type describes the characteristic of a variable.

Python has six standard Data Types:

#1) Numbers

In Numbers, there are mainly 3 types which include Integer, Float, and Complex.

These 3 are defined as a class in Python. In order to find to which class the variable belongs to you can use type () function.

Example:

a = 5
print(a, "is of type", type(a))

Output: 5 is of type <class ‘int’>

Python Data types - numbers example 1

b = 2.5
print(b, "is of type", type(b))

Output: 2.5 is of type <class ‘float’>

Python Data types - numbers example 2

c = 6+2j
print(c, "is a type", type(c))

Output: (6+2j) is a type <class ‘complex’>

Python Data types - numbers example 3

#2) String

A string is an ordered sequence of characters.

We can use single quotes or double quotes to represent strings. Multi-line strings can be represented using triple quotes, ”’ or “””.

Strings are immutable which means once we declare a string we can’t update the already declared string.

Example:

Single = 'Welcome'
or
Multi = "Welcome"

Multiline: ”Python is an interpreted high-level programming language for general-purpose programming. Created by Guido van Rossum and first released in 1991”

or

‘’’Python is an interpreted high-level programming language for general-purpose programming. Created by Guido van Rossum and first released in 1991.’’’

We can perform several operations in strings like Concatenation, Repetition, and Slicing.

Concatenation: It means the operation of joining two strings together.

Example:

String1 = "Welcome"
String2 ="To Python"
print(String1+String2)

Output: Welcome To Python

Python Data types - string multi_line example

Repetition:

It means repeating a sequence of instructions a certain number of times.

Example:

Print(String1*4)

Output: WelcomeWelcomeWelcomeWelcome

python repetition

Slicing: Slicing is a technique for extracting parts of a string.

Note: In Python, index starts from 0.

Example:

print(String1[2:5])

Output: lco

python slicing example 1

Python also supports negative index.

print(String1[-3:])

Output: ome

python slicing example 2

As Strings are immutable in Python, if we try to update the string, then it will generate an error.

Example:

String[1]= "D"

Output: TypeError: ‘str’ object does not support item assignment

python slicing example 3

#3) List

A list can contain a series of values.

List variables are declared by using brackets [ ]. A list is mutable, which means we can modify the list.

Example:

List = [2,4,5.5,"Hi"]
print("List[2] = ", List[2])

Output: List[2] =  5.5

Python Data type - list example

print("List[0:3] = ", List[0:3])

Output: List[0:3] =  [2, 4, 5.5]

Python Data type - list example 2

Updating the list:

List[3] = "Hello"
If we print the whole list, we can see the updated list.
print(List)

Output: [2, 4, 5.5, ‘Hello’]

Python - updating the list

#4) Tuple

A tuple is a sequence of Python objects separated by commas.

Tuples are immutable, which means tuples once created cannot be modified. Tuples are defined using parentheses ().

Example:

Tuple = (50,15,25.6,"Python")
print("Tuple[1] = ", Tuple[1])

Output: Tuple[1] =  15

Python Data type - tuple example 1

print("Tuple[0:3] =", Tuple[0:3])

Output: Tuple[0:3] =  (50, 15, 25.6)

Python Data type - tuple example 2

As Tuples are immutable in Python, if we try to update the tuple, then it will generate an error.

Example:

Tuple[2]= "D"

Output: TypeError: ‘tuple’ object does not support item assignment

Python Data type - tuple example 3

#5) Set

A set is an unordered collection of items. Set is defined by values separated by a comma inside braces { }.

Example:

Set = {5,1,2.6,"python"}
print(Set)

Output: {‘python’, 1, 5, 2.6}

Python Data type - set example 1

In the set, we can perform operations like union and intersection on two sets.

We can perform Union operation by Using | Operator.

Example:

A = {'a', 'c', 'd'}
B = {'c', 'd', 2 }
print('A U B =', A| B)

Output: A U B = {‘c’, ‘a’, 2, ‘d’}

Python Data type - set example 2

We can perform Intersection operation by Using & Operator.

A = {100, 7, 8}
B = {200, 4, 7}
print(A & B)

Output: {7}

Python Data type - set example 3

As the set is an unordered collection, indexing has no meaning. Hence the slicing operator [] does not work.

Set[1] = 49.3

Output: TypeError: ‘set’ object does not support item assignment

Python Data type - set example 4

#6) Dictionary

Dictionaries are the most flexible built-in data type in python.

Dictionaries items are stored and fetched by using the key. Dictionaries are used to store a huge amount of data. To retrieve the value we must know the key. In Python, dictionaries are defined within braces {}.

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

Syntax:

Key:value

Example:

Dict = {1:'Hi',2:7.5, 3:'Class'}
print(Dict)

Output: {1: ‘Hi’, 2: 7.5, 3: ‘Class’}

Python Data type - dictionary example 1

We can retrieve the value by using the following method:

Example:

print(Dict[2])

Output: 7.5

Python Data type - dictionary example 2

If we try to retrieve the value by using the value instead of the key, then it will generate an error.

Example:

 
print("Dict[7.5] = ", Dict[7.5]) 

Output:

Traceback (most recent call last):

File “<pyshell#1>”, line 1, in <module>

print(“Dict[7.5] = “, Dict[7.5])

KeyError: 7.5

Python Data type - dictionary example 3

We can update the dictionary by using the following methods as well:

Example:

Dict[3] = 'python'
print(Dict)

Output:

{1: ‘Hi’, 2: 7.5, 3: ‘python’}

python - update dictionary

Hope you must have understood the various classifications of Python Data Types by now, from this tutorial.

Our upcoming tutorial will explain you all about Python Operators!!

PREV Tutorial | NEXT Tutorial

Was this helpful?

Thanks for your feedback!

Leave a Comment