📖 Phase 1: Programming Fundamentals
📌 Topic 4: Variables
A variable is a named container used to store data in memory. Instead of using the actual value repeatedly, we store it in a variable and use the variable name whenever needed.
Think of a variable like a labeled box. You can store different items inside the box, and whenever you need that item, you simply refer to the label instead of searching for the item.
Why Do We Need Variables?
Variables help us:
• Store data for later use.
• Reuse values multiple times.
• Make programs easier to read.
• Update values whenever required.
• Avoid writing the same value repeatedly.
Creating Variables in Python
In Python, you don't need to declare the data type. Simply assign a value using the "=" operator.
Example:
name = "Ajay"
age = 29
salary = 400000
Here:
• "name" stores a string.
• "age" stores an integer.
• "salary" stores a number.
Printing Variables
You can display variable values using the "print()" function.
name = "Aman"
age = 25
print(name)
print(age)
Output:
Aman
25
Updating Variables
Variables can be changed anytime.
score = 80
score = 95
print(score)
Output:
95
The old value is replaced with the new value.
Multiple Variable Assignment
You can assign multiple variables in one line.
x, y, z = 10, 20, 30
print(x)
print(y)
print(z)
Output:
10
20
30
Naming Rules for Variables
✅ Variable names can contain letters, numbers, and underscores.
✅ Variable names must start with a letter or underscore.
✅ Variable names are case-sensitive ("age" and "Age" are different).
❌ Variable names cannot start with a number.
❌ Variable names cannot contain spaces or special characters.
Good vs Bad Variable Names
✅ Good:
student_name = "Rahul"total_marks = 450is_logged_in = True❌ Bad:
1name = "Rahul"student name = "Rahul"total-marks = 450These will produce errors because they don't follow Python's naming rules.
Best Practices
• Use meaningful variable names.
• Follow the "snake_case" naming convention.
• Keep names short but descriptive.
• Avoid using Python keywords like "if", "for", "class", or "print" as variable names.
Key Takeaways
• A variable is used to store data.
• Variables make programs more readable and reusable.
• Python automatically determines the data type of a variable.
• Variable values can be updated anytime.
• Always use meaningful and valid variable names.
➡️ Double Tap ❤️ For More
