Python Basics — Your First Program
Your First Program
It’s time to write actual code. By the end of this page, you’ll have created and run your first Python program.
Hello World in the REPL
The fastest way to run Python code is in the REPL. Open your terminal and start Python:
python3
Now type:
>>> print("Hello, World!")
Output:
Hello, World!
Congratulations — you just ran your first Python command! Let’s break it down:
print()is a function — it displays output to the screen"Hello, World!"is a string — text enclosed in quotes- The parentheses
()pass the string to theprintfunction
Hello World as a .py File
The REPL is great for quick experiments, but real programs live in files. Let’s create one.
Step 1: Create the File
Open your text editor and create a file called hello.py:
# My first Python program
print("Hello, World!")
print("Welcome to Python programming!")
print("Let's learn together.")
Step 2: Save the File
Save it anywhere you like — your Desktop, a project folder, etc.
Step 3: Run the File
Open your terminal, navigate to the file’s location, and run:
python3 hello.py
Output:
Hello, World!
Welcome to Python programming!
Let's learn together.
That’s it. You wrote a program, saved it to a file, and ran it from the terminal.
Understanding the Execution Flow
Python reads your file from top to bottom, executing each line in order:
print("Line 1") # Runs first
print("Line 2") # Runs second
print("Line 3") # Runs third
Output:
Line 1
Line 2
Line 3
There are no surprises — Python does exactly what you tell it, in the order you tell it. (We’ll learn how to change this order with conditions and loops later.)
Adding Comments
Lines starting with # are comments — Python ignores them completely. They exist to help humans understand the code:
# This is a comment — Python ignores this line
print("This line runs") # This comment is at the end of a line
Output:
This line runs
A More Interesting Program
Let’s make something slightly more useful — a program that does a calculation:
# Simple calculator
length = 10
width = 5
area = length * width
print("Rectangle Calculator")
print("Length:", length)
print("Width:", width)
print("Area:", area)
Output:
Rectangle Calculator
Length: 10
Width: 5
Area: 50
You just used variables (length, width, area) and arithmetic (* for multiplication). We’ll cover these in depth later — for now, just appreciate how readable the code is.
Running Scripts from Terminal
Here’s a quick reference for running Python scripts:
| Platform | Command |
|---|---|
| macOS / Linux | python3 script.py |
| Windows | python script.py |
| Any (explicit) | python3 -u script.py (unbuffered output) |
The Shebang Line (macOS / Linux)
On Unix systems, you can add a shebang line to make your script directly executable:
#!/usr/bin/env python3
print("Hello from a shebang script!")
Then make it executable and run it:
chmod +x hello.py
./hello.py
Output:
Hello from a shebang script!
The shebang line (#!/usr/bin/env python3) tells the operating system which interpreter to use. This is optional and mainly useful for command-line tools.
Common Beginner Mistakes
| Mistake | What Happens | Fix |
|---|---|---|
print "Hello" |
SyntaxError | Use print("Hello") — parentheses required in Python 3 |
Print("Hello") |
NameError | Python is case-sensitive — use lowercase print |
Missing quotes: print(Hello) |
NameError | Strings need quotes: print("Hello") |
| Wrong file extension | May not run | Save as .py, not .txt or .py.txt |
| Not in the right directory | File not found | cd to the folder containing your script |
File Structure Best Practices
As you learn Python, organize your practice files:
python-practice/
├── ch01-introduction/
│ ├── hello.py
│ ├── calculator.py
│ └── greeting.py
├── ch02-variables/
│ ├── variables.py
│ └── types.py
└── ch03-control-flow/
├── if_example.py
└── loop_example.py
This keeps things organized and easy to find.
Summary
- Use
print()to display output - Python scripts are
.pyfiles - Run scripts with
python3 filename.py - Python executes code top to bottom
- Comments start with
#and are ignored by Python - The shebang line (
#!/usr/bin/env python3) is optional on Unix systems
Next up: Practice Exercises →