Introduction
A program is a list of instructions. Python reads them from the top, runs one line, then moves to the next. print() is the instruction that shows something on the screen.
Everything a computer does, from showing a web page to landing a plane, comes down to following instructions like these. Python is a good first language because its instructions read almost like English, so you can think about the problem rather than the punctuation.
How it works
Printing text
print("Hello, StepVioz!")
print("Python runs one line at a time.")Press Visualize, then step with the → key. The highlighted line is the one that just ran, and the dashed line is the one that runs next. Each print() adds one line to the output panel, at exactly the step that ran it.
Text inside quotes is called a string. Python prints it exactly as written, without the quotes.
Text or a calculation?
print(2 + 3)
print("2 + 3")
# A comment: Python skips this line
print(10 * 4)print(2 + 3) shows 5: without quotes, Python works out the calculation first. print("2 + 3") shows the characters 2 + 3, because they are inside quotes.
A line that starts with # is a comment. Python skips it completely; it is a note for people reading the code. Notice that stepping jumps straight over line 3.
Syntax
print("Some text") # text goes in quotes
print(2 + 3) # a calculation is worked out first
print("a", "b", sep="-") # choose what goes between the values
print() # an empty line
Example
print("Course:", "CSE", 101)
print("one", "two", "three", sep=", ")
print()
print("Done")printcan take several values separated by commas; it puts one space between them.sep=", "changes what goes between the values.print()with nothing inside prints an empty line.
Line by line
print("Course:", "CSE", 101)receives three values and shows them with one space between each.- The next line gives
sep=", ", so a comma and a space go between the values instead. print()with nothing inside prints a blank line.- The last line prints
Done.
Expected result
Course: CSE 101
one, two, three
Done
In the real world
Printing is how every programmer checks what a program is doing. A receipt, a bank statement and a report are all programs printing values in a particular layout. Comments matter too, because programs are read far more often than they are written.
Common mistakes
- Forgetting the quotes:
print(Hello)gives aNameError, because Python looks for a variable calledHello. - Mixing quotes:
print("Hi')is aSyntaxError. Start and end with the same kind. - Capital letters matter:
Print("Hi")is aNameError. Python only knowsprint.
Best practices
- Run your program after every small change.
- Write comments that explain why, since the code already shows what.
- Copy the exact spelling of
print, in lower case.
Summary
A program is a list of instructions run from top to bottom. print shows values, quotes mark text, and # starts a comment that Python ignores. sep chooses what goes between values.
Practice
Write a program that prints your name on the first line and your university or school on the second. Then add a comment above each line saying what it does.
Quick check
Key takeaways
- Python runs one line at a time, from top to bottom.
- Quotes mean text, and no quotes means work it out.
- Python is exact about spelling and capital letters.
Practice problems for this lesson
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.