- Home
- Lessons
- GCSE & IGCSE Computer Science
- 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)
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.
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.
The user types Sam and 14 for the program above. What is output?
- name = "Sam", age = int("14") = 14
- print("Hello", name) gives Hello Sam
- age + 1 = 15
Answer: Hello Sam / Next year you will be 15
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.