Home / Programming / Python first steps
Programming · Python
Python: Your First Program
Python is one of the friendliest programming languages, and one of the most used. Four short lessons take you from nothing to a working program.
~12 min4 lessonsExample output for every snippet5-question quiz
01
Running your first line
The quickest way to start: open python.org/shell in your browser. To install it, download Python from python.org/downloads, then open the IDLE app that comes with it.
print("Hello, world!")Hello, world!
print() shows whatever you put inside the brackets. Text goes in quotes; numbers don't.
02
Variables store values
name = "Sam"
age = 29
print(f"Hi {name}, next year you'll be {age + 1}")Hi Sam, next year you'll be 30
=means “store this in”, not “equals”.- An f-string (with an
fbefore the quote) drops variables into text using{ }. - Common types: text (str), whole numbers (int), decimals (float), and
True/False(bool).
answer = input("What's your name? ") # waits for the user to type
print("Nice to meet you, " + answer)03
Making decisions with if
temp = 31
if temp > 30:
print("It's hot!")
elif temp > 15:
print("Nice weather.")
else:
print("Bring a coat.")It's hot!
Indentation matters. Python uses the spaces at the start of a line to know what belongs inside the if. Use 4 spaces, and end the if line with a colon.
Comparisons: == equal, != not equal, >, <, >=, <=.
04
Repeating with loops
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print("I like", fruit)
for i in range(1, 4):
print(i, "squared is", i * i)I like apple
I like banana
I like cherry
1 squared is 1
2 squared is 4
3 squared is 9
range(1, 4) counts 1, 2, 3: it stops before the second number.
Try it · predict the output