Ravindra BagaleCourses & study guides मराठी Track your progress

Python Variables and Data Types Explained for Beginners (int, float, str, bool)

P2.1 What Is a Variable in Python

Why. You do not want to type the same number again and again. A variable gives a value a name, so you can reuse and change it in one place.

What. A variable is a name that points to a value. You create it with =. The name goes on the left, the value on the right.

  • Names can use letters, numbers and _, but cannot start with a number.
  • Names are case-sensitive: price and Price are different.
  • Use clear names like total_sales, not x.

How.

Create and use a variable

  1. Pick a clear name.
  2. Write name = value.
  3. Use the name anywhere later.
  4. Give it a new value when needed.
1. Python creates the value 4992. It makes the name price3. price now points to 4994. print(price) shows 499
What happens with price = 499 (read from top to bottom)

Worked example A — Store one value.

Step 1 — What we do. A shop in Pune sells a water bottle for ₹499. We store the price.

Step 2 — Type and run this code.

price = 499
print(price)

Step 3 — Output you should see.

499

Step 4 — What it means. The name price now holds 499. Wherever you write price, Python uses 499.

Worked example B — Use variables together.

Step 1 — What we do. We add a second variable and calculate the bill for 3 bottles.

Step 2 — Type and run this code.

price = 499
quantity = 3
bill = price * quantity
print(bill)

Step 3 — Output you should see.

1497

Step 4 — What it means. Variables can be used to make new variables. bill is 1497, that is ₹1,497.

Worked example C — Change a value.

Step 1 — What we do. There is a sale and the price drops. We update one line only.

Step 2 — Type and run this code.

price = 499
quantity = 3
price = 449
print(price * quantity)

Step 3 — Output you should see.

1347

Step 4 — What it means. The newest value wins. The bill is now 1347. This is why variables save time: change once, every calculation updates.

Python next to other languages — create a variable. You do not need to learn these other languages. This only shows why Python is easier to read.

Language Code
Python price = 499
Java int price = 499;
JavaScript let price = 499;
Go price := 499

Ravindra Bagale's Tip

Name variables the way you would name a column in Excel: order_amount, city, is_paid. Six months later you will still understand your own code.