NOETRION

Browse by topic

← All articles
LESSON 1 OF 4 · Python & data foundations
TUTORIAL · 8 MIN READ

Your first Python program: input, variables, and output

Build a tiny calculator and learn to read what each line does.

A program is a sequence of instructions. Start with a small one whose result you can predict. This example asks for a number, doubles it, and prints the answer.

text = input("Enter a number: ")
number = int(text)
double = number * 2
print("Double is", double)

Trace the values

If you enter 7, input() returns the string "7". The int() call turns it into the integer 7. Multiplication produces 14, and print() displays Double is 14. A variable is a name referring to a value; number and double help you follow the transformation.

Input
"7"
→Convert
7
→Calculate
14
→Output
Double is 14
Follow one value through the program before changing the code.

Test an edge case

Try entering a word such as seven. Python raises a ValueError at int(text) because the text is not a valid integer. Read the final line of the error and locate the line named in the traceback. Errors are information about what the program tried to do.

Try it yourself

Change * 2 to * 3. Predict the output for an input of 5 before running it. Then use a negative input. These two checks confirm you understand the calculation rather than only the example.

Check your understanding

What does input() return before int() converts it?

Choose one answer