Log in to save your progress.
Home
Next >>
Note! The window is too narrow to use Pythonlab.dev. Use a device with a keyboard!

Reference

Click on a function to read more.
Basics part 1
Syntax
Short description
break
break interrupts code that is repeated
continue
continue interrupts a repetition and starts the next
elif
elif is a combination of else and if
else
else can be used last in an if-statement
float()
Converts to type float (decimal number)
if
if controls the code to do different things depending on a condition
input()
Receives input from the user
int()
Converts to type int (integer)
randint()
Generates a random integer
round()
Rounds a number
str()
Converts to type string
while
while is used to repeat code

Introduction

Here you will learn about

How it works

In all sections there are a number of tasks. At the bottom of the page it says how many tasks have been completed and how many there are in the section.

All examples have a green background color. Always try to run the code! Tasks have different background colors depending on the type of task; when you complete a task, it gets a green background color.

Click "Show tips" if you find a task difficult. There is no requirement that all tasks must be completed to continue.

Take your time! Read the text and examples carefully. Programming is not always easy at first!

Read about why it can be good to learn about programming.

What is Python?

Python is a programming language that is relatively easy to write and read. It was created in 1991 by Guido van Rossum. Python 3 was launched in 2008 and it is the version of Python used here at Pythonlab.dev.

Python is an interpreted language, which means that the code is interpreted while the program is running. There are a number of advantages to this, including that the code can easily be run on different platforms (Windows, Mac, Linux). A major disadvantage is that interpreted languages are slower than languages that convert the code to machine code (ones and zeros), which are called compiled languages.

Nowadays, Python is one of the world's most popular programming languages. Major areas of use are web apps, AI (artificial intelligence or machine learning) and scientific calculations. But Python can be used for most things.

Indentation

Indentation means that one or more lines are to the right of the text above. Look through the example below.

Here first two lines of text
that have no indentation.
    Here comes indented text and
    these green-marked lines belong to
    the same block.
Now we are back to text without indentation.
Here is another line.
    Here is a block that is indented and
    these blue lines are one and the same block.
        Now we are at the next level of indentation.
This line has no indentation.

All lines indented above are colored. The lines that have the same color have the same indentation and belong to the same block. What a block means you will learn later in this course.

A line is indented with four spaces. Here at Pythonlab.dev you can also use the TAB key, which is located above Caps Lock, and then four spaces are automatically written. This is also how many other editors used for writing code work.

In many programming languages braces { } are used to show which lines belong together. In Python, this is done with indentation. In other languages indentation is just a recommendation and the program works just as well without it. In Python, indentation is an important part of the language and the program cannot be run if the indentation is incorrect.

Login or create an account to save your progress and your code.

Correct the error

Someone has forgotten to indent line 2 one step (one TAB or four spaces), correct the error and then run the program. Note that you do not need to try to understand the code.

-- Output from your program will be here --

In the code above, you can see that some words are blue and the number 3 is green. It is common in programming to use an editor that highlights keywords, built-in functions, numbers, etc. with different colors. Color coding is used to make the code easier to read and to find errors faster.

Comments

When programming, it is often important to write code that others can understand. It is also important for your own sake. It is surprisingly difficult to remember how code you have written yourself works, even a short time afterwards.

A comment is plain text that is ignored by the computer. Using comments makes the code easier and faster to understand. For example, by briefly describing the purpose of a block of code with a comment.

In Python, comments are written with the symbol #; subsequent text is considered a comment and is not part of the program code.

Example

In the example below, all green text is comments.

# This is a comment
import math #here we import Python's math library.
print(math.pi) #prints pi

Login or create an account to save your progress and your code.

Correct the error

Oops, someone forgot to make the first line a comment. Fix it and then run the program.

-- Output from your program will be here --
Status
You have finished all tasks in this section!
🎉