Python Data Types
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.
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
- Different data types in Python
- Mutable vs immutable data type in Python?
- Python Numbers
- Python Strings
- Python Lists
- Python Tuples
- Python Sets
- Python Dictionaries
- 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.
Best-suited Python courses for you
Learn Python with these high-rated online courses
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 1no need to specify the data type of num_1Python automatically decides the data type as per the value stored in num_1'''num_1 = 10print(num_1)print(type(num_1))
#Defining numbers in Python - Type 2 num_1, num_2 = 20, 30.0print(num_1,num_2)
Python output:
10 <class 'int'> 20 30.0
Read more in Python Data Type Series
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 Pythonstring_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 linesstring_3 = '''Hello World,Shiksha Online welcomes youto the world of Python'''print(string_3)
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
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 Pythonlist_1 = [2,4,6,8.0, "even", True] # list of even numbers in Pythonprint(list_1)
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 Pythontupl_1 = (1,3,5,7.0, "odd", False)print(tupl_1)
Python output:
1,3,5,7.0, "odd", False
Read more in Python Data Type Series
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 Pythonset_1 = {2,4,6,8.0, “even”}print(set_1)
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 dictionarydict_1 = {}
# dictionary with integer keysdict_type1 = {1: 'Adam', 2: 'Jane'}
# dictionary with mixed keysdict_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 pairdict_type4 = dict([(1,'Adam'), (2,'Jane')])
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))
Output:
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
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.
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