Python Data Types
Introduction to Python Data Types
In Python, every value has a specific data type. A data type defines the kind of data a variable can store and the operations that can be performed on that data.
Python provides several built-in data types to work with numbers, text, Boolean values, and collections of data.
Understanding data types is essential for writing Python programs because it helps programmers store, manage, and process different types of information efficiently.
In this tutorial, you will learn about Python’s built-in data types, including Integer, Float, Complex, Boolean, String, List, Tuple, Set, and Dictionary, with practical examples.
1. What Are Data Types in Python?
A data type specifies the category of a value stored in a variable.
For example:
a = 10
b = 10.5
c = "Python"
d = True
print(type(a))
print(type(b))
print(type(c))
print(type(d))
Output:
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
Python provides the built-in type() function to identify the data type of a value or variable.
2. Numeric Data Types in Python
Python provides three main numeric data types:
- Integer (
int) - Float (
float) - Complex (
complex)
2.1 Integer (int)
An integer is a whole number without a decimal point. It can be positive, negative, or zero.
Examples:
a = 10
b = -20
c = 0
print(a)
print(b)
print(c)
print(type(a))
Output:
10
-20
0
<class 'int'>
Integers are commonly used for counting, indexing, and mathematical calculations.
2.2 Float (float)
A float is a numeric data type used to represent numbers containing decimal points.
Examples:
a = 10.5
b = -25.75
c = 3.14
print(a)
print(b)
print(c)
print(type(a))
Output:
10.5
-25.75
3.14
<class 'float'>
Float values are commonly used in calculations involving measurements, prices, percentages, and scientific computations.
2.3 Complex (complex)
A complex number consists of two parts:
- Real part
- Imaginary part
Python uses j to represent the imaginary component of a complex number.
Example:
a = 5 + 6j
print(a)
print(type(a))
Output:
(5+6j)
<class 'complex'>
You can access the real and imaginary parts using the real and imag attributes.
a = 5 + 6j
print(a.real)
print(a.imag)
Output:
5.0
6.0
3. Boolean Data Type (bool)
The Boolean data type represents one of two possible values:
TrueFalse
Boolean values are commonly used in conditional statements and logical operations.
Example:
a = True
b = False
print(a)
print(b)
print(type(a))
Output:
True
False
<class 'bool'>
Boolean values are useful when checking conditions.
Example:
age = 20
is_adult = age >= 18
print(is_adult)
Output:
True
4. String Data Type (str)
A string is a sequence of characters used to represent text.
Strings can be created using:
- Single quotation marks (
' ') - Double quotation marks (
" ") - Triple quotation marks (
''' '''or""" """)
Example:
name = "Python"
message = 'Hello World'
print(name)
print(message)
print(type(name))
Output:
Python
Hello World
<class 'str'>
Multiline Strings
Triple quotation marks allow you to create multiline strings.
Example:
text = """Welcome to Python.
Python is easy to learn.
Start learning today."""
print(text)
Output:
Welcome to Python.
Python is easy to learn.
Start learning today.
5. Collection Data Types in Python
Python provides four important built-in collection data types:
- List
- Tuple
- Set
- Dictionary
These data types allow you to store multiple values in a single variable.
5.1 List (list)
A list is an ordered and mutable collection of elements.
Mutable means that elements can be added, removed, or modified after creating the list.
Lists are created using square brackets [].
Example:
numbers = [10, 20, 30, 40, 50]
print(numbers)
print(type(numbers))
Output:
[10, 20, 30, 40, 50]
<class 'list'>
Accessing List Elements
List elements can be accessed using their index positions. Indexing starts from zero.
numbers = [10, 20, 30, 40, 50]
print(numbers[0])
print(numbers[2])
print(numbers[-1])
Output:
10
30
50
Modifying List Elements
Lists are mutable, so their elements can be changed.
numbers = [10, 20, 30]
numbers[1] = 100
print(numbers)
Output:
[10, 100, 30]
5.2 Tuple (tuple)
A tuple is an ordered and immutable collection of elements.
Immutable means that elements cannot be modified after the tuple is created.
Tuples are created using parentheses ().
Example:
numbers = (10, 20, 30, 40, 50)
print(numbers)
print(type(numbers))
Output:
(10, 20, 30, 40, 50)
<class 'tuple'>
Accessing Tuple Elements
numbers = (10, 20, 30, 40)
print(numbers[0])
print(numbers[2])
Output:
10
30
Important Difference
Unlike lists, tuples do not allow item assignment.
numbers = (10, 20, 30)
numbers[1] = 100
This code raises a TypeError because tuples are immutable.
5.3 Set (set)
A set is an unordered collection of unique elements.
Sets automatically remove duplicate values.
Sets are created using curly brackets {}.
Example:
numbers = {10, 20, 30, 40}
print(numbers)
print(type(numbers))
Output:
{10, 20, 30, 40}
<class 'set'>
Duplicate Elements in Sets
numbers = {10, 20, 20, 30, 30, 40}
print(numbers)
Output:
{10, 20, 30, 40}
Notice that duplicate values are automatically removed.
Important: Sets do not support indexing because they are unordered collections.
An empty set must be created using set().
a = set()
print(type(a))
Output:
<class 'set'>
Using {} creates an empty dictionary, not an empty set.
5.4 Dictionary (dict)
A dictionary stores data in key-value pairs.
Dictionaries are created using curly brackets {} with keys and corresponding values.
Example:
student = {
"name": "Rahul",
"age": 22,
"course": "Python"
}
print(student)
print(type(student))
Output:
{'name': 'Rahul', 'age': 22, 'course': 'Python'}
<class 'dict'>
Accessing Dictionary Values
You can access dictionary values using their keys.
student = {
"name": "Rahul",
"age": 22,
"course": "Python"
}
print(student["name"])
print(student["age"])
Output:
Rahul
22
Modifying Dictionary Values
Dictionaries are mutable, so their values can be modified.
student = {
"name": "Rahul",
"age": 22
}
student["age"] = 23
print(student)
Output:
{'name': 'Rahul', 'age': 23}
6. Checking Data Types Using type()
Python provides the built-in type() function to identify the data type of a value or variable.
Syntax:
type(value)
Example:
a = 100
b = 10.5
c = 5 + 6j
d = True
e = "Python"
f = [1, 2, 3]
g = (1, 2, 3)
h = {1, 2, 3}
i = {"name": "Rahul"}
print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(e))
print(type(f))
print(type(g))
print(type(h))
print(type(i))
Output:
<class 'int'>
<class 'float'>
<class 'complex'>
<class 'bool'>
<class 'str'>
<class 'list'>
<class 'tuple'>
<class 'set'>
<class 'dict'>
7. Difference Between Python Data Types
The following table summarizes the major built-in Python data types.
| Data Type | Description | Example |
|---|---|---|
| int | Whole numbers | 10 |
| float | Decimal numbers | 10.5 |
| complex | Complex numbers | 5+6j |
| bool | True or False | True |
| str | Text values | "Python" |
| list | Ordered, mutable collection | [1, 2, 3] |
| tuple | Ordered, immutable collection | (1, 2, 3) |
| set | Collection of unique elements | {1, 2, 3} |
| dict | Key-value pairs | {"a": 10} |
8. Practice Questions on Python Data Types
Test your understanding with these multiple-choice questions.
Question 1: Which function is used to check the data type of a variable in Python?
A. print()
B. type()
C. input()
D. check()
Answer: B. type()
Question 2: Which of the following is an immutable data type?
A. List
B. Dictionary
C. Tuple
D. Set
Answer: C. Tuple
Question 3: Which data type stores key-value pairs?
A. List
B. Tuple
C. Set
D. Dictionary
Answer: D. Dictionary
Question 4: What is the output of the following code?
numbers = {10, 20, 20, 30}
print(numbers)
A. {10, 20, 20, 30}
B. {10, 20, 30}
C. [10, 20, 30]
D. (10, 20, 30)
Answer: B. {10, 20, 30}
Question 5: Which symbol is used to create a Python list?
A. ()
B. {}
C. []
D. <>
Answer: C. []
Question 6: What is the data type of 10.5?
A. Integer
B. Float
C. String
D. Boolean
Answer: B. Float