Open the app
  1. Home
  2. Lessons
  3. GCSE & IGCSE Computer Science
  4. Python basics: data types, input and output

Python basics: data types, input and output

🎬 The doodle video for this lesson is coming soon. Subscribe on YouTube to see it first.

Every program takes input, processes it and gives output. Choosing the right data type makes the processing work.

Data types

Input and output

input() shows a prompt and always returns a string.
To do maths with it, convert it: int() for whole numbers, float() for decimals. str() turns a number into a string.
print() displays output. Separate items with commas.
name = input("Name: ")
age = int(input("Age: "))
print("Hello", name)
print("Next year you will be", age + 1)

Assignment and initialisation

Assignment stores a value in a variable: score = 0.
Initialisation is the first assignment, giving a variable its starting value before it is used.
Python reads from right to left: in total = total + 5, it works out total + 5 first, then stores it.
Worked example

The user types Sam and 14 for the program above. What is output?

  1. name = "Sam", age = int("14") = 14
  2. print("Hello", name) gives Hello Sam
  3. age + 1 = 15

Answer: Hello Sam / Next year you will be 15

Key idea

Integer, real, Boolean, character and string are the basic types. input() returns a string, so use int() or float() before doing maths. Initialise variables before using them.

The interactive lesson includes the diagrams for this topic.

Check you have got it

Answer 6 quick questions with instant marking. If you get one wrong, GCSE-ready shows you why and gives you another go. It is free, and you do not need an account.