Python Data Types

Python Data Types

10 mins read5.7K Views Comment
clickHere
Atul
Atul Harsha
Senior Manager Content
Updated on Sep 5, 2023 17:35 IST

You want to store a value, but that value can be anything. It can be a number, list of number, strings, etc. So these different type of values are stored in variables with different data type. In other words, data type determines the type of value stored in a variable. Let’s read more to understand the concept of data types in python in depth.

2021_10_Python-Data-Type.jpg

A Python data type defines the type of data stored in a variable. Unlike Java, C, C++ in Python we do not explicitly specify the data type of the variable. The data type in python is automatically detected when the value stored in the variable. This article covers different types of mutable and immutable data type in python including sets, numbers, string, tuple, lists, and dictionaries.

Also, explore Conditional Statements in Python

Table of Content

  1. Different data types in Python
  2. Mutable vs immutable data type in Python?
  3. Python Numbers
  4. Python Strings
  5. Python Lists
  6. Python Tuples
  7. Python Sets
  8. Python Dictionaries
  9. Python Objects

Python data types are used to store and manipulate data in a Python program. Different data types are suited for different kinds of data and allow for different types of operations to be performed on them.

Different Data Types in Python

Python data type is majorly categorized in two types mutable and immutable data types.

  • Mutable data type in Python
  • Immutable data type in Python

The best part of data type in python is that you don’t have to explicitly specify the data type of a variable (unlike other programming languages like Java, C, C++). Python automatically detects the data type according to the value in the variable and assigns the data type to it. If you don’t get it now don’t worry I have a series of examples lined up for you in this blog. Keep reading.

Explore Best Online Python Course

Mutable vs immutable data type in Python

  • A Mutable data types are those whose values can be changed. 
  • An immutable data type is one in which the values can be easily changed. 

Python Numbers

What is a Python Number?

  • Python Numbers are used to store numeric values
  • Most widely used data type in Python
  • Python Number belong to immutable data types category, i.e. changing the value of a python number will store it in a new memory location.

Definition: “Python numbers are the most commonly used data type in python to represent various types of numeric values. Eg: 1, 2.0, 3.14j, etc.

How to define a Python Number?

Syntax: Declaring a Number in Python

<Variable_name> = “value”
 
For example: 
var1 = 1

Python code:

 
'''
Defining numbers in Python - Type 1
no need to specify the data type of num_1
Python automatically decides the data type as per the value
stored in num_1
'''
num_1 = 10
print(num_1)
print(type(num_1))
#Defining numbers in Python - Type 2
num_1, num_2 = 20, 30.0
print(num_1,num_2)
Copy code

Python output:

10
<class 'int'>
20 30.0

Read more in Python Data Type Series

How to Reverse a String in Python
How to Reverse a String in Python
In Python programming, strings are a sequence of characters or symbols. As a beginner practicing Python string operations, you will often come across problems that need you to work with...read more
Methods to Check for Prime Numbers in Python
Methods to Check for Prime Numbers in Python
Prime numbers are the building blocks of mathematics, and they have fascinated mathematicians for centuries. Regarding Python programming, there are different methods to check whether the given number is a...read more
How to Check if a Python String is a Palindrome
How to Check if a Python String is a Palindrome
A palindrome is a sequence of the word, phrases, or numbers that read the same backward as forward. In this article, we will discuss different methods how to check if...read more
Web Scraping in Python with Scrapy
Web Scraping in Python with Scrapy
scrapping is the automated process of extracting data from internet quickly.This article includes topics like web scrapping architecture,web scrapping example with python implementation.
Working with Python Numbers
How to Find an Armstrong Number Using Python
How to Find an Armstrong Number Using Python
Learn how to find Armstrong numbers using Python with our step-by-step guide. Discover the algorithm to identify these special numbers, which are equal to the sum of their own digits...read more

Python Strings

What is Python Strings?

  • Python string is a sequence of characters enclosed within double (“…”) or single quotes (‘…’)
  • Character is just a symbol. Eg: the English language has 26 alphabets as a character set
  • Strings belong to immutable data types, i.e., their value cannot be changed

NOTE: Computers do not understand what characters you are using. All the characters are internally stored (encoded) as a combination of 0s and 1s. ASCII and Unicode are popular examples of encoding used.

Definition: Python String is a sequence of characters written within the quotes (“…”, ‘…’).

How to define a Python String?

Syntax: Declaring a String in Python

<Variable_name> = “value”
 
For example: 
S1 = "This is a string"

Python code:

 
# defining strings in Python
string_1 = 'Hello, This is a string'
print(string_1)
string_2 = "This is also a valid string"
print(string_2)
# triple quotes string for using multiple lines
string_3 = '''Hello World,
Shiksha Online welcomes you
to the world of Python'''
print(string_3)
Copy code

Python output:

Hello,  This is a string
Hello,  This is also a valid string
 
Hello World,
Shiksha Online welcomes you
to the world of Python

Read more in Python Data Type Series

Modify Python Strings
Getting started with Python Strings
Getting started with Python Strings
Python strings are immutable, i.e., you cannot modify the contents of the object. In this article, we will briefly discuss how to create, assign string to a variable, how to...read more
Slicing in Python

Python Lists

What is a Python List?

  • A list is an ordered sequence of elements 
  • Declaration: List is declared within the square brackets separated by comma […, …, … ] 
  • Use: Lists are used to store multiple items in a single variable
  • Each element or value inside the list is called an item 
  • Lists are mutable data type, i.e., you can modify (change, add, and remove) items after it’s created
  • Non-Homogenous: Can store values of different data types inside it
  • Ordered: items have a defined order, which does not change. When adding new items to a list, they get placed at the end of the list.
  • Allow Duplicate Values: Lists are indexed, they can have items with the same value

Definition: Just like strings are defined as characters within the quotes. Similarly, python lists are defined as values (items) within the square brackets [ ]. Lists are used to store multiple mutable values within a single variable.

How to define a Python List?

A python list is defined using the square brackets. Let’s see the example below.

Syntax: Declaring a list in Python

<Variable_name> = [item_1, item_2, item_3]
 
For example# a list of top 3 programming languages
prog_l = ['Python', 'Java', 'C']

Python code:

 
# defining list in Python
list_1 = [2,4,6,8.0, "even", True] # list of even numbers in Python
print(list_1)
Copy code

Python output:

2,4,6,8.0, “even”, True

Explore FREE Courses to learn Python

Python Tuples

What is a Python Tuple?

  • A python tuple is a collection of ordered and non-changeable values in parenthesis and separated by a comma (…, …., …)
  • Declaration: Tuple is declared within the parenthesis (…)
  • Tuples belong to the immutable data type
  • Use: Tuples are used to store multiple items in a single variable
  • Non-Homogenous: A tuple variable can store different data types and duplicate values

Definition: Just like lists are defined as values within the square brackets. Similarly, python tuples are defined as values (items) within the parenthesis ( ). Tuples are used to store multiple immutable values in a single variable. 

How to define a Python Tuple?

A python tuple is defined using the parenthesis. Let’s see the example below.

Syntax: Declaring a tuple in Python

<Variable_name> = (item_1, item_2, item_3)
 
For example# a tuple of top 3 programming languages
prog_t = ('Python', 'Java', 'C')

Python code:

 
# defining tuple in Python
tupl_1 = (1,3,5,7.0, "odd", False)
print(tupl_1)
Copy code

Python output:

1,3,5,7.0, "odd", False

Read more in Python Data Type Series

Python Sets Practice Programs For Beginners
Python Sets Practice Programs For Beginners
Python offers a rich collection of sequence object types, and sets are one of them.
Python Lists Practice Programs For Beginners
Python Lists Practice Programs For Beginners
Test your Python Lists knowledge with practice programs! In this blog, we will solve 12 python lists exercises for beginners.
Python Dictionary Practice Programs For Beginners
Python Dictionary Practice Programs For Beginners
Working with Python dictionary types can seem slightly difficult if you are not familiar with its features. The best way to master it is by solving problem statements. It is...read more

Read more: Difference between Python List and Python Tuple

Python Sets

What is a Python Sets?

  • A set is a collection of values that is non-ordered, and non-indexed
  • Declaration: A set is declared within the curly brackets separated by comma {…, …, … } 
  • Use: You can use sets to store multiple items in a single variable
  • Each element or value inside the list is called an item
  • Mutable: Can modify (change, add, and remove) items in a set after its created
  • Non-Homogenous: Can store values of different data types inside it
  • Unique Values: Sets are non-indexed. Sets cannot have two items with the same value.

Definition: Python Sets are defined as values (items) within the curly brackets { }. Sets are used to store multiple mutable and unique values within a single variable.

How to define a Python Set?

A Python set is declared using the curly braces. Let see the syntax below:

Syntax: Declaring a set in Python

Variable_name = {item_1, item_2, item_3}
 
For example# a set of top 3 programming languages
prog_s = {'Python', 'Java', 'C'}

Python code:

 
# defining set in Python
set_1 = {2,4,6,8.0, “even”}
print(set_1)
Copy code
Python output:

2,4,6,8.0, “even”

Python Dictionaries

What is a Python Dictionary?

  • Python dictionary is an unordered collection of items
  • Each item of a dictionary has a unique key/value pair
  • You can use Dictionaries to retrieve values with known key
  • You can define Dictionaries within curly braces as key-value pairs {key: value}
  • Key and value can be of any type

Definition: Python Dictionary is an unordered collection that contains key: value pairs separated by commas inside curly brackets {key: value}.

How to define a Python Dictionary?

A python dictionary is defined using the key-value pair. Let’s see an example below.

Syntax: Declaring a dictionary in Python

Variable_name ={key1: value_1, key2: value_2}

For example:

# a dictionary of top 3 programming languages

prog_d = {‘langugae’: ‘Python’, ‘Java’, ‘C’, ‘level’: ‘beginner’}

Python code:

 
# empty dictionary
dict_1 = {}
# dictionary with integer keys
dict_type1 = {1: 'Adam', 2: 'Jane'}
# dictionary with mixed keys
dict_type2 = {'name': 'Jane', 1: [6, 9, 3]}
# using the keyword dict()
dict_type3 = dict({1:'Adam', 2:'Jane'})
# from sequence having each item as a pair
dict_type4 = dict([(1,'Adam'), (2,'Jane')])
Copy code

If you reached till here, I am assuming you liked the article. Hit those stars below to support us and let us know if you want us to publish more information rich-content like this. 

Objects in Python

Objects are the most fundamental notion in Python programming. In fact, pretty much everything including classes, functions, modules, as well as literals such as integers, strings, etc, are objects in Python. So, what exactly is an object?

Let’s say if you were to move house, you would probably label your moving boxes, such as bundle together knives, spoons, and forks in a box and name it cutlery or flatware. The concept of objects is similar to that. You can think of objects as variable-sized boxes with a unique id, value, and set of associated operations. When you type an instruction like age = 30 in a Python module, you are creating an instance of an integer object with the value 30, and the identity age. So, each object in Python has a type, a value, and an identity.

The identity of an object basically acts as a pointer to the object’s location, and you can retrieve that object by simply accessing it through its name: age. The type of an object, also known as the object’s class, is like a stamp on the box. It tells a user what behaviors they can expect when interacting with that entity.

Python Object Types

You should know that in Python, data takes the form of objects. By that I mean, instead of handling raw data values directly, Python wraps each data value in memory as an object. There are two important things that you should be aware of when working with Python objects. Let us explore what they are.

Dynamic vs Static Typing

Now, there is another way to look at the Python data. Python is a dynamic and strongly typed language. Dynamic means that Python keeps track of data types for you automatically instead of requiring the developers to explicitly add type declaration code. The latter is referred to as statically typing where the type of an object has to be attached to the object before compile time. Python is also strongly typed, which means that you can perform only those operations on an object that are valid for its type.

Note: In Python, if you want to check the type of anything (a variable or a literal value), you can use one of Python’s built-in functions type(). Similarly, if you want to check if the variable that you are using points to an object of specific type, use the built-in function isinstance(object, type).

Example

 
x = 7
y = "Hello World"
print(type(x))
print(type(y))
print(type("hello") == int)
print(type("hello") == str)
print(isinstance(True, bool))
Copy code

Output:

2021_12_Dynamic-vs-Static-Typing.jpg

Built-in Object Types in Python

Python provides simple yet powerful object types as an intrinsic part of the language. These built-in objects make it easy to write programs, they form the core of every Python program., and act as extensions if you need to provide your own objects using Python classes or C language interfaces.

Python provides a variety of built-in data types that can be mainly divided into three categories: Numeric, Sequence, and Mapping. The table below outlines the Python data types that are covered in this article.

Category Name Type Mutable? Examples
Numeric Integer int no 45, 0,
  Floating point float no 3.143, 47e5
  Complex complex no 2.5j, 4+3.6j
  Boolean bool no True, False
Sequence String str no ‘Jim ‘, ”Sam”, ”’a
  Tuples tuple no (2, 3.5, ‘Jim’)
  Lists list yes [(1, 2.5), ‘Sam’, True]
Mapping Dictionary dict yes {‘name’ : ‘Jim’, ‘age’ : 20, ‘stu_id’ : [1, 2, 3]}
  Sets set yes set(‘abc’), {‘a’, ‘b’, ‘c’}
  FrozenSet frozenset no frozenset([‘Tim’, ‘Sam’, ‘Jim’])

Note: This is by no means a complete list of Python data types.

More Articles in Python Data Type

Working with Python Numbers
Understanding Tuples in Python
Understanding Tuples in Python
In this guide, you will learn all about Tuples in Python: creating tuples, accessing tuple items, looping through a tuple, and other tuple operations with the help of examples.
Type Conversion in Python: Types and Examples
Type Conversion in Python: Types and Examples
Type conversion in Python refers to the direct conversion of object of one data type to another data type. In this conversion, Python interpreter automatically performs conversion.
Getting started with Python Strings
Getting started with Python Strings
Python strings are immutable, i.e., you cannot modify the contents of the object. In this article, we will briefly discuss how to create, assign string to a variable, how to...read more
Python Sets Practice Programs For Beginners
Python Sets Practice Programs For Beginners
Python offers a rich collection of sequence object types, and sets are one of them.
Type Casting in Python
Type Casting in Python
Through this article, you will be able to learn about type conversion or type casting in Python. We will be discussing type casting into different data types.
Getting started with Python Strings
Getting started with Python Strings
Python strings are immutable, i.e., you cannot modify the contents of the object. In this article, we will briefly discuss how to create, assign string to a variable, how to...read more
Understanding Python Sets (With Examples)
Understanding Python Sets (With Examples)
In this article, you will discuss about Python Sets: creating sets, accessing, adding, and removing set items, looping through a set, and other set operations with the help of examples.
Introduction to Python Dictionary (With Examples)
Introduction to Python Dictionary (With Examples)
In this tutorial, you will learn all about Dictionary in Python: creating dictionaries, accessing dictionary items, looping through a dictionary, and other dictionary operations with the help of examples.
Find the Second Occurrence of a Substring in Python String
Find the Second Occurrence of a Substring in Python String
In this article, we will discuss different methods (find, split and regular expression module) to find the second occurrence of a substring in a given string.
Python Sets Practice Programs For Beginners
Python Sets Practice Programs For Beginners
Python offers a rich collection of sequence object types, and sets are one of them.
All About Python Lists Methods
All About Python Lists Methods
In this guide, you will learn all about lists in Python: accessing lists, changing list elements, looping through a list, and other list operations with examples.
Top 110+ Python Interview Questions and Answers
Top 110+ Python Interview Questions and Answers
Python is a widely-used general-purpose, object-oriented, high-level programming language. It is used to create web applications, and develop websites and GUI applications. The popularity of the language is due to...read more
Conditional Statements in Python – Python Tutorial
Conditional Statements in Python – Python Tutorial
A conditional statement as the name suggests itself, is used to handle conditions in your program. These statements guide the program while making decisions based on the conditions encountered by...read more
Methods to Check for Prime Numbers in Python
Methods to Check for Prime Numbers in Python
Prime numbers are the building blocks of mathematics, and they have fascinated mathematicians for centuries. Regarding Python programming, there are different methods to check whether the given number is a...read more

FAQs

What is the difference between mutable and immutable data types in Python?

Mutable data types are those in which the value assigned to a variable can be changed such as list, set, and dictionary. Immutable data types are those in which the value assigned to a variable cannot be changed, for example, int, bool, float, and string.

What is a Sequence data type in Python?

A sequence is an ordered collection of data types. These data types can be similar or different. The built-in sequence data types in Python include string, tuple, and list.

What is the best resource to learn about Python data types?

One of the best ways to learn about Python data types is an online course. Online courses can help you understand the foundations of Python and write simple programs in Python using the most common data types.

What are the 5 data types in Python?

5 data types in Python are: - Numbers - Tuple - String - List - Dictionary

What is tuple in Python?

Tuple is a python data type used to store the values within the parenthesis (). For eg: a = (1,2,3) is a tuple. and a = [1,2,4] is a list.

About the Author
author-image
Atul Harsha
Senior Manager Content

Experienced AI and Machine Learning content creator with a passion for using data to solve real-world challenges. I specialize in Python, SQL, NLP, and Data Visualization. My goal is to make data science engaging an... Read Full Bio

Comments