Course curriculum
Python · LESSON 1 OF 5

Your first Python program

Run a Python file, print a result and understand the difference between source code and output.

About 7 minutes · Beginner

Set up a small workspace

Install Python 3 from python.org and create a file named hello.py in a new folder. A source file is plain text containing instructions; its .py extension tells tools which language it uses.

Open a terminal in that folder. Run python3 hello.py on systems where Python uses that command, or py hello.py with the Windows launcher. The command must run in the directory containing your file.

Print a message

print is a built-in function. The parentheses contain the value passed to it. Quotes mark text, also called a string; Python does not print the surrounding quotes.

A line beginning with # is a comment for readers. Use comments to explain a decision rather than repeat the code.

Python
# A small greeting
print("Hello, student!")
print(2 + 3)
Expected output
Hello, student!
5

Read errors as feedback

If you leave out a quote or parenthesis, Python reports a syntax error. Read the filename and line number, inspect that line and the line before it, then run the file again.

Do not put terminal commands such as python3 hello.py inside the Python file. The terminal starts the interpreter; the file contains Python instructions.

Try it yourself

Print your subject name on one line and the result of 12 multiplied by 3 on the next line.

Show a possible solution
Python
print("Computer Science")
print(12 * 3)

Keep exploring: official Python documentation.

0 of 5 completed on this device