


Khusboo Tayal
Data types are the core building blocks of any programming language. In Python, data types determine the kind of value a variable can hold. Python comes with a variety of built-in data types to handle different kinds of data such as numbers, text, and collections.
Understanding data types is essential for writing efficient and bug-free code. In this tutorial, you'll learn about the most commonly used data types in Python and how to work with them.
In simple terms, a data type is a classification that tells Python what kind of value a variable is storing. This helps Python understand how to process that value during execution.
Python is dynamically typed, which means you don't need to declare the data type of a variable explicitly—Python infers it automatically.
Python data types are divided into the following main categories:
We'll focus on the most commonly used types for beginners.
Used to store whole numbers.
x = 10 print(type(x)) # <class 'int'>
Used to store decimal numbers.
price = 19.99 print(type(price)) # <class 'float'>
Used in advanced mathematics, contains real and imaginary parts.
z = 2 + 3j print(type(z)) # <class 'complex'>
Used to store text data. Strings are enclosed in single (') or double (") quotes.
name = "Alice" print(type(name)) # <class 'str'>
You can perform many operations with strings, like concatenation, slicing, and formatting.
Stores one of two values: True or False. Often used in conditions and comparisons.
is_active = True print(type(is_active)) # <class 'bool'>
An ordered, mutable collection of items.
fruits = ["apple", "banana", "cherry"] print(type(fruits)) # <class 'list'>
An ordered, immutable collection.
coordinates = (10.0, 20.0) print(type(coordinates)) # <class 'tuple'>
Represents a sequence of numbers, commonly used in loops.
r = range(5) print(type(r)) # <class 'range'>
An unordered, mutable, and unique collection.
colors = {"red", "green", "blue"}
print(type(colors)) # <class 'set'>An immutable version of a set.
fs = frozenset(["apple", "banana", "cherry"]) print(type(fs)) # <class 'frozenset'>
A collection of key-value pairs. Mutable and unordered in nature.
person = {"name": "John", "age": 30}
print(type(person)) # <class 'dict'>You can access values by their keys:
print(person["name"]) # Output: John
Represents the absence of a value.
result = None print(type(result)) # <class 'NoneType'>
Often used as a placeholder or to indicate 'no result'.
You can convert one data type to another using built-in functions like int(), float(), str(), and bool().
x = 5.7 y = int(x) print(y) # Output: 5 num = 10 text = str(num) print(text) # Output: '10'
Use the type() function:
x = "hello" print(type(x)) # <class 'str'>
| Data Type | Example | Description |
|---|---|---|
| int | x = 5 | Integer (whole number) |
| float | x = 3.14 | Decimal number |
| str | x = "hello" | String (text) |
| bool | x = True | Boolean value (True/False) |
| list | x = [1, 2, 3] | Mutable ordered collection |
| tuple | x = (1, 2, 3) | Immutable ordered collection |
| dict | x = {"a": 1, "b": 2} | Key-value mapping |
| set | x = {1, 2, 3} | Unique, unordered collection |
| NoneType | x = None | Represents no value |
Data types are fundamental in Python. They define what kind of data a variable holds and what operations can be performed on it. As a beginner, mastering Python's core data types—like integers, strings, lists, and dictionaries—sets the foundation for more advanced concepts.
By understanding and using the correct data type for each scenario, you’ll write more efficient, readable, and error-free code.
Q1: Can I change a variable's data type after assigning it?
Yes, Python allows dynamic typing, so variables can change types.
Q2: What’s the difference between a list and a tuple?
A list is mutable (can be changed), whereas a tuple is immutable (cannot be changed).
Q3: What does None mean in Python?
None represents the absence of a value or a null value in Python.
Subscribe to our newsletter to get the latest Python tutorials, project ideas, and updates right to your inbox.
#python #pythontutorial #learnpython #pythonforbeginners #pythonvariables #pythondatatypes #codingbasics #techlearning #datascienceprep #pythonprogramming