Lesson · ⏱ 7 min read

🐍 Python basics: variables and data types

EnglishHinglish

🏠 Real-life example

Your kitchen has labelled containers: 'sugar', 'salt', 'rice'. You put something inside and later find it by its label. A variable in Python is exactly that: a labelled container that holds a value. And just as you don't keep milk in a salt jar, each value has a type: a number, some text, or true/false.

πŸ’‘ In simple words

Python is the most-used language in AI because it reads almost like English and has ready-made libraries for data and ML. You store information in variables using '='. The common data types are int (whole numbers), float (decimals), str (text in quotes) and bool (True or False). print() shows a value, and type() tells you what type it is. Most beginner bugs come from mixing types, such as adding a number to text.

πŸͺœ Step by step

  1. Install Python 3 (or open Google Colab in your browser, nothing to install) and run print("Hello").
  2. Create variables: name = "Asha", age = 28, salary = 55000.5, is_active = True. Python works out the type from the value.
  3. Check a type with type(age). Convert with int("42"), float("3.5") or str(100). Input from users and files usually arrives as text.
  4. Combine values with f-strings: f"{name} earns {salary}". Do maths with + - * / and // (whole division) and % (remainder).
  5. Practise by writing tiny programs: a tip calculator, an EMI calculator, a temperature converter.

πŸ”‘ Key terms

Variable
A name that points to a stored value, e.g. price = 20.
String (str)
Text inside quotes, e.g. 'Masala chai'.
Boolean (bool)
A value that is either True or False.

πŸ‘€ See it in action

python
item = "Masala chai"     # str
price = 20               # int
rating = 4.5             # float
in_stock = True          # bool

print(item, "costs", price)
print(type(rating))      # <class 'float'>
total = price * 3        # 3 cups β†’ 60

A tiny chai-stall menu stored in variables.

⚠️ Common mistakes to avoid

  • Adding text and numbers: "Age: " + 28 gives an error. Use f"Age: {28}" or str(28).
  • Forgetting that input() returns text: input() + 5 fails. Convert first with int(input()).
  • Using = (assign) when you mean == (compare). x = 5 sets x; x == 5 asks whether x is 5.

βœ… Quick check

What is the type of the value 4.5?

  1. float
  2. int
  3. str
Show answer

A. float: Numbers with a decimal point are floats. Whole numbers are ints.

πŸ™‹ Questions learners ask

Which Python version should I learn?

Python 3, the latest stable version. Python 2 is retired. Small differences between 3.x versions won't matter while learning.

Do I need a powerful laptop?

No. Any laptop runs Python. For heavier AI work later, free cloud notebooks like Google Colab give you GPUs.

πŸ“Œ Remember

A variable is a labelled box, and every value inside it has a type.

Learn this as part of a full course

Track your progress, take the quick checks and follow the lessons in the right order. This lesson is part of:

AI Foundations Path Β· AI Product Engineer Β· MLOps / AI Platform Engineer Β· LLM / Agent Engineer Β· AI Forward Deployed Engineer

Continue in the course