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
- Install Python 3 (or open Google Colab in your browser, nothing to install) and run print("Hello").
- Create variables: name = "Asha", age = 28, salary = 55000.5, is_active = True. Python works out the type from the value.
- 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.
- Combine values with f-strings: f"{name} earns {salary}". Do maths with + - * / and // (whole division) and % (remainder).
- 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
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 β 60A 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?
- float
- int
- 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