?print-pdf
' Created for
>>> print( type(True) )
<class 'bool'>
>>> print( type(False) )
<class 'bool'>
True/False values for most of the build-in objects:
Type: | =False | =True |
---|---|---|
any numeric type | 0 (zero) | everything else |
string | "" | any non-empty string |
sequences and collections | empty | any non empty |
Using the built-in function bool()
, we can convert any Python value to True or False
>>> bool(-42)
True
>>> bool(0)
False
>>> bool(0.00001)
True
>>> bool("ada")
True
>>> bool("")
False
>>> 2 < 1
False
>>> 2 < "1"
...
TypeError: '<' not supported between instances of 'int' and 'str
Operation | Meaning |
---|---|
< |
strictly less than |
<= |
less than or equal |
> |
strictly greater than |
>= |
greater than or equal |
== |
equal |
!= |
not equal |
is |
object identity* |
is not |
negated object identity* |
*The identity of an object uniquely identifies where this object is stored in the computer memory.
It will be discussed deeply in the OOP part of the course
>>> i = 5
>>> i < 5
False
>>> i <= 5
True
>>> 9 < 1000
True
>>> "9" < "1000"
False
Note, the last example: "9" < "1000"
, the result is False, because strings are compared lexicographically.
57 < 49
and as the result is False it returns False for the whole expressionord()
function
x = 5
# equivalent of: 0<x and x<10:
print(0<x<10)
Note, that Python 2, can compare values from different types
>>> "2" == 2
# False
>>> "2" >= 2
True
>>> 2 >= "2"
False
>>> 99999 >= "2"
False
When you need a reliable comparison, make sure that you compare values from same type!
If you need, you can convert between built-in types by using some of the Python's Built-in Functions (some of them will be discussed in further themes).
print("2" == str(2))
# True
print(int("2") >= 2)
# True
print(99999 >= int("2"))
# True
Operation | Name | Result |
---|---|---|
x or y | Logical OR | if x is false, then y, else x |
x and y | Logical AND | if x is false, then x, else y |
not x | Logical NOT | if x is false, then True, else False |
x
and y
are expressionsx | y | AND x and y |
OR x or y |
NOT not x |
---|---|---|---|---|
T | T | T (y) | T (x) | F |
T | F | F (y) | T (x) | F |
F | T | F (x) | T (y) | T |
F | F | F (x) | F (y) | T |
Legend: T - true; F - false
>>> True and False
False
>>> 0 and 1
0
>>> 0 or 1
1
>>> 1 or 0
1
>>> not 1
False
>>> not 0
True
user_name=''
user_age = 15
user_country = 'BG'
print(user_age>18 and user_country=='BG')
print(user_age>18 or user_country=='BG')
# note that the value of logical operations is not always True/False
print(user_name and 'Anonymous' )
print(user_name or '***Anonymous***' )
False
True
***Anonymous***
if
statementif
statement
if condition :
block 1
x = 42
if x % 2 == 0:
print("{} is an even number!".format(x))
if False :
print("1")
print("2")
print("3")
print(1)
and print(2)
forms a block, which will be executed, only if the condition is true.print(3)
is not in the block, and will be always executed!if - else
statement
if condition :
block 1
else :
block 2
x = 41
if x % 2 == 0:
print("{} is an EVEN number!".format(x))
else:
print("{} is an ODD number!".format(x))
# OUTPUT
# 41 is an ODD number!
user_lang = "bg"
if user_lang == "bg":
print("Здравейте")
else:
print("Hello")
print("-" * 20)
Здравейте
--------------------
if - elif - else
statement
if c1 :
block 1
elif c2:
block 2
else:
block 3
user_lang = "it"
if user_lang == "bg":
print("Здравейте")
elif user_lang == "it":
print("Ciao")
elif user_lang == "en":
print("Hello")
else:
print("I do not speak your language!")
print("-" * 20)
x if condition else y
condition
, and:condition
is True
, the expression evaluates to x
.condition
is False
, it evaluates to y
.
user_age = 5
print( 'adult' if user_age>=18 else "child" ) # 'adult'
# above code is equivalent to next if-else statement:
if user_age>=18:
print('adult')
else:
print('child')
print( 2 + 3 * 4 ) # Equals 14, not 20
print( (2 + 3) * 4 ) # Equals 20
These slides are based on
customised version of
framework