S/Shortutorial
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!")

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}")
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.")

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)

range(1, 4) counts 1, 2, 3: it stops before the second number.

Try it · predict the output

NEXTKeep learning