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

Mathematical functions

In this section you will learn about

Built-in mathematical functions

Python has many built-in functions. They do not need to be imported from any module. We have already encountered several and here we will get acquainted with more.

Function Description Example
abs() Absolute value of abs(-5) = 5
round() Rounds to integer round(3.14) = 3
sum() Sum of sum([1,2,3]) = 6
min() Returns smallest number min([5,3,7]) = 3
max() Returns largest number max([5,3,7]) = 7

The first two, abs and round, work on numbers. Taking the absolute value of a number means that the minus sign is removed if it is a negative number. If it is a positive number, we get the same number back without anything happening. If we write abs(x - y) it can be interpreted as the distance between the numbers x and y. Distances are always positive.

The last three in the table work on objects that can be iterated over, for example on lists and tuples. More simply put, sum, min and max work on objects that we can "loop through" with a for-statement.

Example

Built-in mathematical functions.

l = [2, 3, 5, 7, 11]

total = sum(l)
smallest = min(l)
largest = max(l)
  

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?

-5
-3
0
3
5

The math module

To access the math module, we can write import math. For example, to use the constant \(\pi\) we write math.pi. Another way to write is from math import pi and then it is enough to write pi.

Example

Import the entire math module and print math.pi.

import math
print(math.pi)
  

Another useful "constant" in the math module is inf which stands for infinity. Since all numbers are smaller than this constant, it can be used as a first value in a variable that will keep track of a minimum value.

Example

The user enters three numbers and we keep track of which is the smallest entered.

from math import inf

smallest = inf
for i in range(3):
    number = int(input('Enter a number.'))
    if number < smallest:
        smallest = number

print('The smallest number you entered is', smallest)

If we instead want to keep track of the largest number, we can start by initializing a variable largest = -inf instead.

The math module also contains several functions. Many functions are for advanced mathematics and some are suitable for high school mathematics. The two simplest functions are ceil() which rounds up and floor() which rounds down.

The table below lists a couple of the most common functions in math.

Function Description Example
ceil() Rounds up ceil(4.2) = 5
floor() Rounds down floor(4.2) = 4
sin() Sine sin(pi/2) = 1.0
cos() Cosine cos(pi/2) = 0.0
factorial() Factorial factorial(5) = 120
log10() Logarithm log10(100) = 2.0

On Python's official page, you can see all functions and constants in the math module.

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

Change the code

We want to calculate the circumference of a circle with radius \(\pi\). The code does not work as it is now. Fix it!

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