Python by Dr.
Sadaf Zeeshan
Lecture 3
Common Concepts in Python: Variables in Python
A variable stores a value that can change during program execution.
Example: Assigning and Using Variables
name = "Alice" # String (text)
age = 25 # Integer (whole number)
height = 5.6 # Float (decimal number)
is_student = True # Boolean (True/False)
print("Name:", name)
print("Age:", age)
print("Height:", height, "feet")
print("Student:", is_student)
Common Variable Types in Python
Type Example Description
String (str) "Hello" Text data
Integer (int) 42 Whole numbers
Float (float) 3.14 Decimal numbers
Boolean (bool) True / False Logical values
List (list) [1, 2, 3] Collection of items
Tuple (tuple) (4, 5, 6) Immutable (unchangeable) collection
Dictionary (dict) {"name": "Alice", "age": 25} Key-value pairs
Python by Dr. Sadaf Zeeshan
Types of Variables in Python (with Examples)
A variable is used to store data in Python. Different types of variables hold different types of
data. Below are the most common types:
1. String (str)
A string is a sequence of characters, enclosed in quotes (" " or ' ').
Example:
name = "Alice"
greeting = 'Hello, how are you?'
print(name) # Output: Alice
print(greeting) # Output: Hello, how are you?
Strings can contain letters, numbers, and special characters.
We can perform operations like concatenation (+) and repetition (*).
full_name = name + " Johnson" # Concatenation
print(full_name) # Output: Alice Johnson
repeat_text = "Python " * 3
print(repeat_text) # Output: Python Python Python
2. Integer (int)
An integer is a whole number, positive or negative, without decimals.
Example:
age = 25
temperature = -10
count = 1000
Python by Dr. Sadaf Zeeshan
print(age, temperature, count) # Output: 25 -10 1000
Integers are used for counting and indexing.
We can perform arithmetic operations like addition, subtraction, multiplication, etc.
x = 10
y=3
print(x + y) # Output: 13
print(x * y) # Output: 30
print(x // y) # Output: 3 (integer division)
print(x % y) # Output: 1 (remainder)
3. Float (float)
A float is a number with a decimal point.
Example:
height = 5.8
price = 99.99
pi = 3.14159
print(height, price, pi) # Output: 5.8 99.99 3.14159
Used for measurements and precise calculations.
Supports arithmetic operations similar to integers.
a = 10.5
b = 2.3
print(a + b) # Output: 12.8
print(a * b) # Output: 24.15
4. Boolean (bool)
A boolean holds True or False values.
Python by Dr. Sadaf Zeeshan
Example:
is_raining = True
is_summer = False
print(is_raining) # Output: True
print(is_summer) # Output: False
Used in conditions and decision-making.
x = 10
y = 20
print(x > y) # Output: False
print(x < y) # Output: True
print(x == y) # Output: False
5. List (list)
A list is an ordered, changeable collection of items. It can contain different data types.
Example:
fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4, 5]
mixed = [10, "hello", 3.5, True]
print(fruits) # Output: ['apple', 'banana', 'cherry']
print(mixed) # Output: [10, 'hello', 3.5, True]
Lists are mutable (changeable).
Supports indexing and slicing.
print(fruits[0]) # Output: apple
fruits.append("orange") # Adds an item
print(fruits) # Output: ['apple', 'banana', 'cherry', 'orange']
Python by Dr. Sadaf Zeeshan
List Methods
Method Description Example
append(x) Adds item x to end lst.append(4)
insert(i, x) Inserts x at index i lst.insert(2, "hello")
remove(x) Removes first occurrence of x lst.remove(3)
pop(i) Removes item at index i (default last) lst.pop(2)
clear() Removes all elements lst.clear()
sort() Sorts list in ascending order lst.sort()
reverse() Reverses the list order lst.reverse()
count(x) Counts occurrences of x lst.count(5)
index(x) Returns index of x lst.index(7)
6. Tuple (tuple)
A tuple is like a list but immutable (cannot be changed after creation). Tuples are faster and
use less memory than lists.
Example:
coordinates = (10, 20)
colors = ("red", "green", "blue")
print(coordinates) # Output: (10, 20)
print(colors[1]) # Output: green
Tuples use () instead of [].
Faster and safer than lists.
Python by Dr. Sadaf Zeeshan
# Attempting to change a tuple will cause an error
# colors[0] = "yellow" # TypeError: 'tuple' object does not support item assignment
List vs. Tuple – Key Differences
Feature List (list) Tuple (tuple)
Syntax [item1, item2] (item1, item2)
Mutable? Yes (modifiable) No (fixed)
Ordered? Yes Yes
Performance Slower Faster
Duplicates Allowed? Yes Yes
Memory Usage Higher (due to flexibility) Lower (due to immutability)
Best Used For Collections that change frequently Fixed data sets
Real-World Example
Storing Student Records
student1 = ("Alice", 20, "A") # Tuple (fixed)
students_list = [("Alice", 20, "A"), ("Bob", 21, "B")] # List of tuples
Tuple → Used for a single student (data doesn’t change).
List → Used for multiple students (data can be updated).
Python by Dr. Sadaf Zeeshan
7. Dictionary (dict)
A dictionary stores data in key-value pairs.
A dictionary in Python is a data structure that stores information in key-value pairs. Unlike
lists and tuples, where data is stored in an ordered sequence, dictionaries allow quick access
to values based on unique keys.
A dictionary is created using curly braces {} or the dict() function.
Dictionaries are one of the most powerful and flexible data structures in Python. They allow
fast lookups, efficient storage, and easy data organization, making them essential for real-
world applications like:
Storing user profiles
Managing databases
Configuring settings in applications
Creating machine learning models with structured data
Example:
student = {"name": "Alice", "age": 20, "grade": "A"}
car = {"brand": "Toyota", "year": 2022, "color": "red"}
print(student["name"]) # Output: Alice
print(car["brand"]) # Output: Toyota
Uses {key: value} format.
Keys must be unique and immutable (strings, numbers, or tuples).
student["age"] = 21 # Updating a value
print(student) # Output: {'name': 'Alice', 'age': 21, 'grade': 'A'}
student["city"] = "New York" # Adding a new key-value pair
print(student) # Output: {'name': 'Alice', 'age': 21, 'grade': 'A', 'city': 'New York'}
Python by Dr. Sadaf Zeeshan
Summary Table
Variable Type Description Example
String (str) Text data "Hello", 'Python'
Integer (int) Whole numbers 10, -5, 1000
Float (float) Decimal numbers 3.14, 99.99, -0.5
Boolean (bool) True/False values True, False
List (list) Ordered, changeable collection ["apple", "banana"], [1, 2, 3]
Tuple (tuple) Ordered, unchangeable collection ("red", "blue"), (10, 20)
Dictionary (dict) Key-value pairs {"name": "Alice", "age": 20}
By understanding these variables, you can store and manipulate different types of data in
Python effectively! 🚀