Python Data Types
Theory, Examples, Applications &
Code Snippets
Categories of Data Types
• Python data types are used to store different
kinds of data.
– Basic (Primitive) Types: int, float, bool, str
– Collection Types: list, tuple, dict, set
– Special Type: NoneType (None)
Integer (int)
• Theory: Represents whole numbers without
decimal part.
– Example values: 0, 7, -15, 2025
– Applications: Counting items, loops, indexing, age,
quantity, etc.
– Code:
num = 10
print(num)
print(type(num)) # <class 'int'>
Float (float)
• Theory: Represents real numbers with decimal
points.
– Example values: 3.14, -0.5, 100.0
– Applications: Measurements, calculations with
precision (marks %, salary, speed).
– Code:
pi = 3.14
marks_percentage = 86.5
print(pi, marks_percentage)
Boolean (bool)
• Theory: Stores True or False values.
– Example values: True, False
– Applications: Conditions, decision-making, flags
(is_logged_in, is_adult).
– Code:
age = 18
is_adult = age >= 18
print(is_adult) # True
String (str)
• Theory: Sequence of characters; used to store
text. Strings are immutable.
– Example values: 'Hello', "Python", '123'
– Applications: Names, messages, file paths, user
input, printing output.
– Code:
name = "Saba"
message = "Hello, " + name
print(message)
print(len(name))
List
• Theory: Ordered, mutable collection that can
store mixed data types.
– Example values: [1, 2, 3], ['apple', 'banana'], [1,
'Saba', 3.5]
– Applications: Storing group of related items
(student marks, to-do list, products).
– Code:
marks = [85, 90, 78]
[Link](95)
print(marks)
print(marks[0]) # first element
List – More Operations
• Lists support slicing, updating, and iteration.
– Useful when data frequently changes
(add/remove/update items).
– Code:
fruits = ['apple', 'banana', 'mango']
fruits[1] = 'orange'
for f in fruits:
print(f)
Tuple
• Theory: Ordered, immutable collection; once
created, it cannot be changed.
– Example values: (1, 2, 3), ('red', 'green'), (10,
'Saba', 90.5)
– Applications: Fixed data like coordinates,
configuration values, days of week.
– Code:
point = (10, 20)
print(point[0]) # 10
# point[0] = 15 # Error: tuples are immutable
Dictionary
• Theory: Stores data as key–value pairs. Keys
must be unique and immutable.
– Example value: {'name': 'Saba', 'age': 22, 'city':
'Bareilly'}
– Applications: Records, JSON-like data,
configurations, user profiles.
– Code:
student = {'name': 'Saba', 'age': 22}
print(student['name'])
student['age'] = 23
print(student)
Dictionary – Operations
• Theory: Very fast lookup by key; can add or
remove key–value pairs.
– Applications: Storing data where each item has a
unique identifier (id, roll no).
– Code:
student = {'name': 'Saba', 'age': 22}
for key, value in [Link]():
print(key, '->', value)
Set
• Theory: Unordered collection of unique
elements; duplicates are removed
automatically.
– Example value: {1, 2, 3}, {'a', 'b', 'c'}
– Applications: Removing duplicates, membership
tests, mathematical set operations.
– Code:
numbers = {1, 2, 2, 3}
print(numbers) # {1, 2, 3}
print(2 in numbers) # True
Set – Operations
• Common operations: union, intersection,
difference.
– Useful in data analysis to find common or distinct
elements.
– Code:
a = {1, 2, 3}
b = {3, 4}
print([Link](b)) # {1, 2, 3, 4}
print([Link](b)) # {3}
NoneType (None)
• Theory: Represents 'no value' or 'nothing'.
– Example: result = None (means result is currently
empty).
– Applications: Default values in functions, resetting
variables, checking if something is set or not.
– Code:
result = None
if result is None:
print("No result yet")
Type Conversion (Casting)
• Theory: Converting one data type into another
using built-in functions.
– Common functions: int(), float(), str(), list(),
tuple(), set().
– Applications: Converting user input (string) into
int/float for calculations.
– Code:
user_input = '25'
age = int(user_input)
print(age, type(age))
Summary
• Each data type has a specific purpose and
behavior.
– Choose data type based on: mutability, order,
uniqueness, and structure of your data.
– Practice by creating small programs using each
data type.