1.1 β€” Variables in Python (The Friendly Containers of Your Code)

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.

Comments

Leave a Reply

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