First steps: your first Python program
Run Hello World in the interpreter and from a file.
All lessons · Python lessons: learn from scratch
What a program is and what Python does
A program is a list of step-by-step instructions for a computer. Python lets you write those instructions with words close to everyday English. A program called the interpreter reads your text file line by line and carries out each instruction.
Python's strengths: simple syntax, a huge ecosystem of libraries, and the same code runs on Windows, macOS and Linux. That is why beginners and people working on web, data and automation all pick it.
Ways to run code
There are two main ways. The first is interactive mode: type python in a terminal, press Enter, and every line you type is evaluated immediately. It is ideal for small experiments.
>>> 7 * 6 42 >>> "sa" + "lam" 'salam' >>> exit()
The second is file mode: save the code in a .py file and run it with python file_name.py. Real programs are written this way because the file is kept and can be run again.
# greeting.py
name = "Aysel"
print("Welcome,", name)
print("2 + 3 =", 2 + 3)Choosing an editor: VS Code, PyCharm Community or the simple IDLE all work. What matters is saving the file with a .py extension and opening the terminal in the right folder.
Lines, comments and spacing rules
Each command goes on its own line. Anything after # is a comment: Python ignores it, and you use it to leave notes for yourself. In Python the spaces at the start of a line (indentation) carry meaning: lines in the same block must share the same indentation.
# total price of three items
price = 12
count = 3
if count > 2:
print("Bulk order") # 4 spaces = inside the if block
print(price * count)- Use 4 spaces for indentation.
- Never mix tabs and spaces.
- Wrap text in quotes: "like this" or 'like this'.
Start with simple things: show output with print(), change something, run it again. Do not fear mistakes; an error message tells you which line has the problem.