🟢 Beginner  ·  Lesson 05

Variables and Data Types in Python

Introduction

In Python, a variable is a name that points to a value stored in memory. Unlike C or Java, you do not declare the type — Python figures it out automatically from the value you assign. This is called dynamic typing.

💡 Why this matters

Variables are the foundation of every program. Marks, names, totals, flags — everything your program remembers is stored in a variable. Master this first; the rest of Python builds on it.

What is a Variable?

Think of a variable as a labelled box. The label is the name, and the box holds a value. When you write marks = 90, Python creates a box named marks and puts 90 inside it.

The = sign is the assignment operator — it does not mean "equal to" (that is ==). It means "store the value on the right into the name on the left".

Built-in Data Types

Python has several built-in data types. These five are used most often at Class 12 to B.Tech level:

TypeExampleUsed For
intage = 17Whole numbers
floatpct = 91.5Decimal numbers
strname = "Aman"Text
boolpassed = TrueTrue / False
NoneTyperesult = None"No value yet"

Program 1: Declaring Variables

Python – variables.py
# Each line creates a variable of a different type
name = "Aman"        # str
age = 17             # int
percentage = 91.5    # float
is_pass = True       # bool

print("Name:", name)
print("Age:", age)
print("Percentage:", percentage)
print("Passed:", is_pass)
Name: Aman Age: 17 Percentage: 91.5 Passed: True

Line-by-line:

  • name = "Aman" — text goes inside quotes, so Python stores it as a str.
  • age = 17 — a whole number with no decimal becomes an int.
  • percentage = 91.5 — the decimal point makes Python store it as a float.
  • is_pass = TrueTrue/False (capital T/F) are bool values.
  • print(...) — the comma adds a space between the label and the value automatically.

Program 2: Checking & Converting Types

Use type() to check a type, and int()/float()/str() to convert (called type casting).

Python – type_convert.py
marks = "85"               # this is a STRING, not a number!
print(type(marks))          # <class 'str'>

# Convert string to integer before doing maths
marks_int = int(marks)
print(marks_int + 5)        # now arithmetic works
print(type(marks_int))
<class 'str'> 90 <class 'int'>

Why this is important: data from input() is always a string. If you forget to convert it, "85" + 5 gives an error. Program 3 shows the real fix.

Program 3: Student Report Card

A practical program that uses every type together:

Python – report_card.py
name = input("Enter student name: ")
maths = int(input("Maths marks: "))
science = int(input("Science marks: "))
english = int(input("English marks: "))

total = maths + science + english
percentage = total / 3
is_pass = percentage >= 33

print("\n----- REPORT CARD -----")
print("Name       :", name)
print("Total      :", total, "/ 300")
print("Percentage :", round(percentage, 2), "%")
print("Result     :", "PASS" if is_pass else "FAIL")
Enter student name: Aman Maths marks: 90 Science marks: 85 English marks: 80 ----- REPORT CARD ----- Name : Aman Total : 255 / 300 Percentage : 85.0 % Result : PASS
  • int(input(...)) reads text and converts it to a number in one step.
  • percentage >= 33 produces a bool stored in is_pass.
  • round(percentage, 2) keeps only 2 decimal places.
  • "PASS" if is_pass else "FAIL" is a one-line conditional (ternary).

Variable Naming Rules

  • Can contain letters, digits and underscore (_), e.g. total_marks.
  • Cannot start with a digit: 2marks is invalid, marks2 is fine.
  • Case-sensitive: Name and name are different.
  • Cannot be a keyword (if, class, for...).
  • Use clear names: percentage, not p.

Common Mistakes

  • Forgetting to convert input() to int before maths.
  • Using = (assign) when you meant == (compare).
  • Writing true/false in lowercase — Python needs True/False.
  • Wrong indentation (Python is strict about spaces).

Practice Tasks

  1. Create variables for your name, class and three subject marks; print a formatted report.
  2. Take two numbers from the user and print their sum, difference and product.
  3. Store your height in metres (float) and print whether it is above 1.5 (bool).
  4. Use type() on five different values and note the output.

Summary

  • A variable is a name pointing to a value; Python detects the type automatically.
  • Main types: int, float, str, bool, None.
  • input() always returns a string — convert with int()/float().
  • type() checks a type; = assigns, == compares.
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.