Your first Python program
Python follows a series of instructions and can print the result for you to see.
How it works
Think of a script as a list of things for the computer to do. A line starting with # is a comment for a reader. The first display line in the example gives print a text value. The second gives it a calculation. Python works out that calculation before print displays the result.
The console shows two lines, Hello and then 5. Try reading the material inside the parentheses first. That is how you can predict what print receives.
Walk through an example
Imagine a program with two lines. print("Hello") displays the word Hello. print(2 + 3) evaluates the addition first and then displays 5 on the next line.
What to watch for
Quotation marks surround text. print("2 + 3") displays the expression exactly as written. Indentation also has meaning once you start writing blocks.
Code example
print("Hello")
print(2 + 3)Check your understanding
What appears when Python evaluates this line?
See the example and explanation
- Worked example
- Given
print(10 - 4) - Answer
6
Python subtracts 4 from 10, then sends the resulting number to print.