This is a machine-generated translation. If you're not at the in-person workshop, try the DjangoGirls tutorial for an intro to Python!
You will want to perform a lot of things in the code only if certain conditions are met. Python has conditional statements for this purpose.
We will now try to gradually write a program that verifies a secret password.
To start, write a program that outputs
True when you enter the word
čokoláda (chocolate in English). If a different password is entered, it will output
password = input('Enter password: ') print(password == 'chocolate')
True is not very interesting. A better program would do this:
A Python command for this is called
if. It is used like this:
password = input('Enter password: ') if password == 'chocolate': print('Correct! Welcome.')
The conditional statement begins with
if, continues with a condition (such as a comparison) and ends with a colon.
After the line with
if, there is an indented command - there are 4 spaces at the beginning of the line. Python recognizes that this part of the program should be executed only when the condition is true.
Save and run:
(venv) $ python python_intro.py Enter password: chocolate Correct! You may enter. (venv) $ python python_intro.py Enter password: sesame
Just because four spaces are needed at the beginning of a line doesn't mean you have to press the spacebar four times. Some editors automatically indent (if you write the line with 'if' correctly). However, in all properly configured editors, you can indent using the ↹ Tab key, and the ⇧ Shift+↹ Tab combination will return the line back one indentation level.
In the previous example, the code was executed only if the condition was met. An even better program would be this one:
Python has the command
else for that:
password = input('Enter password: ') if password == 'chocolate': print('Correct! Welcome.') else: print('WARNING! WARNING!') print('UNAUTHORIZED ACCESS!')
Does it work?
(venv) $ python python_intro.py Enter password: chocolate Correct! You may enter. (venv) $ python python_intro.py Enter password: sesame WARNING! WARNING! UNAUTHORIZED ENTRY!
Sometimes it happens that a program needs to decide between multiple options. For this purpose, the
elif command is used (short for "else if" in English).
By using this method, it is possible to comment on the volume of the music.
Ask about the volume, remember the numerical response.
In Python, it would be written like this:
volume = int(input('What is the radio volume set to? ')) if volume < 20: print("It's quite quiet.") elif volume < 40: print("Good background music.") elif volume < 60: print("Great, I can hear all the details.") elif volume < 80: print("Good for a party.") elif volume < 100: print("A bit too loud!") else: print("My ears are bleeding!")
(venv) $ python python_intro.py What is the radio volume set to? 28 Good as background music.
Notice that only one option is always selected. If you enter
28, Python will reach
volume < 40, display the appropriate message, and skip all other options.
What did you see in this lesson?
The following is the English translation of the Czech text:
*The commands if (if), elif (else, if), and else (else) condition other commands.