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:
priceandPriceare different. - Use clear names like
total_sales, notx.
How.
Create and use a variable
- Pick a clear name.
- Write
name = value. - Use the name anywhere later.
- Give it a new value when needed.
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.
Ravindra Bagale's Tip – मराठी
Excel मध्ये column ला जसं नाव द्याल तसं variables ला नाव द्या: order_amount, city, is_paid. सहा महिन्यांनीही स्वतःचा code समजेल.
Ravindra Bagale's Tip – हिंदी
Excel में column का जैसा नाम दोगे वैसा variables का नाम दो: order_amount, city, is_paid. छह महीने बाद भी अपना code समझ आएगा.