Your First Python Program
This lesson is a first Python program you can run on your own machine: printing text, storing values, reading input, branching with if/else, looping, and a small number-guessing game with the full code at the end. If the ideas (variables, conditions, loops) are still foggy, skim what programming really is first. If you are still deciding whether Python is the right first language, pick it as a default for scripts and stay with it through this file. Tools and installers change; check the official Python documentation for your operating system when a click path looks different.
Installing Python
You want a Python 3 interpreter, not an old Python 2 leftover. On many systems you can open a terminal and type:
python3 --version
If you see a version number that starts with 3, you already have it. If the command is not found, install Python 3 from the official Python website for your OS, or use the package source your operating system documents. During Windows setup, choose the option that puts Python on your PATH if you are offered it. After installing, close and reopen the terminal, then run python3 --version again.
You will also need a text editor. Any editor that saves plain text is enough. Avoid a word processor that inserts fancy quotes. Save files with a .py ending.
Running a file
Create a folder for practice. Inside it, create hello.py with this single line:
print("Hello, Python.")
In the terminal, change into that folder and run:
python3 hello.py
You should see Hello, Python. If you see an error instead, read it from the bottom up. Reading error messages is a companion skill; do not skip the traceback. Common first-day issues: you are in the wrong folder, the file is named hello.py.txt, or you launched Python 2 as python. The command that worked for --version is the command to use for the file.
You can also start an interactive session with python3 and type lines one at a time. That is handy for tiny experiments. Programs you want to keep belong in files.
print and variables
print sends text to the terminal. A variable holds a value so you can use it again:
name = "Amina"
count = 3
print("Hello,", name)
print("You have", count, "tries.")
The equals sign is assignment, not a math claim that both sides were already equal. After count = 3, the name count refers to the integer 3. You can assign again: count = count - 1.
Strings use quotes. Integers do not. 3 and "3" are different. Mixing them in arithmetic will raise a TypeError, which is the interpreter refusing to guess.
input
input pauses, shows a prompt, and returns whatever the user typed as a string — even if they typed digits.
raw = input("How many guests? ")
print("You typed:", raw)
To treat the answer as a number, convert it:
raw = input("How many guests? ")
guests = int(raw)
print("Tables needed:", guests)
If they type twelve, int will fail. That is expected. Later you can catch that error. For this lesson, type digits when the program asks for a number.
if / else
Use a condition when the next step depends on a value:
guests = int(input("How many guests? "))
if guests < 1:
print("Enter at least one guest.")
elif guests > 8:
print("We will split across two tables.")
else:
print("One table is enough.")
Indentation is part of the syntax. The lines under if must be indented one level, consistently. Spaces are the usual choice; do not mix tabs and spaces. Python will raise IndentationError if the layout is inconsistent. The colon at the end of the if line starts the block.
A loop
A while loop repeats as long as a condition is true. This countdown prints 3, 2, 1:
n = 3
while n > 0:
print(n)
n = n - 1
print("Done.")
If you forget n = n - 1, the loop never ends. Stop it with Ctrl+C in the terminal. A for loop is nicer when you already have a collection:
for n in [3, 2, 1]:
print(n)
The guessing game below uses while because we do not know how many tries the player will need.
A number-guessing game
The program picks a secret integer from 1 to 10, then asks until the guess is correct or the player runs out of tries. It uses random.randint from Python’s standard library — no extra install.
import random
secret = random.randint(1, 10)
tries_left = 3
print("I picked a number from 1 to 10.")
while tries_left > 0:
raw = input("Your guess: ")
guess = int(raw)
if guess == secret:
print("Correct.")
break
if guess < secret:
print("Too low.")
else:
print("Too high.")
tries_left = tries_left - 1
print("Tries left:", tries_left)
if tries_left == 0:
print("The number was", secret)
Save it as guess.py and run python3 guess.py. break leaves the loop early on a correct guess. After the loop, tries_left == 0 means they never hit break (or they used the last try incorrectly — if they guess right on the last try, break happens before the counter hits zero, so the reveal does not print). Trace it once on paper with a secret of 4 and guesses 2, 9, 4.
When you are ready to keep versions of this file as you change it, Git and GitHub basics is the next practical tool, not a requirement for the game itself.
Checklist
python3 --versionprints a 3.x version.- A
.pyfile runs withpython3 filename.pyfrom the right folder. - You used
print, a variable,input,int,if/else, and awhileloop. - The guessing game runs end to end; a wrong type still crashes until you add error handling later.
- Indentation is consistent; quotes are straight quotes from a text editor.
Do not add features until the basic game runs. A working 30-line program teaches more than a half-finished “engine.” When it runs, change the range to 1–20 or add a fourth try — one change at a time — and run it again.
Related lessons
Beginner-level guidance; tools and versions change, so check official documentation for details.