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
Basics part 2
Syntax
Short description
abs
Absolute value
append
Adds an element to a list
def
Defines a function
for
for is used to repeat code
len
Returns the number of elements in an object
max
Returns the largest element
min
Returns the smallest element
pop
Removes an element from a list
return
Returns data from a function
sum
Sums the elements in an object

Tuples

In this section you will learn

What is a tuple and why are tuples used?

Tuples are very similar to lists in Python. Just like lists, they have an order and they can contain duplicates and different types. But there is one important difference, and that is that a tuple cannot be changed, it is immutable.

Since a tuple cannot be changed, Python can use tuples more efficiently than lists. They take up less memory, for example. Tuples' immutability also means that they can be used with dictionaries and sets, as we will see in later sections.

Now we will show how a tuple is created.

Example

Creates a simple tuple with two values. Parentheses do not have to be written but it is recommended.

my_tuple = (3, 5) #can also be written without parentheses: 3, 5

Example

Several things that can be done with lists can also be done with tuples.

my_tuple = (2, 3, 5, 7, 11)

print(my_tuple[0]) #print the first element
print(my_tuple[0:3]) #the first three elements
print(5 in my_tuple) #is 5 in the tuple?
  

However, it is not possible to change a tuple, as mentioned. The following example shows what the error message looks like.

Example

An error message is printed and the program terminates if you try to change a tuple.

tupel = ('a','a','c')
tupel[1] = 'b' #FEL
  

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

Three dice rolls

The code below generates all possible results of three dice rolls. The list all_rolls consists of tuples with three values. These three values represent the result of a roll with three dice.

The list contains all 216 possibilities. How many of these give a dice sum of 7? Only print the number to get it right.

-- Output from your program will be here --

Some tricks with tuples

Tuples can be used to easily create multiple variables with different values on the same line.

Example

Create multiple variables on one line with tuple-syntax. We take advantage of the fact that parentheses are not necessary.

name, age, language = 'Emil', 38, 'Python'

Thanks to this syntax, it is easy to swap values between two variables. In most programming languages, a third temporary variable is required.

Example

Swap values between two variables.

a, b = 'a', 'b'
a, b = b, a #swap values
  

A common use of tuples is as a way to let functions return multiple values.

Example

A function that returns two values in a tuple, the circumference and the area of a rectangle. Here too we use a tuple without parentheses because that is how it is usually written.

def rectangle(width, height):
    circumference = 2*width + 2*height
    area = width * height
    return circumference, area
  

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?

(3, 5) (3, 5)
(3, 5) (5, 3)
(5, 3) (3, 5)
(5, 3) (5, 3)
Status
You have finished all tasks in this section!
🎉