Lesson 1: Setup and Your First Program
🎧 Listen to this lesson
Python is the world's most popular first programming language: readable enough to learn in school, powerful enough to run Instagram's servers and NASA's data pipelines. This lesson gets you from zero to a running program, and — just as important — teaches you how to react when a program refuses to run. By the end you will know what a program is, how to print text and numbers, how Python does maths, and how to read an error message like a detective instead of panicking.
Setting up
Install Python 3 free from python.org, or skip installing entirely with an online editor such as Replit. Check the install worked by opening a terminal (Terminal on Mac, Command Prompt or PowerShell on Windows) and typing python3 --version (on some Windows setups it is python --version). You should see a reply like Python 3.12.3 — the exact numbers do not matter, as long as the first one is 3. Write code in a proper editor — VS Code is free and excellent — because its syntax highlighting colours keywords, strings and comments differently, which helps your eyes catch mistakes before Python does. Save Python files with the .py ending, then run one from the terminal with python3 myfile.py.
What a program is
A program is a sequence of instructions that the computer executes top to bottom, one at a time. Python reads your file, translates it, and carries out each instruction in order. The instruction you will use most at first is print(), which displays values on the screen. Tradition says your first program prints a greeting:
print("Hello, world!")
print("My name is Ada.")
print(2 + 3 * 4)
Run it. Expected output:
Hello, world!
My name is Ada.
14
Three things to notice. First, text inside quotes is a string — Python displays it exactly as written. Second, 2 + 3 * 4 has no quotes, so Python treats it as maths and does the multiplication before the addition, exactly like school maths: the answer is 14, not 20. Third, each print starts a new line.
Numbers and the maths operators
Python is a perfectly good calculator. The operators are + (add), - (subtract), * (multiply), / (divide), // (whole-number division), % (remainder) and ** (power). Here they are in action:
print(10 / 3) # normal division always gives a decimal
print(10 // 3) # floor division: how many whole 3s fit in 10?
print(10 % 3) # remainder after dividing 10 by 3
print(2 ** 10) # 2 to the power 10
Output:
3.3333333333333335
3
1
1024
The long decimal is not a bug — computers store decimals in binary and cannot represent 10/3 exactly, so you get the closest value they can hold. Two operators deserve a memory hook: // answers "how many full groups?", and % answers "what is left over?". Splitting 10 sweets among 3 friends uses both: each friend gets 10 // 3 = 3 sweets, and 10 % 3 = 1 sweet is left over.
Comments: notes the computer ignores
Anything after a # is a comment — Python skips it completely. Comments are for humans: future you, your teacher, your teammates.
# This whole line is a note; Python ignores it
print("7 * 6 =", 7 * 6) # print can mix text and values
Output:
7 * 6 = 42
Notice that print accepted two items separated by a comma: the string "7 * 6 =" and the value 7 * 6. Python printed them side by side with a space between. Good comments explain why the code does something, not what it obviously does — print("7 * 6 =", 7 * 6) needs no comment saying "this prints", but a strange calculation deserves one explaining its purpose.
Worked example 1 — a program that introduces you
Let us build a complete first program step by step.
Step 1. In your editor, create a new file and save it as intro.py. Step 2. Type this code:
print("Hi! I'm learning Python.")
print("Today is a great day to code.")
# The two lines below report the week in numbers
print("Days in a week:", 7)
print("Hours in a week:", 7 * 24)
Step 3. Run it from the terminal with the command python3 intro.py. Expected output:
Hi! I'm learning Python.
Today is a great day to code.
Days in a week: 7
Hours in a week: 168
Step 4. Experiment. Change the messages, add a line printing 60 * 24 labelled "Minutes in a day:", save, and run again. Deliberately breaking and fixing small things is not cheating — it is exactly how programmers learn what each instruction does.
Worked example 2 — drawing a banner with string repetition
Multiplying a string by a number repeats it: "ha" * 3 gives "hahaha". That trick draws borders without typing forty characters by hand:
print("+" + "-" * 22 + "+")
print("| My first program! |")
print("+" + "-" * 22 + "+")
Output:
+----------------------+
| My first program! |
+----------------------+
The top border is built from three pieces joined by +: a "+", then 22 dashes, then another "+". Joining strings with + is called concatenation. The middle line was typed to be exactly the same width — count the characters in the output and you will find 24 on every line. Programs that draw with text (menus, boxes, charts) all rest on this idea: build lines piece by piece and keep the widths consistent.
Errors are normal: read the message
Every programmer — every single one — sees error messages daily. An error is not a verdict on you; it is Python telling you precisely where it got stuck. Read the last line of the error first: it names the error type and the line number. The two you will meet immediately:
- SyntaxError — Python could not even parse your code, often a missing quote or bracket.
print("Hello)fails with "unterminated string literal" because the string never ends. - NameError — you used a name that does not exist.
Print("hi")fails because Python is case-sensitive: the function isprint, notPrint.
print("Hello)
Running that line raises a SyntaxError pointing at line 1. The fix is one character. Debugging is detective work with a reliable pattern: reproduce the error, read the message, form a hypothesis about the cause, test a small fix, and run again. Lesson 7 turns this into a full method.
# starts a comment, and an error message is a set of fixing instructions — read its last line first.Common mistakes
- Capitalising
print.PrintandPRINTare different names fromprintand raise NameError. Python names are case-sensitive everywhere. - Forgetting a closing quote or bracket.
print("Hello)orprint(2 + 3leaves Python waiting for something that never comes — a SyntaxError. - Quoting your maths.
print("2 + 3")prints the text2 + 3, not 5. If you want the answer, leave the quotes off; if you want the words, add a separate item:print("2 + 3 =", 2 + 3). - Typing code in a word processor. Word processors insert curly "smart quotes" that Python rejects. Always type code in a code editor or plain-text editor.
- Mixing up
/and//.10 / 3is 3.333...,10 // 3is 3. Pick the one the problem actually asks for.
Real-World Challenge
The task. A café wants a little program that prints a receipt. Using only print and arithmetic (variables arrive in Lesson 2), print a receipt for 2 coffees at 3.00 each and 1 muffin at 2.50, with a total that Python calculates — never type the total yourself, because a total you type can disagree with the items above it.
Worked solution.
print("=== CORNER CAFE ===")
print("Coffee x2 ....", 2 * 3.00)
print("Muffin x1 ....", 1 * 2.50)
print("TOTAL ........", 2 * 3.00 + 1 * 2.50)
Output:
=== CORNER CAFE ===
Coffee x2 .... 6.0
Muffin x1 .... 2.5
TOTAL ........ 8.5
How it works. Each line pairs a label with a calculation, so the numbers on the receipt are computed from the prices and quantities — change 3.00 to a new coffee price and every line, including the total, stays correct. The total line repeats the two multiplications and adds them; that repetition is exactly the inconvenience variables (Lesson 2) exist to remove. The plain 6.0 and 8.5 look unfinished for money — Lesson 4 teaches the :.2f formatting that prints them as 6.00 and 8.50.
Extension. Add two more lines after the total: a tip line computing 10% of the total, and a grand-total line adding it on. With the prices above, the tip is 0.85 and the grand total is 9.35 — check that your program prints exactly those values.
Going further — expressions, types and the calculator mindset
Two habits separate people who *use* Python from people who fight it. The first is treating the interpreter as a laboratory: any expression can be tried instantly, and Python's answers about precedence and types are always one experiment away. Multiplication binds tighter than addition, parentheses override everything, and the same symbol behaves differently per type:
print(2 + 3 * 4)
print((2 + 3) * 4)
print(type(3), type(3.0), type("3"))
Output:
14
20
<class 'int'> <class 'float'> <class 'str'>
The third line deserves a long look. 3, 3.0 and "3" print alike and behave completely differently — one divides exactly, one carries decimals, one refuses arithmetic entirely and concatenates instead. type() is your flashlight: whenever a result surprises you, ask the values what they are before asking why they misbehaved — Lesson 7 turns that instinct into a method. The second habit is saving files rather than living in the interactive prompt: the REPL is for experiments and quick checks, files are for anything you would grieve losing. A healthy workflow alternates — prototype three lines interactively, commit the working idea to the file, run the file, return to the prompt with the next question. Everything in this course is written that way, including the code in this lesson.
🎬 Watch & Learn
Prefer video? These searches surface top tutorials on this lesson's topic:
- Python installation & your first program — full beginner walkthrough (freeCodeCamp style) — pick a recent, well-reviewed video
- Corey Schafer: getting started with Python — setup and running code — pick a recent, well-reviewed video
- CS50-style introduction to programming with Python — pick a recent, well-reviewed video
💪 Practice Zone
Try these yourself — hints are included, answers are not. Run your code in any Python environment.
- Hello, three ways — Write a program that prints your name, your grade, and your favorite hobby — each on its own line.
Hint
Use threeprint()calls, one per line. - Calculator warm-up — Print the answers to
15 + 27,100 - 64,7 * 8, and144 / 12. No variables needed.Hint
print()can do math directly:print(15 + 27). - Fix the broken program — This program crashes. Find and fix the bug, then run it:
print("Hello, world!)Hint
Read theSyntaxErrormessage — it tells you the line number. Check the quotes. - Comment detective — Copy any example from this lesson and add a
#comment above every line explaining what it does in your own words.Hint
A comment starts with#and Python ignores it — it's a note for humans.
🌍 Real World
Python runs the back end of Instagram, powers NASA data tools, and automates boring office work worldwide. This lesson's skill — installing Python and running a script — is literally step one of every Python job: data analysts run scripts like yours every morning to clean spreadsheets before anyone arrives.