The idea
A program is a list of instructions written in a language the computer understands. Python reads your instructions from top to bottom, one line at a time, and does exactly what each line says. Not what you meant, what you wrote. That sounds harsh, but it's also freeing: when something goes wrong, the answer is always somewhere in the lines you can see.
Think of it like a recipe. A recipe is useless until someone follows it, step by step, in order. Python is the cook. Your job is to write a recipe so clear that a very literal cook can't get it wrong.
Python runs your file from the first line to the last. If line 3 breaks, lines 4 onwards never run.
Your first instruction: print()
The first tool every programmer learns is print(). It takes whatever you put between the brackets and shows it on the screen.
print("Hello, Python!")
print("I'm learning to code.")
Hello, Python!
I'm learning to code.
A few things are happening here, and every one of them matters:
printis the name of a function, a named action Python already knows how to do. You'll write your own functions later.- The brackets
( )mean "do it now, with this". Calling a function always needs brackets. - The text inside quotes is a string, Python's word for a piece of text. The quotes tell Python where the text starts and ends. Single quotes
'like this'and double quotes"like this"both work, just don't mix them on one string. - Each
print()ends with a new line, so the second message appears underneath the first.
Press Try it on the code above to run it yourself. Change the words and run it again. You can't break anything here.
Printing several things at once
You can give print() more than one value, separated by commas. Python puts a space between them:
print("Coffee", "Tea", "Juice")
print("Total:", 3, "drinks")
Coffee Tea Juice
Total: 3 drinks
Notice 3 has no quotes. It's a number, not text, and Python is happy to print numbers too. Try print(2 + 3) and then print("2 + 3"). The first does the maths and shows 5. The second shows the text exactly as written, because anything in quotes is just text to Python.
Two handy options: sep and end
print() has optional settings you can change by name. sep is the separator placed between values (normally a space), and end is what's printed at the very end (normally a new line).
print("2024", "10", "05", sep="-")
print("Loading", end="...")
print("done!")
2024-10-05
Loading...done!
The first line joins the values with dashes. On the second, end="..." replaces the usual new line with three dots, so the next print() continues on the same line.
Comments: notes for humans
A comment is a note in your code that Python completely ignores. In Python, a comment starts with # and runs to the end of the line.
# Monthly report for the sales team
print("Sales report") # the title the manager asked for
Python sees only print("Sales report"). The comments are for the next person who reads your code, and very often that person is you, three months from now, wondering what on earth you were thinking.
Good comments explain why, not what. print("Sales report") # prints the title adds nothing, since the code already says that. # Finance needs the title on its own line for their import script explains a decision you can't see in the code.
Comments are also useful for temporarily switching off a line while you experiment. Put a # in front of it and Python skips it. In the editor on this site, select some lines and press Ctrl + / (or Cmd + / on a Mac) to comment or uncomment them in one go.
Running code: here and on your computer
On this site, press Run to execute your code and see what it prints. Press Submit to run it and check it against the question's requirements.
On your own computer, the usual way is:
- Write your code in a file ending in
.py, for examplehello.py. - Open a terminal in that folder.
- Type
python3 hello.pyand press Enter.
python3 hello.py
Python reads the file top to bottom and prints the results in the terminal. That's all "running a program" means.
Errors are normal (and helpful)
Every programmer sees errors every day. An error isn't Python being mean, it's Python telling you exactly where it got confused. Here's a classic:
print("Hello)
Line 1
print("Hello)
^
SyntaxError: unterminated string literal
Read errors from the bottom up. The last line names the problem: a SyntaxError means the code doesn't follow Python's grammar, and "unterminated string literal" means a string was opened with a quote but never closed. The line number and the little ^ arrow show where. Add the missing " and it works.
Here's another one you'll meet often:
prnt("Hello")
NameError: name 'prnt' is not defined
A NameError means Python doesn't recognise a name. Here it's a typo: prnt instead of print. Python never guesses what you meant, so spelling and capital letters must be exact. Print("Hi") with a capital P fails too.
When a line has an error, Python stops there. Anything after it doesn't run, so fix the first error first, then run again.
Why this matters for clean code
Even a two-line program has a reader. Using clear text, one instruction per line and comments that explain your reasons makes code easy to follow, and code that's easy to follow is easy to fix. These habits feel small now, but they're the same ones that keep a 5,000-line project readable.
Recap
- A program is instructions that Python runs top to bottom, one line at a time.
print()shows values on the screen. Text goes in quotes, numbers don't.sepchanges what goes between values,endchanges what goes after them.#starts a comment. Python ignores it, humans read it. Explain why.- Errors tell you the type of problem (last line) and where it is (line number). Fix the first one, then run again.