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:
The block of statements above are executed if and only if the boolean expression following the if statement is true.
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...
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
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
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
# 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)
number = 15 if (number % 2 == 0): print(number,'is even number.') else: print(number, 'is odd number.')
15 is odd number.
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
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.
true_passwd = '123456' input_passwd = input('Password? ') if true_passwd == input_passwd: print('correct password.') else: print('incorrect password.')
Password? 123456 correct password.
string1 = 'Mary' string2 = 'Mar' if string1 > string2: print(string1,'is greater than',string2)
Mary is greater than Mar
# 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')
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 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)
# 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.
# not operator temperature = 45 if temperature <= 50: print('Temperature is not more than 50.')
Temperature is not more than 50.
A Boolean variable can reference one of two values: True or False.
is_even = True print(type(is_even))
# 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.