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

Simple Calculations

In this section you will learn

Using the + operator

In this section, we will use the most common operators. Typical mathematical operators are + * and /. Of these, only + can be used between strings. Adding numbers with + works exactly as you imagine.

Example

The operator + between two numbers and between two strings.

print(10 + 5)
print('Bye' + 'Bye')
print('Hey ' + 'you')

As you can see in the example above with 'hey ', it is fine to use spaces as a termination of a string to create a space to the next word. It does not work to combine a string with a number, for example, the program does not work if you write 'A string' + 10.

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?

6
6
42
6

Common Operators

Operator Description Example
+ Addition 10 + 5 = 15
Subtraction 7 − 5 = 2
* Multiplication 2 * 4 = 8
/ Division, gives decimal numbers 10 / 4 = 2.5
// Integer division, gives integers 11 // 5 = 2
% Remainder (also called modulo) 11 % 5 = 1
** Exponentiation 2 ** 4 = 16

Example

Here you can see examples of common simple calculations that are printed.

print(15 - 5) #10
print(7 * 8) #56
print(4 / 2) #2.0
print(11 / 3) #3.666666666666667
print(11 // 3) #3
print(11 % 3) #2
print(2 ** 10) #1024
    

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

Change

Change the code so that an integer division is performed instead of regular division.

-- Output from your program will be here --

Order of Operations

As you have probably learned, multiplication and division have higher priority than addition and subtraction. This means that for an expression like $$4+7\cdot2=18$$ the multiplication \(7\cdot2\) is performed first and then that result is added to 4. This is exactly how it works in Python as well. Of the operators we have looked at in this section, you can see the order of precedence in the table below.

Priority Operators
High **
  | *   /   //   %
Low +   −

Just like in mathematics, you can use parentheses to change the order. To perform the addition first in the example above, we can write $$(4+7)\cdot2=22$$ Now you know more about how you can use Python as a calculator.

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.5
12.5
13
70

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

Create

Print what \(2^{137} + 543\cdot263\cdot1000\) becomes

-- Output from your program will be here --

The number you calculated above is very large with its 42 digits. It is actually quite unusual for programming languages to handle such large numbers. In Python, there is no upper limit to how large a number can be. The disadvantage of allowing arbitrarily large numbers is that the performance of calculations becomes worse.

Do you want to practice another calculation? See the activity Calculate the expression.

Status
You have finished all tasks in this section!
🎉