Your First Python Program
After this lesson, you will be able to:
- Run real Python code in your browser — no install required
- Use print() to display text and the results of calculations
- Do math in Python with +, -, *, /, **, and parentheses
- Work with strings — quotes, concatenation, and len()
- Recognize the four most common beginner syntax mistakes (and avoid them)
What does this print?
#What Is Python and Why Should You Care?
Python is a programming language — a way to write instructions that a computer can understand and execute. It was created in 1991 by Guido van Rossum (he named it after Monty Python, the comedy group, not the snake).
Here is why so many people choose Python:
-
It reads like English. Compare these two lines that do the same thing:
- Java:
System.out.println("Hello, world!"); - Python:
print("Hello, world!")
Python wins. Every time.
- Java:
-
Somebody has usually done the hard part already. Want to read a spreadsheet, send an email, make a chart, or download a web page? A free, ready-made tool exists for each, and using one is a single line. Thousands of people have built these and given them away.
-
You are never stuck for long. Because so many people use Python, almost every question you will hit has already been asked and answered by someone else.
#Run Your First Python in 30 Seconds
print("Hello, world!")You just ran your first Python program. Output appears under the code. No install, no terminal, no setup.
print(...)is a built-in function that displays whatever you give it."Hello, world!"is a string — text wrapped in quotes.- The result shows up below the block (your "stdout").
Try changing the text inside the quotes — click in the code, edit, and Run again. Make it say your name. This is your laboratory for the next 53 lessons.
#The Python REPL: Your Interactive Playground
You're already using one. Every Run button in this course IS a REPL — paste any Python expression, hit Run, see the result. Try a few:
print("Hello, world!")
print(2 + 2)
print(100 * 365)>>> prompt in your terminal. The behaviour is identical — it's the same Python.The REPL is for experimentation. Calculator math, string fiddling, quick sanity-checks. By lesson 53 you'll use it constantly — to test a snippet before pasting it into a real file.
#A Multi-Line Python Program
A real program has multiple lines. Python runs them top-to-bottom, one at a time. Try this:
# My first Python program
print("Hello, world!")
print("I am learning Python.")
print("This is going to be awesome.")You should see three lines of output. Let's break it down:
#starts a comment. Python ignores everything after#on that line. Comments are notes for humansprint()is a function that displays text on the screen. Whatever you put inside the parentheses gets printed"Hello, world!"is a string — a piece of text wrapped in quotes. You can use single quotes'like this'or double quotes"like this"— Python treats them the same
What does print(3 + 4) output?
3 + 4 first (getting 7), then print() displays the result. Python is smart enough to do the math before printing. Notice there are no quotes around 3 + 4 — it is not a string, it is a math expression.#Python as a Calculator
Python is incredible at math. Here are the basic operators:
| Operator | What It Does | Example | Result |
|---|---|---|---|
+ | Addition | 10 + 3 | 13 |
- | Subtraction | 10 - 3 | 7 |
* | Multiplication | 10 * 3 | 30 |
/ | Division | 10 / 3 | 3.333... |
// | Floor division (rounds down) | 10 // 3 | 3 |
% | Modulo (remainder) | 10 % 3 | 1 |
** | Exponentiation (power) | 2 ** 10 | 1024 |
Let us try a calculation with some big numbers:
# Underscores make long numbers readable -- Python ignores them
people_on_earth = 8_100_000_000
print("People on Earth:", people_on_earth)
# If everyone drank 2 litres of water a day, how many litres per day?
litres_per_day = people_on_earth * 2
print("Litres per day:", litres_per_day)
# That is a big number. How many Olympic pools (2,500,000 litres each)?
pools = litres_per_day / 2_500_000
print("Olympic pools per day:", round(pools))
# Python follows order of operations (PEMDAS)
result = 2 + 3 * 4
print("2 + 3 * 4 =", result) # 14, not 20
# Use parentheses to change the order
result2 = (2 + 3) * 4
print("(2 + 3) * 4 =", result2) # 20Notice a few things:
- You can use underscores in numbers to make them readable:
1_700_000_000_000is the same as1700000000000 - Python follows standard math order of operations (multiplication before addition)
- You can print multiple things by separating them with commas
What does print(7 / 2) output? And print(7 // 2)?
#Strings — Working with Text
Most programs handle text somewhere — names, messages, addresses, whole documents. Here is how Python handles text:
# Strings are text wrapped in quotes
greeting = "Hello"
name = "Aditi"
# Concatenation -- joining strings together
message = greeting + ", " + name + "!"
print(message) # Hello, Aditi!
# String multiplication -- yes, this works!
print("Ha" * 3) # HaHaHa
print("-" * 40) # prints a line of 40 dashes
# The len() function tells you how many characters a string has
prompt = "Explain quantum physics like I am five"
print("Prompt length:", len(prompt)) # 38
# Strings are case-sensitive
print("python" == "Python") # False
print("python" == "python") # True#Code Playground
Time to experiment. Run this code, then try the challenges in the comments:
Tests · Try each challenge one at a time. Run after each change to see your result!
#Common Beginner Mistakes
Everyone makes these. Here is how to fix them:
# Wrong -- Python thinks Hello is a variable name
print(Hello) # NameError: name 'Hello' is not defined
# Right -- wrap text in quotes
print("Hello") # Hello# Wrong -- started with double, ended with single
print("Hello') # SyntaxError: mismatched quotes
# Right -- match your quotes
print("Hello") # or print('Hello')# Wrong -- Print with capital P does not exist
Print("Hello") # NameError: name 'Print' is not defined
# Right -- lowercase print
print("Hello")# Wrong in Python 3 (this is Python 2 syntax)
print "Hello" # SyntaxError: Missing parentheses
# Right -- print is a function, needs parentheses
print("Hello")These errors are not failures — they are how you learn. Every programmer in the world has made every one of these mistakes. The key is reading the error message. Python tells you what went wrong and even points to the exact line.
Which command runs a Python file named myfile.py from the terminal?
Interactive Lab
Step through Python loops line by line and watch exactly what each iteration does — perfect for building mental models
#Key Takeaways
- Python is a good first language — it reads like English, ready-made tools exist for most jobs, and help is easy to find
- The REPL lets you experiment instantly — type
python3in your terminal and start coding. No setup, no files needed print()displays output. It is the most basic and most useful function. You will use it in every program you write- Python is a powerful calculator — it handles
+,-,*,/,//(floor division),%(remainder), and**(power) - Strings are text in quotes — you can join them with
+, repeat them with*, and measure them withlen()
This program was copy-pasted from an old tutorial and uses Python 2 syntax. Fix it so it runs on Python 3 and prints the greeting.
Hello, world! I am learning Python 3.
What does REPL stand for?
#Appendix: Installing Python Locally (Optional)
#On Mac
python3 --version
Python 3.12.4. If you want the latest version, install via Homebrew: brew install python@3.12.#On Windows
- Go to python.org/downloads and click Download Python 3.x
- Run the installer. Check the box "Add python.exe to PATH" at the bottom — this is the #1 mistake beginners make on Windows
- Open Command Prompt or PowerShell and run
python --version
#On Linux
Most distros ship with Python 3. Open a terminal:
python3 --version
sudo apt install python3 python3-pip python3-venv (Ubuntu/Debian) or sudo dnf install python3 python3-pip (Fedora).#Verify your install
python3 -c "print('Hello from local Python!')"
Hello from local Python!, you're set up.#Once you have Python locally: virtual environments
python3 -m venv .venv
source .venv/bin/activate # macOS / Linux
.venv\Scripts\activate # Windows
pip install requests # installs into THIS project only
deactivate. The Modules & Packages lesson (order 20) covers this in depth.