Variables and Data Types
Storing values
A program that cannot remember anything is not much use. A variable is a name attached to a value, so you can refer to that value later.
name = "Priya"
age = 19
cgpa = 8.42
print(name, "is", age, "years old with a CGPA of", cgpa)
The = sign is assignment, not equality. Read age = 19 as "let age refer to 19", not "age equals 19". This matters because assignment has a direction: the value on the right is worked out first, then the name on the left is attached to it.
That direction explains a line beginners find strange:
count = 5
count = count + 1
print(count)
Python evaluates count + 1 using the current value (5), gets 6, and then makes count refer to 6. As mathematics, count = count + 1 is nonsense. As an instruction, it is perfectly clear.
A variable can be reassigned freely, including to a value of a different type. The name is just a label; it is not permanently bound to one kind of thing.
The four types you will use first
Python has many types, but almost everything you write in your first month uses these four.
| Type | Name in Python | Example | What it holds |
|---|---|---|---|
| Integer | int |
19, -4, 0 |
Whole numbers, no decimal point |
| Floating point | float |
8.42, -0.5, 3.0 |
Numbers with a decimal part |
| String | str |
"Priya", 'B.Tech' |
Text |
| Boolean | bool |
True, False |
A truth value |
Some notes worth carrying forward:
3and3.0are different types even though they represent the same quantity.3is anint; the decimal point makes3.0afloat.- Booleans are written with a capital letter:
TrueandFalse. Lowercasetrueis not defined and will raise an error. - A number in quotes is a string.
"19"is text that happens to look like a number, and Python will not do arithmetic with it.
Checking a type
When you are unsure what you are holding, ask. The type() function tells you.
roll_number = 42
percentage = 76.5
branch = "Computer Science"
is_hosteller = True
print(type(roll_number))
print(type(percentage))
print(type(branch))
print(type(is_hosteller))
print(type("42"))
print(type(42))
The last two lines are the important ones. "42" is a str and 42 is an int. When a program misbehaves in a way you cannot explain, printing type() of the suspicious variable is often the fastest way to find the problem.
Converting between types
Conversion — sometimes called casting — is done with the type name used as a function.
marks_text = "87"
marks_number = int(marks_text)
print(marks_text + marks_text)
print(marks_number + marks_number)
print(float(7))
print(int(9.87))
print(str(2026) + " batch")
Three things to notice:
marks_text + marks_text gives "8787". With strings, + joins them end to end. With numbers, + adds. The same symbol behaves differently depending on the types involved, which is exactly why type confusion causes so many beginner bugs.
int(9.87) gives 9, not 10. Converting a float to an int truncates — it throws away the decimal part rather than rounding. If you want rounding, use round(9.87), which gives 10.
str(2026) turns a number into text so it can be joined to another string. Without it, 2026 + " batch" raises TypeError.
int("hello") raises ValueError because there is no sensible number in that text. int("12.5") also fails — that string contains a float, not an int; use float("12.5") instead, or int(float("12.5")) if you want 12.
Naming rules and conventions
Rules Python enforces:
- A name may contain letters, digits and underscores.
- It may not start with a digit.
2nd_marksis invalid;marks_2is fine. - No spaces and no hyphens.
total marksis two names;total-marksis a subtraction. - Names are case-sensitive.
Totalandtotalare different variables. - You cannot use a Python keyword such as
if,for,class,def,True,importorreturnas a variable name.
Conventions Python does not enforce but everyone follows:
- Use
snake_case: lowercase words joined by underscores, liketotal_marksorstudent_name. - Choose names that describe the value.
xanda1tell a reader nothing;attendance_percenttells them everything. - Avoid shadowing built-in names. Assigning
list = [1, 2]orstr = "hi"works, but it breaks thelist()andstr()functions for the rest of your program.
student_name = "Rahul Menon"
subjects_taken = 6
attendance_percent = 82.5
is_eligible = attendance_percent >= 75
print(student_name, "takes", subjects_taken, "subjects")
print("Eligible to sit exams:", is_eligible)
That last assignment is worth a second look. attendance_percent >= 75 is a comparison, and a comparison produces a boolean. You do not need if to get True or False — a condition is a value in its own right.
Common mistakes
Using a variable before assigning it. Printing total before any line has set total gives NameError: name 'total' is not defined. Python has no concept of an "empty" variable waiting to be filled; the name simply does not exist until you assign it.
Assuming assignment is a permanent link.
a = 5
b = a
a = 10
print(b) # still 5, not 10
b = a copies the value at that moment. Changing a afterwards does not reach back into b.
Adding a string to a number. "Total: " + 90 raises TypeError: can only concatenate str (not "int") to str. Either convert with str(90), or pass both to print separated by a comma.
Writing true instead of True. Python's booleans are capitalised. Lowercase true and false are undefined names.
Expecting exact decimal arithmetic. print(0.1 + 0.2) shows 0.30000000000000004. Floats are stored in binary and cannot represent every decimal exactly. This is not a Python bug — every language using standard floats behaves this way. For display, use round(value, 2).
Practice
Work through the variables and types exercises on the practice page.