FirstHack Learn
Log in Sign up free

Variables and Data Types

9 min read · 23 views

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.

Python 3
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:

Python 3
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:

  • 3 and 3.0 are different types even though they represent the same quantity. 3 is an int; the decimal point makes 3.0 a float.
  • Booleans are written with a capital letter: True and False. Lowercase true is 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.

Python 3
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.

Python 3
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.

⚠️Conversion can fail

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_marks is invalid; marks_2 is fine.
  • No spaces and no hyphens. total marks is two names; total-marks is a subtraction.
  • Names are case-sensitive. Total and total are different variables.
  • You cannot use a Python keyword such as if, for, class, def, True, import or return as a variable name.

Conventions Python does not enforce but everyone follows:

  • Use snake_case: lowercase words joined by underscores, like total_marks or student_name.
  • Choose names that describe the value. x and a1 tell a reader nothing; attendance_percent tells them everything.
  • Avoid shadowing built-in names. Assigning list = [1, 2] or str = "hi" works, but it breaks the list() and str() functions for the rest of your program.
Python 3
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.

TEXT
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.

Create a free account to track what you have finished.