Log in to save your progress.
<< Previous
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

The print function

In this section you will learn to

Printing text

A simple way to make your program communicate with the outside world is to print text on the screen. In Python, this is done with the print function.

Example

Prints the text Hello World on the screen. The character ' can be found just to the left of the enter key.

print('Hello World!') #Prints Hello World!

Hello World is an example of what in programming is called a text string or just a string. In Python, print can be used to print strings, numbers, lists, and much more. It is often useful when programming to see what is happening in the program and to communicate with the user. Note that we use ' around the string to tell Python that it is a string.

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

What does the code do?

Read the code below and try to figure out what the program prints. Run the program after you answer and see if you were correct.

-- Output from your program will be here --

Question: What will the program print?

print('I love Python.')
I love Python.
'I love Python.'

Printing numbers

With print we can also print numbers. It is also possible to do a calculation inside the parenthesis. Note that in programming, a decimal point is used instead of a decimal comma. For example, 3.14 is written as 3.14 in our code below. Also note that an asterisk * is used for multiplication.

Example

A couple of examples where we print numbers

print(42) #Prints 42
print(3.14*5*5) #Calculates 3.14*5*5 and prints the result
    

As you can see in the example above, each new print gives a new line. This is how print works by default, as long as we don't give the function other settings.

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

What does the code do?

Read the code below and try to figure out what the program prints. Run the program after you answer and see if you were correct.

-- Output from your program will be here --

Question: What will the program print?

10 / 3 = 3,333333333333333
10 / 3 =
3.333333333333333

In a later section, we will learn how to print both numbers and a text string on the same line. Now it's time to try changing the code and writing your first own program.

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

Find the issue

The code has errors. It is up to you to fix.

-- Output from your program will be here --

The error above is a so-called syntax error. It is the easiest type of error to fix. In the error message, we often find out that it is a typo and which line the error is on.

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

Create

Write code that prints It is currently the year on the first line and 2026 on the second line.

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