Pylerno
English

Introduction

Learn Python from your first line of code to a tested command-line application. Start here if you have never programmed before, or use the chapter index to look up a specific language feature.

On this page

What is Python?

Python is a programming language. You write instructions as text, and a program called the interpreter carries them out. A Python program can calculate a bill, organize a folder of files, or decide which tasks are overdue.

An instruction can be as small as displaying a message. In the example below, predict what will appear on the screen, then select Run.

python
print("Hello, Python!")

The characters inside the quotes form a string, Python’s name for text. The parentheses pass that string to the built-in function print. The output is the message itself, without its quotation marks.

How a program runs

Python executes these statements from top to bottom. Read them in order and keep track of the value that each name refers to.

python
price = 12
quantity = 3
total = price * quantity
print(total)

The first two lines give names to numbers. The third line multiplies their values and stores the result in total. Only the last line displays anything. The output is 36. If you change the quantity to four, which line computes the new result? Change the quantity in your local Python file and run it again.

When your first program stops

Suppose you type print(Hello) and see NameError. Python read Hello as a name and tried to find its value. This program never gave that name a value. If you meant the word itself, surround it with quotes. If you meant to reuse a message, give it a name first.

python
message = "Hello"
print(message)
print("message")

Predict the two lines before running the example. The first call looks up the name and displays Hello. The second displays the seven letters message. When a program stops, read the last line of its error report and look for the named line in your file. Fix one thing and run the same example again.

Choose your starting point

Practice as you read

For each worked example, predict its output, run it, and explain one line in your own words. Then attempt the exercise before revealing the solution.

Most early examples run directly on the page. For examples involving a terminal, multiple files, or a database, download the example files, extract them, and follow the commands in the article.

A path from beginner to developer

  1. Read and predict. Finish the foundations and control-flow chapters. Write a small price calculator and explain which branch runs for a zero price.
  2. Organize data. Learn collections, functions, and classes. Build a grade report with an empty-class case and a failing-grade case.
  3. Make code dependable. Read the files, validation, and testing chapters. Save a report, load it again, and write a test for a malformed input file.
  4. Ship a program. Build the TaskFlow capstone. Continue through packaging, integration tests, continuous integration, transactions, and releases. Verify each change against a fresh installation.
  5. Reason about the runtime. Trace method lookup, test a structural protocol, and explain what survives task cancellation. Write a test that checks whether the remaining tasks clean up after a failure.

Check your understanding

What does this program print?

python
print("2 + 3")
Choose one answer

Search documentation

Share this page