Variables and data types

Variables and data types

How does a shopping app remember your cart and work out your bill? See how programs follow steps, store values in variables and handle numbers, text and true or false.

Asha hands pictorial instructions to her new helper in a stationery shop

Instructions for a new helper

Asha runs a stationery shop in Indore. Her new helper knows nothing, so she writes exact steps: take the price, multiply by the quantity, tell the customer.

A computer is the same kind of helper. A list of exact steps for it is called a “program”.

Your first program

Computers don’t follow English. We write the steps in a programming language, and ours is Python.

print("Asha Stores")
print("Indore")
Output
Asha Stores
Indore

print shows whatever is inside the brackets. Python runs the lines from top to bottom.

Python as a calculator

Kabir buys 3 notebooks at ₹40 each, adds a ₹30 pen and pays with a ₹200 note.

print(3 * 40)
print(120 + 30)
print(200 - 150)
Output
120
150
50

* multiplies and / divides. Search for “online Python editor” and try your own sums.

A box tagged with a notebook pictogram holds a removable card, with a replacement card above it

Labelled boxes

Asha keeps a box labelled “Notebook price” with a slip inside that says 40. When the price changes, she swaps the slip. The label stays.

Programs need the same thing: a place to keep a value, with a name to find it again.

Variables

A named box in a program is called a “variable”. The = sign puts a value in the box.

price = 40
qty = 3
total = price * qty
print(total)
Output
120

Python opens the price and qty boxes, multiplies the values and stores the result in a new box, total.

Changing a value

Kabir now wants 5 notebooks. We put a new value in the same box:

qty = 3
qty = 5
print(qty)
print(40 * qty)
Output
5
200

A box holds only its latest value, so the old 3 is gone. The value can vary, hence the name variable.

A shop receipt uses person, package, percentage and checkmark symbols for different kinds of values

Different kinds of values

Look at one of Asha’s bills. It has a customer name, a quantity, a GST rate and a “Paid” stamp.

You can multiply a quantity, but you can’t multiply a name. Python also sorts values into kinds, called “data types”.

Numbers: int and float

Python has 2 types of numbers. Whole numbers are “int”, short for integer. Numbers with a decimal point are “float”.

total = 200
gst = total * 0.18
print(gst)
Output
36.0

Here 200 is an int and 0.18 is a float. Say GST is 18%. On ₹200, it comes to ₹36.

Strings

Text is a third type. We write it inside quotes, and Python calls it a “string”, or “str” for short.

name = "Kabir"
shop = "Asha Stores"
print(name)
print(len(shop))
Output
Kabir
11

len counts the characters in a string, spaces included.

True or false

Has Kabir paid? The answer is only yes or no. Python has a type for this, called “bool”, with just 2 values: True and False.

paid = True
print(paid)
print(type(paid))
Output
True
<class 'bool'>

type tells us the data type of any value.

Mixing types

What if the quantity arrives as text, like "3" typed into a form?

qty = "3"
print(qty + 2)
Output
TypeError: can only
concatenate str (not "int")
to str

Python stops with an error, because text and a number can’t be added. Always read the error’s last line first. The fix: int(qty) + 2 gives 5.

The full bill

Let’s put it all together for Kabir’s bill:

name = "Kabir"
total = 5 * 40
gst = total * 0.18
bill = total + gst
print(f"{name} pays ₹{bill}")
Output
Kabir pays ₹236.0

The f before the quotes lets us drop variables into a string using curly brackets.

Summary

  1. Program: A list of exact steps that a computer runs from top to bottom.
  2. Variable: A named box that stores a value. The = sign puts a value in it.
  3. Data types: int and float for numbers, str for text and bool for True or False.
  4. Errors: Types don’t mix freely. Read the error’s last line, then convert with int().