Decision Structure and Boolean Logic

Acknowledgement : the contents of this notebook are partially based on the book "Starting Out with Python (3rd Edition)".

In this class, we will look at the if statement in Python. The syntax of if statement is

if boolean expression:
    statement(s)

The block of statements above are executed if and only if the boolean expression following the if statement is true.

if statement

In [7]:
number = int(input('Enter integer number : '))
remainder = number % 2

if remainder == 0:
    print('This is an even number...') # this statement is executed if remainder is zero
else:
    print('This is an odd number...') # this statement is executed if remainder is *not* zero
Enter integer number : 5
This is an odd number...
In [9]:
x = 4
if x > 3:
    print('x is greater than 3') # this statement is executed because 4 > 3.
    
print('x is',x)
x is greater than 3
x is 4

Boolean Expressions and Relational Operators

The expressions that are tested by the if statement are called "Boolean expressions". A relational operator determines whether a specific relationship exists between two values. Common relational operators are

  • ">" : greater than
  • "<" : less than
  • ">=" : greater than or equal to
  • "<=" : less than or equal to
  • "==" : equal to
  • "!=" : not equal to

In [5]:
number = 7
if (number == 7): # check whether the value of number is equal to 7
    print('less than 10...')

print('number is',number)
less than 10...
number is 7
In [10]:
# Boolean variable : we can always assign the truth value "True" or "False" to a variable.

x = False # x is a Boolean variable whose value is False.
type(x)
Out[10]:
bool

if-else statement

In [26]:
number = 15
if (number % 2 == 0):
    print(number,'is even number.')
else:
    print(number, 'is odd number.')
15 is odd number.
In [29]:
number = 18
diviser = 6
if (number % diviser == 0):
    print(number,'is divisible by',diviser)
else:
    print(number,'is not divisible by',diviser)
18 is divisible by 6

comparing strings

In [31]:
name1 = 'nutnicha1'
name2 = 'nutnicha2'

if name1 == name2:
    print('The names are the same.')
else:
    print('The names are NOT the same.')
The names are NOT the same.
In [34]:
true_passwd = '123456'
input_passwd = input('Password? ')

if true_passwd == input_passwd:
    print('correct password.')
else:
    print('incorrect password.')
Password? 123456
correct password.
In [11]:
string1 = 'Mary'
string2 = 'Mar'

if string1 > string2:
    print(string1,'is greater than',string2)
Mary is greater than Mar

if-elif-else statement (nested decision structure)

In [13]:
# grading system
# A if 90 < score <= 100
# B if 80 < score <= 90
# C if 70 < score <= 80
# D if 60 < score <= 70
# F if score < 60

score = 80

if score < 60:
    print('F')
elif score <= 70:
    print('D')
elif score <= 80:
    print('C')
elif score <= 90:
    print('B')
elif score <= 100:
    print('A')
else:
    print('invalid score')
C

Logical Operators

The logical operators such as "and" and "or" allow you to connect multiple Boolean expression to create a compound expression.

Common logical operators are

  • "and" : (expr1 and expr2) both expr1 and expr2 must be true for the compound expression to be true.
  • "or" : (expr1 or expr2) one or both expr1 and expr2 must be true for the compound expression to be true.
  • "not" : (not expr1) the not operator reverses the truth of its operand. If expr1 is true, (not expr1) is false, and vice versa.

In [53]:
# and operator
x = 101
if (x > 0) and (x < 100):
    print(x,'is between 0 and 100.')
else:
    print(x,'is outside of the range (0,100)')
101 is outside of the range (0,100)
In [56]:
# and operator
x = 155
if (x < 0) and (x > 100):
    print(x,'is outside of the range.')
else:
    print(x,'is inside of the range.')
155 is inside of the range.
In [58]:
# not operator
temperature = 45
if temperature <= 50:
    print('Temperature is not more than 50.')
Temperature is not more than 50.
In [60]:
not(False)
Out[60]:
True

Boolean Variables

A Boolean variable can reference one of two values: True or False.

In [3]:
is_even = True
print(type(is_even))
<class 'bool'>
In [4]:
# check if the number is an even number
number = 13
is_even = (number % 2 == 0)

if is_even:
    print(number,'is an even number.')
else:
    print(number,'is an odd number.')
13 is an odd number.
In [ ]: