Python Get Started
To run a Python program you need the Python interpreter: a program that reads your code and carries it out, line by line. On your own computer you would install Python from python.org and type your code in a file that ends with .py. In this tutorial the online editor does everything for you: the interpreter runs inside your browser, the file is called main.py, and the Run button starts it.
Your first program
Programs usually start by showing something on the screen. In Python, print shows text in the terminal:
print("Hello, World!")
print("I am learning Python.")Hello, World! I am learning Python.
Each print writes one line. The text between the quotes is called a string. The brackets after print hold what you want to print; we say that you call the function print and pass it the string.
Notice what is not there: there is no main function to write, no semicolons and no curly brackets. Python starts at the first line of the file and goes down to the last. When it reaches the end, the program is finished.
How the Run button works
When you press Run, the editor types a command in the terminal for you:
python3 main.py
python3 is the interpreter, and main.py is the file it should run. You can type the same command yourself, or run another file of your project, for example python3 games.py. The terminal behaves like the terminal on a Linux computer, so the commands you learn here work on real computers too.
The interactive interpreter
Type python3 alone in the terminal and press Enter. The interpreter starts and shows its prompt, three arrows: >>>. Now every line you type is run at once, and the value of an expression is shown immediately:
>>> 2 + 3 5 >>> "Py" + "thon" 'Python' >>> exit()
This is the fastest way to try a small idea, such as how a function behaves or what an expression gives. Type exit() or press Ctrl+D to leave the interpreter and return to the terminal.
When something is wrong
If your code breaks one of Python's rules, Python stops and prints an error message called a traceback. It names the file and the line, and the last line says what kind of error it is. Our editor adds a short hint in plain English and marks the line in the code. Fix the line and press Run again; making and fixing mistakes is a normal part of programming.
Practice exercises
- Write a program that prints three lines: your name, your age and your favourite food.
- Start the interactive interpreter with
python3and work out365 * 24there. - Remove one of the quotes in a
printline, run the program and read the error. Then fix it.