๐ฏ Learning Objectives
By the end of this lesson, learners will be able to:
-
Define what a variable is in Python
-
Create and assign values to variables
-
Follow Pythonโs naming rules and best practices
-
Understand the concept of dynamic typing
๐ Lesson Content
๐น What is a Variable?
A variable in Python is a named container that stores data.
You can assign a value to a variable using the equals sign (=
).
๐งโ๐ป Example:
Here:
-
name
is a variable storing a string -
age
is a variable storing an integer
๐ธ How Python Handles Variables
-
Python is dynamically typed, which means you donโt need to declare the type โ Python figures it out for you.
-
A variableโs type is assigned automatically at runtime.
๐ง Example:
โ Python allows reassigning a variable with a different data type.
๐ธ Variable Naming Rules (Syntax)
-
Must start with a letter or an underscore (
_
) -
Cannot start with a number
-
Can only contain letters, numbers, and underscores
-
Case-sensitive (
Age
โage
)
โ Valid Names:
โ Invalid Names:
๐ก Best Practices
-
Use descriptive names:
user_age
,total_price
-
Use snake_case: lowercase with underscores for multi-word variables
๐ง Practical Examples
๐ Assignment / Activity
-
Create three variables:
-
Your name
-
Your age
-
A Boolean that says if you’re a student
-
-
Print all three using the
print()
function
โ Common Mistakes
Mistake | Why it’s wrong |
---|---|
2name = "Amit" |
Starts with a number |
first name = "Radha" |
Contains a space |
Print("hello") |
Python is case-sensitive: should be print |
๐ง Reflection Question
What happens if you reassign a variable multiple times?
Try: