Python Data Types

Tutorial Playlist

Understanding the data types in Python is essential for writing efficient programs. Data types determine the type of value a variable can store, including a number, text, or lists. They help organize and process data effectively. They also ensure that the operations are performed correctly. In this article, we’ll explore various Python Data Types with Examples of how to use them effectively.

Table of Contents:

What are Python Data Types?

In Python, data types define a variable’s value, including numbers, strings, or sets. The most commonly used data types are integers(int), floating point numbers(float), string(str), list(list), and dictionary(dict). Python is known for its simplicity and powerful functionality. One of the important features of data type is dynamic typing, which allows variables to change their data type as needed. This flexibility makes Python highly versatile and easy to work with.

Data Type Class Value
Numeric Int, float, complex Numeric value
String str Sequence of characters
Sequence List, tuple, range Collection of items
Mapping dict Data in key-value pair form
Set Set, frozen set Unordered, unique collection
Boolean bool Boolean value “True” or “False”

Numeric Data Types in Python

Numeric data types in Python can hold numeric values such as integers, decimal numbers(floating numbers), and complex numbers.

  • Integer: Integers are represented by the int class. It can contain positive and negative values. The size of an integer is not limited by a fixed maximum it can grow as long as memory permits
  • Floating point: Floating point variables are represented by the float class. It can only hold decimal values.
  • Complex Numbers: Complex numbers are represented by complex classes.
    Data Type Class Example
    Integer int x = 10
    Floating Point float y = 10.5
    Complex Number complex z = 5 + 6j

Example:

Python

Output:

Sequence Data Types in Python

Sequence data types in Python are capable of representing ordered collections of items. They are iterable, support indexing and slicing, and provide multiple manipulation methods.  These are the following sequence data types in Python:

  • String
  • List
  • Tuple
Data Type Class Example
String str str_example = “Hello, World!”
List list list_example = [1, 2, 3, “Python”]
Tuple tuple tuple_example = (10, 20, “AI”)
Unlock Python’s Power!
Enroll today and begin your journey to becoming a Python pro!
quiz-icon

1. Python String Data Type

Python strings are a combination of letters or characters enclosed in single quotes or double quotes. In Python, there is no character data type. Here, character is also included in the string class. A string of length 1 can be called a character string. We can access individual characters with the help of an index. The str class represents it.

  • Creation of String in Python:
    In Python, a string can be created by placing characters or letters within either single quotes (‘ ‘) or double quotes (” “).

Example:

Python

Output:

  • Accessing Strings in Python

Individual characters within a string can be accessed using an index. Python uses zero-based indexing, meaning the first character is at index 0. Negative indexing can also be used to access the characters from the end of the string.

Example:

Python

Output:

2. Python List Data Types

Lists in Python are created using a square bracket [ ]. They can hold various data types, such as numbers, text, or even other lists. Each entry within a list is separated by a comma. Lists allow you to combine multiple items into one collection and give easy access and modification as required.

  • Creating a List in Python

Python

Output:

  • Access List Items in Python

We can access the list of items with the help of the index. In Python, negative indices are used for reverse traversal, like: a[-1] represents the first element from the end, a[-2] represents the second element from the end.

Example:

Python

Output:

3. Python Tuple Data Type

Tuple data types are also the same as list data types in Python. The only difference between them is that tuples are immutable, which means we can not change the tuple elements after they are created. Tuples are defined using parentheses.

  • Creating a Tuple in Python

In Python, tuples are created using parentheses with different types of values separated by commas.

Example:

Python

Output:

  • Access Tuple Items in Python

We can access tuple elements with the help of an index. With the help of the index and subscript operators, we can access the tuple elements easily.

Start Your Python Adventure!
Join now and start coding your future with Python!
quiz-icon

Example:

Python

Output:

4. Python Range Data Type

In Python, range() is a sequence data type that gives an output for any series of numbers within a specified range. The range() becomes more efficient with the help of a loop.

Example:

Python

Output:

Python Set Data Type

A set in Python is an unsorted (unordered) collection of elements that contains unique elements. They are unordered and do not allow duplicates. Sets are defined using curly brackets.

  • Creating a Set in Python

A set is created in Python with the set() function and an iterable object or list of objects separated by commas. Sets can also contain different types of data.

Example:

Python

Output:

  • Access Set Items

Set Items cannot be accessed with an index, we have to use a loop to iterate through the set items.

Example:

Python

Output:

Python Dictionary Data Type

The dictionary in Python uses data types to store elements in key-value pairs like maps. An unordered collection of data values is stored in the dictionary in the form of key-value pairs. In the key-value pair format, the key values are separated from each other by a colon.

  • Create a Dictionary in Python

In Python, a dictionary can be built using the built-in function dict(). Any object can be used to take the values provided in the dictionary. Duplicates are allowed in a dictionary, but the keys must be unique. The keys of the dictionary are case-sensitive.

Example:

Python

Output:

  • Access Dictionary Values Using Keys in Python

The values in the dictionary can be accessed using their keys. The built-in function get() can be used to access the values.

Example:

Python

Output:

Boolean Data Type in Python

Python also provides one built-in data type, Boolean, that gives us two values, True and False. It can take the value of either True or False and is implemented by the class bool. The true expressions will be returned as True by Python, while expressions that are not true will be returned as False.

Example:

Python

Output:

None Data Type in Python

None is a data type that represents null, which is commonly used to indicate that a variable does not contain a value.

Example:

Python

Complex Data Types in Python

Complex data types can be defined as a combination of integers, strings, lists, and dictionaries joined together.

Python allows you to build more structured data using nested data structures such as a list inside a dictionary or a dictionary inside another dictionary. These complex types help store and manage information efficiently in Python.

Example: Storing Information in a Nested Dictionary

Python

Output:

Mutable vs Immutable Data Types in Python

Feature Mutable Data Type Immutable Data Type
Definition Can be changed even after the creation Cannot be changed once created
Data Types Lists, Dictionaries, Sets Strings, Tuples, Numbers
Memory Usage Can change memory location if resized Stays in the same memory location even after resizing
Modification Supports add, remove, and update Need to create a new object to make the changes
Performance Slightly slower due to modification Faster as data remains constant
Methods Available Methods like append(), remove() modify the data Methods like replace() return a new object
Example intellipaat_list = [1, 2, 3]
intellipaat_list.append(4)
print(intellipaat_list)
# Output: [1, 2, 3, 4]
intellipaat_tuple = (1, 2, 3)
intellipaat_tuple[0] = 5
# Output: TypeError: ‘tuple’ object does not support item assignment

Using isinstance() to Check Data Types in Python

The isinstance() function verifies a specific type of variable. Types in Python int, float, str, list, and tuple, are supported. Sometimes it is necessary to check what type the variable it is before you go further in your Python program. The isinstance() method checks whether an object is an instance of a specified class or type, helping the programmer avoid errors in the code.

Example: Checking Data Types with isinstance()

Python

Output:

Type Conversion in Python

Type conversion in Python is used to change a variable from one data type into another.

If you want to do some operations on specific types of data, you may need to convert data from one type to another to perform specific operations. Python makes type conversion simple and easy to accomplish by providing built-in functions like int(), float(), str(), list(), tuple(), and much more. Type conversion makes data types accessible to each other in your Python program.

Example: Converting Different Data Types

Python

Output:

Why Check Data Types in Python?

  • Ensures Correct Operations: Certain operations require a specific type of data to be used.
  • Prevents Errors: Knowing the data type helps avoid type mismatch errors during the execution of the program.
  • Debugging: Checking the data type makes it easier to trace errors when your program gives unexpected output.
  • Improves Code Readability: Knowing the type of data gives an idea about the purpose of the variable.
  • Optimizes Performance: A proper check of the data type makes it possible for the program to execute with ease and efficiency, without producing unnecessary errors.

Python Data Type Exercise Questions

Here are some of the practice questions on Data Types in Python that you should prefer for revising the concept learned in this article.

1. Implement Basic Dictionary Operations in Python

Python

Output:

2. Implement Basic List Operations in Python

Python

Output:

3. Implement Basic Set Operations in Python

Python

Output:

4. Implement Basic Tuple Operations in Python

Python

Output:

5. Implement Basic String Operations in Python

Python

Output:

Conclusion

Python offers a wide range of data types such as int, float, string, list, and tuple. Each serves a unique purpose in your code. Understanding Python data types and dynamic typing is key to writing efficient, clean, and optimized programs. With its simple syntax and flexible features, Python is ideal for both beginners and experienced programmers.
Explore more Python programming tutorials to boost your coding skills and master data structures. Enroll Today! with Intellipaat’s Python Programming Course.  Also, prepare for job interviews with our Python developer interview questions, prepared by industry experts.

Our Python Courses Duration and Fees

Program Name
Start Date
Fees
Cohort Starts on: 15th Apr 2025
₹20,007

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.