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 if statement

In this section you will learn

Simple if statements

Now we will learn how we can make the program run different parts of the code depending on different conditions. Being able to control which code runs in different situations is fundamental in programming.

Think about what happens when you press the keyboard. What happens then, of course, depends on which key you have pressed. If (English: if) you have pressed ESC, something might be canceled, and if you press Enter, something completely different happens.

The syntax (how to write) for an if statement is if condition: where condition is a logical expression, i.e., true or false. If the condition is true, the code under the if statement is executed; if the condition is false, the code is skipped. In Python, the code under if is indented in its own block (see first section).

Example

A first if statement. Try changing the value of x and see what happens.

x = 10
if x == 10:
    print('x is equal to 10')
print('End')

Example

We test if the user is named Darth Vader or Yoda and, if so, print a message. Note that Python is case-sensitive.

name = input('Enter your name')
if name == 'Darth Vader':
    print('You are not kind.')
if name == 'Yoda':
    print('You are very kind.')
print('Goodbye ' + name)
    

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

Find the issue

The code below contains an indentation error. It's up to you to fix the code.

-- Output from your program will be here --

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

Create

Write a program that asks the user for the outdoor temperature (integer). If the temperature is less than -10, print Stay inside. If the temperature is greater than or equal to -10, print Go outside.

-- Output from your program will be here --

else

Sometimes it's good to run certain code if something is true (as we did with an if statement) and otherwise (English: else) run some other code. You can use else to run code if the condition in the if statement is false. An else cannot be used alone, but is always used in conjunction with if.

Example

A simple example of using else. Note that else is at the same indentation level as if.

name = input('What is your name?')
if name == 'Elsa':
    print('I think your sister\'s name is Anna.')
else:
    print('Your name is not Elsa.')

Example

Another simple example of using else.

age = int(input('How old are you?'))
if age >= 18:
    print('You are an adult!')
else:
    print('You are under 18 years old.')

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?

Line 3
Line 5

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?

Feel the force!
You will find only what you bring in.
Do or do not. There is no try.
Great warrior. Wars not make one great.
Feel the force!
Great warrior. Wars not make one great.
Do or do not. There is no try.
You will find only what you bring in.

elif

Most programming languages have something called else if or elseif (Swedish: otherwise if). In Python, the abbreviation elif is used instead. Just like with else, elif is only used in conjunction with an if statement, and the program will only reach it if the condition in the if statement above is false. The difference is that with elif, we write a new condition that must be true for the code block below to be executed.

You can use as many elif as you want in a row. An if statement thus begins with if condition:, followed by none, one or more elif condition: and possibly ends with an else:. Think of else as what happens if no conditions above are true.

Example

A menu using elif and else.

menu_choice = int( input('Make your choice:') )
if menu_choice == 1:
    print('Pizza ordered.')
elif menu_choice == 2:
    print('Hamburger ordered.')
elif menu_choice == 3:
    print('Soup ordered.')
else:
    print('Your choice is not valid.')

Why not just use multiple if statements in a row? With elif as above, the program executes only one code block (a print). With multiple if statements, all that are true are executed, and there can be multiple blocks.

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

Change the code

We forgot pancake in the menu in the example above! Change the code so that Pancake ordered. is printed if the user chooses 4.

-- Output from your program will be here --

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

Create

Your program should take an integer as input. If the user enters a negative number, the program should print Negative, if the number is positive, the program should print Positive, and otherwise Zero is printed.

-- Output from your program will be here --

Do you want to practice on a similar task? See What programming language do you like?.

Status
You have finished all tasks in this section!
🎉