Skip to contentSkip to navigation

Module 01 · Lesson 01

Introduction to Python

Meet Python: what a program is, how the interpreter runs your lines from top to bottom, and how to read an error message without panic.

  • About 8 minutes
  • Beginner friendly
  • 12-question quiz at the end

What you will learn

  • What Python is
  • Your first line
  • Think like a computer
  • Read an error

Meet Python

A programming language is a way to give a computer instructions. Python is one of the friendliest.

  • Reads like English

    Short, clear lines with few strange symbols. If you can read a sentence, you can start reading Python.

  • Free for everyone

    Anyone can download it. It runs on Windows, macOS, Linux and tiny boards like the Raspberry Pi.

  • Named after comedy

    The name comes from the comedy show Monty Python's Flying Circus, not the snake.

Python is all around you

You have probably used something built with Python today. These programs start from the same print() and names you meet in this lesson.

  • AI and chatbotsMost AI models are built and trained with Python. Chat assistants, photo filters and voice helpers all learn from examples fed in by Python code.
  • Apps you useInstagram's servers run on a Python web toolkit. Spotify and Netflix use Python behind the scenes to work out what to suggest next.
  • Space and scienceIn 2019 scientists used Python to turn telescope data into the first picture of a black hole. NASA teams use it to plan and check missions.
  • GamesYou can build your own 2D games with a few hundred lines. The huge space game EVE Online runs much of its logic in Python.
  • Data and sportFootball clubs study every pass of a match, and weather services sort millions of readings. Both find the patterns with Python.
  • Boring jobs, doneTen lines can rename hundreds of photos in a second. People also use Python to fill in spreadsheets and send reminders for them.

Your first line of Python

This is a whole program. Tap each part to see its job.

Part 1 of 4

Tap any part of the code below, or press Next.1 of 4 explored

command:print is a built-in command. It shows whatever is inside the brackets on the screen. Think of it as a megaphone.

Prints Welcome to Python!

Try your first Python code

Pick an example, change it, then press Run. It runs right here in your browser.

Example 1 of 5Hello

Every coder's first program: make Python say something.

Your turn: Change the second line so it says hello to you by name.

Python
print("Hello, World!")
print("I am learning Python!")
Output
Press Run to see what it prints.

Think like a computer: the robot game

A computer never guesses. It follows your steps exactly, in order. Build a program to get the robot to the star.

Level 1 of 3Straight ahead

The robot starts top left, facing right.

1. Add moves

2. Your program (0/12)

Tap the moves above to write your program.

Watch Python run, line by line

Press Step to run one line at a time, the way Python does. Watch the names and the output change.

Ready. Press Step to run the first line.

  1. print("Start")
  2. score = 5
  3. score = score + 1
  4. print("Score:", score)
  5. print("End")

Output so far

Nothing printed yet.

What Python does

Python reads the program from the top. Nothing has run yet.

Names

None yet.

Draw with code

Python has a drawing pen called `turtle`. It walks forward and turns, and draws as it goes. Pick a shape or slide the numbers.

Shape 1 of 5Square

Real Python opens the turtle in its own window, so here the drawing is a preview of what the code does.

Change the numbers

4
120
90
import turtle
t = turtle.Turtle()

for i in range(4):
    t.forward(120)
    t.right(90)

Challenges0 of 3

  • Make a hexagon: 6 sides, and turns that add up to 360 degrees.
  • Make an octagon with 8 sides.
  • Make a flower with 6 circles.

Why Python feels easier

The same job, printing a greeting, in Python and in other languages.

Language 1 of 2Python vs Java

Python1 line
print("Hello, World!")

One line. Almost nothing to memorize before you see a result.

Java5 lines
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Java needs a class, a main method and a semicolon before it can print one sentence.

Quick guesses

Guess first, then read why. A wrong guess is where the learning happens. Nothing here is scored.

Question 1 of 6

1.What does this print?

print(7 * 6)

0 of 6 answered

Common beginner misconceptions

Worried about any of these? Tap a card.

Misconception 1 of 4

Notes

Everything above, in words. Open it when you want the full explanation.

Read the notesOpen

How Python runs a program

A program is a text file of instructions. Each instruction is called a statement. Python is a programming language: a set of words and rules for writing those statements so a computer can follow them. The program that reads your Python and carries it out is the interpreter. On this site the interpreter runs inside your browser, so you can edit any example and press Run.

The interpreter works through your program from top to bottom, one line at a time. It finishes one statement before it starts the next. That simple rule explains most beginner surprises: if a line uses something that only appears further down, Python has not seen it yet.

Try it 1: edit me, then Run
print("Good morning")
print("Python runs this line second")
print("and this one last")
Output
Press Run to see what it prints.

print() is a built-in function: a named tool that does a job. You call it by writing its name, then parentheses, with the thing to show inside. Text goes inside quotes, so Python knows it is text and not an instruction. Text in quotes is called a string.

Python can also work things out before it prints. Without quotes, 3 * 4 is a calculation, and print() shows the answer. With quotes, "3 * 4" is just text, and Python shows it exactly as written.

Try it 2: edit me, then Run
print(3 * 4)
print("3 * 4")
print(10 - 2 + 1)
Output
Press Run to see what it prints.

The output is 12, then 3 * 4, then 9. The symbols +, -, * and / add, subtract, multiply and divide. Python follows the usual maths order, so multiplication happens before addition unless you add parentheses.

Spaces and blank lines

Blank lines between statements do nothing, so you can use them to split a program into chunks that are easier to read. Spaces at the start of a line are different: Python uses them to group lines together, which later lessons cover. Until then, start every statement at the left edge. One extra space in front of a line stops the program with IndentationError: unexpected indent.

Names that remember values

You will often want to keep a value for later. Writing score = 5 makes the name score refer to the value 5. Later lines can use score instead of the number. When a line like score = score + 1 runs, Python works out the right side first using the current value, then stores the new value under the same name. The next lesson on variables covers this properly; for now it is enough to see that values can change as the program runs, one line at a time.

When something goes wrong

If Python cannot run a line, it stops and prints a traceback. A traceback looks alarming, but it is a helpful report. Read it from the bottom: the last line names the kind of problem and gives a short message. The line above it shows which line of your code caused it.

Try it 3: edit me, then Run
print("Start")
print(totl)
print("End")
Output
Press Run to see what it prints.

This program prints Start, then stops with NameError: name 'totl' is not defined. End never prints, because Python stops at the first error. The fix is to spell the name correctly or create it first. Two other errors you will meet early are SyntaxError, which means Python could not understand how a line is written (often a missing quote or bracket), and TypeError, which means a value was used in a way its type does not allow, such as adding a number to text.

Comments

A line that starts with # is a comment. Python skips it. Comments are notes for people who read the code, including you next week.

Python is case sensitive: print works, Print does not. Small details like quotes, brackets and capital letters matter, and the error messages will point you to them.

Syntax

The general shape of the statements in this lesson.

syntax.py
print("text")          # show text
print(7 + 5)           # show the result of a calculation
print("a", "b")        # show several values, separated by a space
name = "Ada"           # give a value a name
# a comment: Python skips this line
Output
Press Run to see what it prints.

Worked examples

Read the problem, run the code, then read why it works.

Example 1 of 2

A short program, line by line

Beginner

Write a program that greets the reader, shows how many minutes are in a day, and then says goodbye. Predict the output before you run it.

Python
print("Hello!")
print("Minutes in a day:")
print(24 * 60)
print("Goodbye.")
Output
Press Run to see what it prints.

Predict first

How it works(open after your guess)

Python runs the four statements in order. The first, second and fourth show text exactly as written, because it is inside quotes. The third has no quotes, so Python calculates 24 * 60 first and prints the answer, 1440. Changing the order of the lines changes the order of the output.

Common pitfalls

Mistakes people make with this topic, and what to write instead.

Pitfall 1 of 3

Forgetting the quotes around text

Spot the bug

Can you see what is wrong? Run it to watch it go wrong, then open the fix.

Buggy code: run it
print(Hello)
Output
Press Run to see what it prints.
Show what goes wrong and the fix

What happens

NameError: name 'Hello' is not defined

Write this instead

print("Hello")

Why it happens

Without quotes, Python treats Hello as a name it should look up, not as text to show. No value called Hello exists, so it stops.

In the real world

Where this shows up in software people use every day.

  1. Working out a weekly budget

    Everyday life

    Python is often used for small personal jobs, such as checking how far a weekly budget stretches before you go shopping.

    Why Python fits

    Each line does one small step, and Python runs them in order. Naming the budget once means you can change it in one place and run the program again for a new answer.

    Python
    budget = 50
    spent = 12 + 8 + 15
    print("Spent:", spent)
    print("Left:", budget - spent)
    Output
    Press Run to see what it prints.

Key takeaways

Tick each one you could explain to a friend.

0 of 5

Common questions

Guess the answer first, then open it.

Do I need to install anything?

Not for this course. Every example and Lab runs in your browser. Later you can install Python from python.org to run programs on your own computer.

Is Python slow because it is interpreted?

For learning and for most everyday programs, Python is plenty fast. Heavy number crunching often uses libraries written in faster languages, which Python calls for you.

Do I have to remember all of this?

No. Programmers look things up every day. The Syntax box and the lesson cheat sheet at the end hold every line this lesson used.

Your notes

Sign in to keep private notes alongside this lesson.

Sign in to take notes

Where to go next

The quiz and the Lab live on their own pages, so this lesson stays about learning.

Finished this lesson?

Create an ID or sign in to save your progress across devices.

Create ID to save
Lessons done in this module0/4 complete
Quiz

Check what you learned

12 questions, with an explanation after every answer. A verified attempt counts towards opening the module Lab.

Lab

Keep going in the Lab

7 tasks on this lesson, from writing code to tracing it by hand. Practice only, no XP.

Review

Spaced review

Questions you miss in a quiz come back on a spaced schedule, so what you learned here sticks.

This lesson at a glance

The 5 lines of code this lesson uses, each with a short note. Keep it open while you do the quiz and the Lab.

What else you can search for and explore

Curious to go further? Each card opens a web search in a new tab.

References

Look things up here whenever you need the exact details.