3. Boolean variables
▪ A Boolean variable can take only
one of two values true or false.
Boolean variables are very often
used to set the option whether a
certain condition is met.
▪ Example
If YouHaveTime >= 60 minut
will_see_presentations = true
else:
will_see_presentations = false
In the 19th century,
George Bool
introduced a system
to mathematics that
makes it possible to
perform calculations
based on abstract
concepts such as
true and false.
4. Relationship operators
Operator in Python definition example in Python
> greater than x > y
Is x greater than y ?
< less than x < y
Is x less than y ?
>= greater than or equal
to
x >= y
Is x greater than or equal to y ?
<= less than or equal to x <= y
Is x less than or equal to y ?
== equal to x == y
Is x equal to y ?
!= unequal x != y
Is x unequal to y ?
5. Logical operators
(Using logical operators you can create extended Boolean
expressions)
operator definition example in Python
and The expression and returns true only
if both subexpressions are true.
x>y and a<b
Whether x is greater than y
and a is less than b?
or Or expression returns true, only when
one of the sub-expressions returns
true.
x == y or y ==z
Is x equal to y or x equal to z ?
not The not operator negates the value of
an expression.
not (x > y)
Does the expression x > y
return false ?
6. „If”, „else” construct
If time >= 60 minut
will_see_presentations = true
else:
will_see_presentations = false
7. Nested conditional structures
(To check more than one condition, conditional
structures can be nested.)
If time >= 60 minut
will_see_presentations = true
If notebook = true
will_take_notes = true
print (’note lesson’)
else:
will_take_notes = false
else:
will_see_presentations = false
8. Comparing text strings
▪ Python allows you to
compare text strings. This
allows you to create
conditional structures.
▪ ASCII codes are placed in
the computer's memory
(https://www.w3schools.c
om/charsets/ref_html_asci
i.asp )
▪ Python compares the
values of these codes,
therefore you can
compare:
if ’a’ < ’b’
Example in Python
Password = input (’Enter password: ’)
If password == ’prospero’
Print (’Password is correct’)
else:
Print (’Password is incorrect’)
9. Practical exercises
▪ Write a Python program to find those
numbers which are divisible by 7 and
multiples of 5, between 1500 and 2700
(both included).
Source: https://www.w3resource.com/python-exercises/python-
conditional-exercise-1.php
10. Source
▪ Gaddis T., Python dla zupełnie początkujących.
Wydanie IV. Helion 2019.
▪ https://www.w3resource.com/python-
exercises/python-conditional-statements-and-
loop-exercises.php
▪ Official source form Python community
▪ https://docs.python.org/3.11/tutorial/index.ht
ml
11. Thank you for your
attention! Please submit
any comments on the
software-engineers.org
PhD Artur Machura
arturmachura.info