1. If else statements¶
if
if True:
print('True value')
Upper code is returning following text:
True value
if False:
print('False value')
As you there is nothing printed.
1.1. Conversion of list into boolean¶
empty_list = []
if empty_list:
print('List with content')
else:
print('List empty')
Text above is returning following text:
List empty
Attention
What happened here is implicit conversion of type list into bool
>>> bool([])
False
>>> bool([1, 2, 3])
True
1.2. Checking bool values¶
>>> bool(-1)
True
>>> bool(0)
False
>>> bool(124)
True
>>> bool({})
False
Warning
Every number but not zero will return True
.
Number -1 if written in binary (U2) got got ones (1) on decimal positions
1.3. Checking ranges¶
temperature = 18
if 16 <= temperature < 24:
print('Temperature good for biking')
else:
print('Temperature not appropiate for biking')
This will give us
Temperature good for biking
If we change temperature to below zero
temperature = -3
if 16 <= temperature < 24:
print('Temperature good for biking')
elif 3 <= temperature < 16:
print('Temperature good for walk')
elif -5 <= temperature < 3:
print('Temperature good for skiing')
else:
print('Don`t know what to do :(')
Temperature good for skiing
1.4. Exercises - part 1¶
Let user put his age, check if he is adult,
Let user put number, check if the value is float or integer
Hint
There are many ways to do that. Find your own ;)
1.4.1. Exercises - part 2¶
Create simple BMI calculator, which will get all values from
input
. In result it should return status if person is:Overweight,
Normal,
Underweight
1.4.2. Exercises - part 3¶
Use library
os
functionsystem
for checking if host is activeDepending on a status print proper message,
Hint
Bear in mind that systems got own status codes after execution commands