Skip to content
Study Hall
Lesson 1 of 5Computer Programming
Unit 5 · Lesson 1 of 5

Variables, Data Types and Input/Output

The three things every program does — take input, work on it, produce output — and the variables that hold it all.

3 min read10 practice questionsAsk AI about this →

Key things to remember

  1. 1= assigns a value; == tests equality.
  2. 2input() always returns a string — wrap it in int() or float() for numbers.
  3. 3/ gives a float; // gives integer division; % gives the remainder.
  4. 4"5" + "5" is "55", but 5 + 5 is 10.
  5. 5Flowchart shapes: oval = start/end, parallelogram = input/output, rectangle = process, diamond = decision.
  6. 6n % 2 == 0 is the standard test for an even number.

A program is a list of instructions carried out in order, and the very first thing it needs is somewhere to put values. A variable is a named box in memory. You put a value in it with an assignment, and from then on the name stands for whatever is currently inside. The name is for you; the computer only cares about the address.

The equals sign does not mean equals
In programming, = means "take what is on the right and store it in the name on the left". So count = count + 1 is not a false equation — it reads the current value of count, adds one, and stores the result back. To test whether two things are equal you use ==, which is a completely different operator.
The shape carries meaning — never draw everything as a box.START / ENDterminator — one entry, one exitINPUT / OUTPUTread a value, print a resultPROCESSa calculation or assignmentDECISIONa yes/no test — exactly two exitsa loop is a decision whose “yes” goes back upPseudocode does the same job:READ markIF mark >= 50 THEN PRINT "Pass"ELSE PRINT "Fail"ENDIF
Fig 1Planning a program before writing code: the standard shapes.

Data types

TypePython nameExampleWhat it is for
Integerint42, -7, 0Counting — whole numbers only
Floating pointfloat3.14, -0.5, 2.0Measurements, anything with a decimal
Stringstr"TEJ3M", "5"Text, including digits used as text
BooleanboolTrue, FalseThe result of a comparison; conditions
Listlist[4, 8, 15]An ordered collection of values

Python decides a variable's type from the value you assign, which is convenient but means type errors surface at run time rather than when you write the code. The distinction that trips everyone up is the string "5" versus the integer 5. They look identical on screen and behave completely differently: 5 + 5 gives 10, but "5" + "5" gives "55", because + on strings means join.

Input and output

PythonWhat it does
name = input("Your name: ")Shows the prompt and stores what the user types — ALWAYS as a string
age = int(input("Age: "))Converts the typed text to an integer before storing it
temp = float(input("Temp: "))Converts to a floating point number
print("Hello", name)Prints values separated by a space
print(f"Hi {name}, you are {age}")An f-string: substitutes each variable inside the braces
input() always returns a string
Even when the user types 25, input() hands you the string "25". Forget to convert and age + 1 raises a TypeError, while age * 2 silently produces "2525" — which is worse, because it runs without complaining. Wrap every numeric input in int() or float() at the moment you read it.

Operators

OperatorMeaningExampleResult
+ - *Add, subtract, multiply7 * 321
/Division — always gives a float7 / 23.5
//Integer division — discards the remainder7 // 23
%Modulo — the remainder only7 % 21
**Exponent2 ** 8256
Modulo is more useful than it looks
n % 2 == 0 tests whether n is even. n % 5 == 0 tests divisibility by 5. seconds % 60 gives the seconds left over after whole minutes. Any "every nth time" or "wrap around" problem is a modulo problem — including flashing an LED on every third loop.

Planning before coding

Writing code before you know what it should do is the slowest way to write it. Two planning tools are standard. A flowchart uses the shapes above to draw the path a program takes. Pseudocode writes the logic in structured English with no syntax to get wrong. Both are about getting the logic right while it is still cheap to change.

PseudocodePython
READ radiusradius = float(input("Radius: "))
SET area TO 3.14159 * radius * radiusarea = 3.14159 * radius ** 2
PRINT "Area is", areaprint(f"Area is {area:.2f}")
Pseudocode maps almost line for line onto real code — which is the point.
Naming variables
Use lowercase with underscores, and say what the value means: total_marks, not tm or x. Never use a single letter except for a loop counter. A well-named variable removes the need for a comment, and you will read your own code far more often than you write it.

Worked examples

Reveal one step at a time and try to beat it to the next line.

Example 1
Write a program that asks for a temperature in Celsius and prints it in Fahrenheit, rounded to one decimal place.
Example 2
A student writes total = input("Enter a mark: ") + 10 and gets a TypeError. Explain and fix it.
Example 3
Predict the output of each expression: 17 / 5, 17 // 5, 17 % 5, 2 ** 5.

Where marks get lost

  • Using = where == is needed in a condition — Python raises a syntax error, and other languages silently assign instead.
  • Forgetting int() around input() and then trying arithmetic on a string.
  • Using / where // is needed, or vice versa. / always gives a float, even 10 / 2 which gives 5.0.
  • Naming variables x, a, temp2 — future you will not remember what they held.
  • Printing without units or context, so the output is a bare number the user cannot interpret.
  • Assuming a value's type from how it looks on screen. "5" and 5 display identically and behave completely differently.

Flashcards 10

What is the difference between = and ==?
What type does input() return?
7 / 2 vs 7 // 2
What does % do?
+ 6 more

Practice

1
What does input() return when the user types 42?
2
What is the value of 23 % 7?
3
What is the value of 23 // 7?
4
What is the result of "3" + "4" in Python?
5
Which line correctly reads a whole number from the user?
6
Which expression tests whether the variable n is divisible by 3?
7
Match each flowchart shape to its meaning.
Oval
Parallelogram
Rectangle
Diamond
8
What does 3 ** 4 evaluate to?
9
A program stores x = "10" then runs print(x * 3). What is printed?
10
Order the stages of a simple program that converts a mark to a percentage.
  1. 1Convert both inputs from strings to numbers
  2. 2Print the result formatted to one decimal place
  3. 3Calculate mark divided by total, times 100
  4. 4Read the mark and the total from the user

Spot a mistake on this page? Let us know