Log in to save your progress.
<< Previous
Home
Next >>
OBS! Fönstret är för smalt för att använda pythonlabbet.se. Använd en enhet med tangentbord!

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

Logical Expressions

In this section you will learn

Logical Expressions

A logical expression is something that is either true (English: true) or false (English: false). Let's say we ask for the user's age. Is the user's age greater than 18? It is either true or false. We can write age > 18 and this is a logical expression. If we use print on a logical expression in Python, we will get either True or False as output.

Example

Two examples of logical expressions that are true. In both cases, a variable is compared with a number.

x = 5
print(x == 5) #x is equal to 5? (true)
print(x != 10) #x is NOT equal to 10? (true)

Note that two equals signs == are used to compare if two things are equal. It is a very common mistake to accidentally write only one equals sign when comparing.

Example

Two examples of logical expressions that are false. In this example, two variables are compared with each other.

x = 5
y = 10
print(x > y) #x is greater than y? (false)
print(y <= x) #y is less than or equal to x? (false)

In the examples above, different types of comparisons were used, and the most common comparisons are summarized in the table below.

Comparison Means
== Equal to
!= Not equal to
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to

It is fine to compare integers (int) with decimal numbers (float), but for natural reasons it is not possible to compare numbers with strings. Between two strings, it is excellent to use all the comparisons above. The strings are compared alphabetically, for example, 'sun' < 'star' is true.

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?

False
False
False
True
True
False
True
True

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?

False
False
False
True
True
False
True
True

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?

0.3
False
0.3
True

If you want to read more about what actually happened in the exercise above, see here on python.org.

Data type for true or false

It is excellent to store the value of a logical expression (True or False) in a variable. It is called a boolean variable. You will not hear the word "boolesk" often, usually the English word "boolean" or just "bool" is used. A boolean is the data type that takes up the least space in memory; it only needs one bit. 0 for False and 1 for True.

Example

True is stored in variable x because 3 > 2 is true.

x = 3 > 2

Combine logical expressions

We will now use two so-called logical operators, and (English: and) and or (English: or).

With and, the expressions on both sides of and must be true for the combined expression to be true. If any of the expressions around and are false, the entire expression becomes false. A common use is to check if a variable is within a range, see the example below.

Example of and

Check if variable x is between 10 and 20. Try changing x.

x = 15
print(x > 10 and x < 20)

With or, at least one of the expressions around or must be true for the combined expression to be true. For the combined expression to be false, both logical expressions around or must be false. A common use is to check if a variable is outside a range, see the example.

Example of or

Check if variable x is outside the range [10, 20]. Try changing x.

x = 5
print(x < 10 or x > 20)

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?

False
False
False
True
True
False
True
True
Status
You have finished all tasks in this section!
🎉