Python Tutorial for Beginners: Your First Program
Python is a general-purpose programming language known for code that reads close to plain English. It is used for automation scripts, web back ends, data analysis and most of today's AI tooling. This chapter gets a program running before we explain any theory.
What a Python program is
A Python program is a plain text file, usually ending in .py. The Python interpreter reads that file from the top and executes one statement at a time. There is no separate compile step you have to run, and no main function is required: the first line of the file is the first thing that happens.
The simplest useful statement is a call to the built-in print() function. Whatever you put between the parentheses is written to the screen, followed by a new line. Text goes inside quotes; numbers and calculations do not.
Expected output
Hello, TetSharing! 2 + 3 = 5 Python can repeat text: hahaha
Press Run above. The first time, your browser downloads Pyodide, a full Python interpreter compiled to WebAssembly, so the first run takes a few seconds. After that, runs are almost instant. Try changing the greeting or the numbers and run it again.
What happened
print("Hello, TetSharing!")printed the text exactly as written, without the quotes.print("2 + 3 =", 2 + 3)received two values separated by a comma. Python evaluated2 + 3first, then printed both values with one space between them."ha" * 3repeats a piece of text three times. Python decides what an operator does based on the types of the values around it.
Running Python on your own computer
When you want to keep your work, install Python 3 from python.org (macOS and Linux often have it already). Save code in a file such as hello.py, open a terminal in that folder and run python3 hello.py (on Windows, py hello.py). Running python3 with no file starts the interactive interpreter, where each line runs as soon as you press Enter. It is a handy calculator and a quick way to test an idea.
Common mistakes
- Forgetting the quotes:
print(Hello)makes Python look for a variable namedHelloand fails with aNameError. - Mismatched quotes such as
"Hello'. Start and end a piece of text with the same kind of quote. - Writing
Printwith a capital P. Python is case-sensitive, soPrintandprintare different names.
Summary
- A Python program is a text file that runs from top to bottom.
print()shows values; separate several values with commas.- Text is written in quotes; numbers and expressions are not.
Quiz: Get started