Skip to content
Lesson 2

Variables In Real Life

A variable is a labelled box. You put something in. You can take it out later by saying the label. That is the entire idea. The reason it sounds harder than that, in most courses, is that the examples are dry. Let radiu…

There is more than one way to understand this. If you have only been taught one, you have been taught less than you deserve.

There is more than one way to understand this. If you have only been taught one, you have been taught less than you deserve.

A variable is a labelled box. You put something in. You can take it out later by saying the label. That is the entire idea. The reason it sounds harder than that, in most courses, is that the examples are dry. Let radius equal five. Let temperature equal twenty seven. None of those are things you actually care about. We will use boxes whose contents you will recognise from the world you already live in.

Open Replit. New Repl, Python, name it variable-zindagi. Type these four lines. balance = 1450. balance = balance - 50. print("Easypaisa balance ke baad recharge,", balance). Run it. The screen prints fourteen hundred. The variable balance held one number, then was updated. The label stayed the same. The contents changed. This is exactly what happens to a real Easypaisa wallet on your father's phone. He starts the day with whatever was left at night. He sends fifty rupees to his cousin in Faisalabad. The wallet shows the new number. The label, balance, has not moved. The number behind the label has.

Variables can hold more than numbers. They hold names, sentences, true or false flags, lists, and almost anything else. A scorecard on the back of your maths copy holds three numbers, marks in three quizzes. In Python, that is three variables. quiz_one = 18, quiz_two = 21, quiz_three = 24. You can add them, total = quiz_one + quiz_two + quiz_three. You can divide for the average, average = total / 3. The same five lines that read like a maths problem are the same five lines a tax software at FBR uses to add three quarters of income. Same shape, much bigger numbers, same idea.

An attendance example, closer to school life. Imagine a Class 7 register with thirty kids. Every morning the teacher marks present or absent. In code, that is one variable per child or, more elegantly, a list. Today let us start with one. present_count = 0. Every time a name is called and the teacher hears yes sir, the program runs present_count = present_count plus one. By the end of register, the variable holds the count of children in school today. Add a second variable, total_count = 30. Then percentage = (present_count divided by total_count) times 100. Three variables, two operations, the entire attendance system that schools across Pakistan run by hand every day.

Variables can be reassigned freely, and this is the source of half the bugs in early code. If on line ten you set price equal to one hundred and fifty, and on line twenty seven you accidentally write price equal to one hundred and five, every later use of price is now wrong. The cure is to be careful where a variable is changed. Many professional codebases have a rule that a variable, once set, is rarely changed again. Instead, a new variable holds the new value. price_with_tax. price_after_discount. The original price stays clean. This habit is one of the simplest ways to write bug free code. Apply it from the first week.

Closing thought for this lesson. Variables are how a program holds the world steady for long enough to do something with it. The Easypaisa balance, the quiz scores, the kids in the register, the price of biscuits, are the world. The variable is the small finger your code uses to point at one piece of the world and say, this one. Get comfortable naming, setting, and updating these little pointers, and you will have crossed the first real bridge from learner to programmer. Everything else in this track sits on top of that bridge.

Estimated time: 12 min