Welcome to Chapter 1.1!
If Python programs were stories, variables would be the characters β each with a name, a personality (type), and a role in the plot.
Letβs learn how they work, with simple and enjoyable examples.
π§© 1. What Is a Variable? (Introduction)
A variable is like a labelled storage box in Python.
You put something inside it (a value), give it a name, and use it later.
Example:
message = "Hello Python!"
Here:
messageβ the label"Hello Python!"β the content inside the box
You can open the box anytime:
print(message)
Python: βHello Python!β
βοΈ 2. Declaring and Assigning Variables
In some languages (like Java), you must declare the type:
int age = 25;
But Python is cool and relaxed:
age = 25
No βintβ, no βstringβ, no paperwork.
Python examines the value and figures out the type automatically.
You can assign:
name = "Amit Sharma"
price = 99.99
is_active = True
π·οΈ 3. Naming Conventions (Write Clean Pythonic Code)
Python variable names must follow some rules:
β Allowed
- Letters (aβz, AβZ)
- Numbers (0β9)
- Underscores (_)
- Must NOT start with a number
β Not allowed
1name = "Amit" # starts with number
first-name = "Amit" # hyphens not allowed
β Pythonic Naming Style
Use snake_case:
user_age = 30
total_amount = 4500
is_verified = False
β Avoid
UserAge = 30 # PascalCase is for Classes
userAge = 30 # camelCase is for Java developers :)
π 4. Understanding Variable Types
Python supports many data types.
Here are the most common:
1. Integer
Whole numbers
count = 10
2. Float
Decimal numbers
temperature = 36.7
3. String
Text inside quotes
title = "Learn Python"
4. Boolean
True or False
is_logged_in = True
5. None
Represents βnothingβ or βemptyβ
data = None
π§ͺ 5. Type Checking and Conversion
Sometimes you want to check what type a variable is:
β Checking type
age = 25
print(type(age)) # <class 'int'>
β Type conversion (casting)
Convert integer to string:
age = 25
age_text = str(age)
Convert string to number:
price = "199"
price_number = int(price)
Convert to float:
value = "3.14"
value_float = float(value)
If you convert something invalid:
age = "twenty"
int(age)
Python:
βI cannot convert poetry into numbers.β
β ValueError
π 6. Dynamic Typing
Python is a dynamically typed language.
This means a variable can change its type anytime β Python doesnβt mind.
Example:
item = 10 # integer
item = "Laptop" # now a string
item = 3.5 # now a float
Python is flexible, but with great power comes great responsibility.
Changing variable types too often makes code confusing.
Better:
item_count = 10
item_name = "Laptop"
item_weight = 3.5
π§― 7. Practical Examples and Common Errors
β Example 1: Calculating total price
quantity = 3
price_per_item = 499.99
total = quantity * price_per_item
print("Total price:", total)
β Example 2: Combining strings
first_name = "Amit"
last_name = "Sharma"
full_name = first_name + " " + last_name
print(full_name)
β Common Error 1: Mixing strings and numbers
age = 25
text = "Age: " + age
Python:
TypeError: can only concatenate str (not “int”)
Fix:
text = "Age: " + str(age)
β Common Error 2: Typos in variable names
name = "Amit Sharma"
print(nam) # NameError
Tip: use descriptive names to avoid confusion.
π Wrap-up
You learned:
β What variables are
β How to assign values
β Python naming rules
β Different variable types
β Checking & converting types
β Dynamic typing
β Real examples and frequent errors
This chapter gives you the foundation to write real Python programs.

Leave a Reply