Python Variables and Data Types in Lecture 2 of the Python for Data Science course. Beginner-friendly explanation, real examples, quiz and practice exercises.
What You Will Learn
After this lecture, you will be able to:
- Understand variables & how values are stored in Python
- Use different data types (numbers, text, True/False)
- Perform basic mathematical operations
- Write simple Data Science calculations
- Apply concepts using a mini quiz + coding practice
Lecture 1 – Python Installation & Jupyter Setup
What Are Variables?
A variable is a name used to store data in memory.
Think of a variable like a box where you keep information.
Real-life Example
- You store your age
- You store your name
- Your computer stores your Wi-Fi password
Similarly, Python stores information in named boxes.
Python Example
name = "Tahir"
age = 20
Here:
name→ stores text (“Tahir”)age→ stores a number (20)
Rules for Naming Variables
Must start with a letter or underscore
Cannot start with a number
No spaces allowed (use underscore _)
Case-sensitive (Age ≠ age)
Wrong vs Correct
| Wrong | Correct |
|---|---|
| 1name | name1 |
| my name | my_name |
| NAME = “Tahir” (can but not recommended) | name = “Tahir” |
Data Types in Python
Python automatically detects types. You don’t have to declare them.
Common Data Types
| Type | Example | Use |
|---|---|---|
| int (integer) | 20 | Whole numbers |
| float | 20.5 | Decimal values |
| str (string) | “Data” | Text |
| bool (boolean) | True / False | Yes/No values |
Example in Python
name = "Sarah"
age = 21
gpa = 3.65
is_student = True
Checking Data Type (IMPORTANT!)
Use type() function.
a = 10
print(type(a))
Output:
<class 'int'>
Operators (Basic Calculations)
Operators help perform mathematical tasks.
| Operator | Use | Example |
|---|---|---|
+ | Addition | 5 + 3 |
- | Subtraction | 10 – 2 |
* | Multiplication | 4 * 2 |
/ | Division | 8 / 2 |
// | Floor Division (No decimal) | 9 // 2 |
% | Modulus (Remainder) | 10 % 3 |
** | Power | 2 ** 3 |
Example (Power, Division & Modulus)
x = 10
print(x % 3) # remainder 1
print(x // 3) # 3
print(2 ** 4) # 16
Mini Real-World Example (Data Science Style)
Let’s calculate the average score of a student:
score1 = 85
score2 = 90
score3 = 80
average = (score1 + score2 + score3) / 3
print("Average Score:", average)
Quiz – Test Your Understanding
Instruction: First read the question and choose your answer. Then scroll down to see the correct answer
A variable is used to:
a) Print data
b) Store data
c) Delete data
d) Translate data
Answer: b) Store data
Which of the following is a valid variable name?
a) 2age
b) my age
c) student_name
d) class-1
Answer: c) student_name
What type is 3.14 in Python?
a) int
b) float
c) str
d) bool
Answer: b) float
Which operator gives the remainder?
a) //
b) %
c) **
d) /
Answer: b) %
Practice Questions
Instruction for students: Read the question, open your Jupyter Notebook, and try to solve it yourself. After that, come back and check the answer.
city = "Lahore"
print(city)
p1 = 120
p2 = 350
p3 = 200
total = p1 + p2 + p3
average = total / 3
print("Total:", total)
print("Average:", average)
print(45 % 7)
print(type("100"))
print(type(100))




