Lecture 2 – Python Fundamentals (Variables, Data Types & Operators)

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

WrongCorrect
1namename1
my namemy_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

TypeExampleUse
int (integer)20Whole numbers
float20.5Decimal values
str (string)“Data”Text
bool (boolean)True / FalseYes/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.

OperatorUseExample
+Addition5 + 3
-Subtraction10 – 2
*Multiplication4 * 2
/Division8 / 2
//Floor Division (No decimal)9 // 2
%Modulus (Remainder)10 % 3
**Power2 ** 3

Example (Power, Division & Modulus)

x = 10
print(x % 3)   # remainder 1
print(x // 3)  # 3
print(2 ** 4)  # 16

For official Python documentation

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) %

Next Lecture: Python Lists, Tuples, Dictionaries

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.

Write a variable city to store your city name and print it?
city = "Lahore"
print(city)
Create variables for three product prices and print their total + average?
p1 = 120
p2 = 350
p3 = 200

total = p1 + p2 + p3
average = total / 3

print("Total:", total)
print("Average:", average)
Find the remainder when 45 is divided by 7?
print(45 % 7)
Check the data type of "100" and 100?
print(type("100"))
print(type(100))

Leave a Reply

Your email address will not be published. Required fields are marked *