The if
statement allows us to condition the program flow on its data.
a = 10
b = 2
if a > b:
print('Yes')
Yes
if a < b:
print('Yes')
Notice the colon and the indented block. The syntax is always:
if condition:
statement1
statement2
statement3
...
Whitespaces mark block code: Only commands within the indented block are conditional. Other commands will be executed, no matter if the condition is met or not. There is no use of curly brackets or end
command: unindenting will close the code block.
if a > b:
print('Yes')
print('Another operation will follow')
a = 0
print(a)
Yes Another operation will follow 0
Note: the condition expression always returns a boolean (if it's not already a boolean, it will be implicitly converted into one), and the indented commands only occur if the boolean has a True
value. Therefore, we can use logical operators to create more complex conditions.
x = 15
y = 8
if (x > 10 and y < 10) or x * y == 56:
print('Yes')
Yes
x = 9
if (x > 10 and y < 10) or x * y == 56:
print('Yes')
x = 7
if (x > 10 and y < 10) or x * y == 56:
print('Yes')
Yes
Let's write a program that checks if a number is devisible by 17. Remember the modulo operator.
x = 442
if x % 17 == 0:
print('Number is devisible by 17!')
print('End of program.')
Number is devisible by 17! End of program.
else
¶We can add else statements to perform commands in case the condition is not met, or in other words, if the boolean is False.
x = 586
if x % 17 == 0:
print('Number is devisible by 17!')
else:
print('Number is not devisible by 17!')
print('End of program.')
Number is not devisible by 17! End of program.
elif
¶When using elif statements, multiple conditions are tested one by one. Once a condition is met, the corresponding indented commands are performed. If none of the conditions is True
, the else
block (if exists) is executed.
x = 586
if x % 17 == 0:
print('Number is devisible by 17!')
elif x % 2 == 0:
print('Number is not devisible by 17, but is even!')
else:
print('Number is not devisible by 17, and is odd!')
print('End of program.')
Number is not devisible by 17, but is even! End of program.
A leap year is a year that has 366 days (adding February 29th). A year is a leap year if it is divisible by 400, or divisible by 4 but not by 100.
For example, 2012 and 2000 are leap years, but 1900 isn't.
Test a year of your choice by using an appropriate if
statement and print the result.
while
loop¶We use while
loops to do something again and again, as long as a condition is met.
The syntax is very similar to that of if
statement.
from random import randint # we will get back to import later on
random_num = randint(1,100)
while random_num <= 90: # condition
print(random_num) # indented block
random_num = randint(1,100) # indented block
print ('Found a number greater than 90:', random_num)
77 3 14 90 84 26 Found a number greater than 90: 93
Now let's count how many times it takes to get a random number greater than 90.
We'll use a counter variable.
from random import randint
counter = 1
random_num = randint(1,100)
while random_num <= 90:
print(random_num)
random_num = randint(1,100)
counter = counter + 1
print ('Found a number greater than 90:', random_num, '. It took',counter,'tries.')
Found a number greater than 90: 99 . It took 1 tries.
The Collatz Conjecture (also known as the 3n+1 conjecture) is the conjecture that the following process is finite for every natural number:
If the number n is even divide it by two, if it is odd multiply it by 3 and add 1. Repeat this process until you get the number 1.
Write a program to check if the Collatz conjecture is true for a number of your choice. Print every step of the process.
This notebook was written by Yoav Ram and is part of the Python for Engineers course.
The notebook was written using Python 3.6.1. Dependencies listed in environment.yml, full versions in environment_full.yml.
This work is licensed under a CC BY-NC-SA 4.0 International License.