Python Data Types
Data types define what kind of value a variable can hold. Python has built-in data types — numbers, strings, booleans, lists, tuples, sets, and dictionaries. Understanding them is essential for writing correct and efficient Python code.
Overview of Python Data Types
Python has 7 primary built-in data types:
- int — whole numbers (
10,-5) - float — decimal numbers (
3.14,-0.5) - str — text strings (
"hello") - bool — True / False
- list — ordered, mutable collection
- tuple — ordered, immutable collection
- dict — key-value pairs
1. Numeric Types — int & float
age = 22 # int
price = 99.99 # float
big = 1000000 # int
scientific = 1.5e3 # float (1500.0)
print(type(age)) # <class 'int'>
print(type(price)) # <class 'float'>
2. String (str)
Strings are sequences of characters, enclosed in single or double quotes. Strings are immutable.
name = "Ankit"
city = 'Varanasi'
greeting = """Multi-line
string works too"""
print(name.upper()) # ANKIT
print(len(city)) # 8
print(city[0]) # V
3. Boolean (bool)
Boolean has only two values: True and False. Used in conditions.
is_student = True
is_logged_in = False
print(10 > 5) # True
print(5 == 5) # True
4. List — Ordered & Mutable
Lists store multiple items in a single variable. You can change, add, or remove items.
fruits = ["apple", "banana", "cherry"]
fruits.append("mango")
print(fruits) # ['apple', 'banana', 'cherry', 'mango']
numbers = [1, 2, 3, 4, 5]
print(numbers[0]) # 1
print(numbers[-1]) # 5
5. Tuple — Ordered & Immutable
Tuples are like lists but cannot be changed after creation. Faster than lists.
coordinates = (10, 20)
rgb = (255, 0, 0)
# coordinates[0] = 5 # ❌ Error — tuples are immutable
print(coordinates[0]) # 10
6. Dictionary (dict)
Key-value pairs. Perfect for storing structured data. Very common in real-world apps.
student = {
"name": "Ankit",
"age": 22,
"course": "Python"
}
print(student["name"]) # Ankit
print(student.get("age")) # 22
student["city"] = "Varanasi"
print(student)
Checking Type of Any Variable
x = 10
print(type(x)) # <class 'int'>
y = [1, 2, 3]
print(isinstance(y, list)) # True
Use type() to check a variable's type, and isinstance() to check if a variable is an instance of a given type — the latter is preferred in production code.
Create a dictionary called profile with keys name, age, skills (a list of 3 skills). Print each value on a separate line. Then add a new key city = "Varanasi".
Quick Revision
intandfloatfor numbers.stris immutable — cannot be changed in place.boolonly holds True / False.listis mutable, ordered, allows duplicates.tupleis immutable, ordered.dictstores key-value pairs, keys must be unique.- Use
type()to inspect any variable.
Interview Questions
Q: What's the difference between a list and a tuple?
A: Lists are mutable (can be changed) and use square brackets []. Tuples are immutable (cannot be changed after creation) and use parentheses (). Tuples are faster and used for fixed data.
Q: Is Python's string mutable or immutable?
A: Strings are immutable. Every operation on a string returns a new string. For example, name.upper() returns a new string — the original name is unchanged.
Q: What are the 4 immutable data types in Python?
A: Numbers (int, float, complex), strings, tuples, and frozensets are immutable. Lists, dictionaries, and sets are mutable.
Want to Master Python?
Join our Python course in Varanasi — practical classes and career support at our Sarnath centre.