RedLectern topic

Python

Learn Python fundamentals, from expressions and control flow to organizing and testing programs.

For: Learners at different starting points, from people new to programming to programmers new to Python.

After this page you can: Write small Python programs, choose suitable data structures, and organize code into reusable parts.

Page reviewed

Part of: Computer Science › Programming

About Python

Python is a general-purpose programming language whose syntax and dynamic typing support scripting and application development. It provides high-level data structures and an object-oriented approach. The official tutorial introduces core language concepts but assumes basic programming knowledge, so it suits learners who can already follow a simple program.

Study by trying small expressions, then reading examples and changing them in an interpreter: tutorial examples are self-contained and recommend hands-on use. MIT course materials offer another structured route with lectures, assignments, and exercises.

Start here

route

I know another language

Use the official tutorial to get oriented to Python, and try its examples in an interpreter as you read.

Start with: The Python Tutorial

Learn the foundations

1.Build a foundation with expressions and control flowTry an expression, then trace how a condition or loop changes the next step in a program.

Try an expression, then trace how a condition or loop changes the next step in a program.

if statement

A statement that selects a branch based on a condition.

for loop

A loop that iterates over items in a sequence.

Try it

What values does `sum(range(4))` add together, and what result does the Python tutorial show?

It adds 0, 1, 2, and 3; the result is 6.

2.Use functions and choose data structuresPractice turning a repeated task into a function, then choose a collection that suits the information you need to represent.

Practice turning a repeated task into a function, then choose a collection that suits the information you need to represent.

function

A named piece of code that can be called, with parameters and a body of statements.

dictionary

A collection of unique keys associated with values.

Try it

The tutorial’s dictionary example stores a value for the key `'jack'`. What value does it retrieve for that key?

It retrieves 4098.

3.Organize code and handle files and errorsFollow one value through a program: where it is defined, how it is saved or read, and what should happen if an operation fails.

Follow one value through a program: where it is defined, how it is saved or read, and what should happen if an operation fails.

  • Input and Output

    The tutorial covers reading and writing files and saving structured data.

  • Errors and Exceptions

    The tutorial distinguishes syntax errors from exceptions and explains handling exceptions.

module

A file containing Python definitions and statements that can be imported.

exception

An error detected while a program is executing.

Try it

Why can `with` be useful when opening a file?

It closes the file after the `with` suite finishes, even if an exception occurs.

4.Model related data and check program behaviorUse a small example to decide whether related data and behavior belong together, then write down what response you expect for a particular input.

Use a small example to decide whether related data and behavior belong together, then write down what response you expect for a particular input.

class

A way to bundle data and functionality and create a type of object.

test case

A check for a specific response to a particular set of inputs.

Try it

A test checks the response to one input. What should it specify?

It should specify the input and the response expected for it.

Latest

No recent updates are available.

See the sources list

Books

Glossary

Every free Python course and book on RedLectern

Free Python online courses

  1. A Gentle Introduction to Programming Using PythonMIT 6.189 · Browse materials
  2. Introduction to Computer Science and Programming in PythonMIT 6.0001 · Browse materials · Video
  3. Introduction to CS and Programming using PythonMIT 6.100L · Browse materials · Video
  4. A Gentle Introduction to Programming Using PythonMIT 6.189 · Open on OCW
How these courses are chosen

Listed as Python courses:

  • those whose title contains “Python”, or
  • those tagged “Python” as one of at most 2 topic tags, or as the first of 3.

Free Python textbooks and books

Show all 43 booksShow fewer books

Questions and comments