# Examples of various comparison operators
x = 5
y = 10
# is x equal to y? True or False?
x == y
False
# is x not equal to y? True of False?
x != y
True
# is x is greater than y? True or False?
x > y
False
# is x less than y?
x < y
True
# is x less than or equal to y?
x <= y
True
# is x greater than or equal to y
x >= y
False
if <boolean expression> == True:
# execute this block of code
if True:
print('Hello, there!')
Hello, there!
a = 'apple'
if a == 'apple': # this should evaluate True
print('value of a equals to apple')
print('continue execution here...')
value of a equals to apple continue execution here...
a = 'apple'
if a == 'Apple': # this should evaluate False
print('value of a equal to Apple') # this block will NOT be executed!
print('continue execution here...')
continue execution here...
if <boolean expression> == True:
# execute this block of code
else:
# otherwise, execute this
num1 = 100.5
num2 = 100.49999999999999
if num1 > num2:
print('{} is greater than {}'.format(num1, num2))
else:
print('{} is NOT greater than {}'.format(num1, num2))
100.5 is greater than 100.49999999999999
if 10 >= 20:
print ('print 10 is greater than or equal to 20')
else:
print ('print 10 is NOT greater than or equal to 20')
print 10 is NOT greater than or equal to 20
if <condition1>:
# block1
# execute this...
elif <condition2>:
# block2
# execute this
...
...
else:
# if all the previous conditions are evaluated False; this is the alternative!
# else block
# execute code in this block
# Guess the number entered by user
ans = int(input('Enter a number between 1-5: '))
if ans == 1:
print('You entered 1')
elif ans == 2:
print('You entered 2')
elif ans == 3:
print('You entered 3')
elif ans == 4:
print('You entered 4')
elif ans == 5:
print('You entered 5')
elif ans > 5:
print('You entered a number larger than 5')
else:
print('Hmm.... I do not know what you entered!')
Enter a number between 1-5: 2 You entered 2
and, or, and not -- allow to build more complex boolean expressions
Truth table for and
a | b | a and b |
---|---|---|
T | T | T |
T | F | F |
F | T | F |
F | F | F |
Truth table for or
a | b | a or b |
---|---|---|
T | T | T |
T | F | T |
F | T | T |
F | F | F |
Truth table for not
a | not a |
---|---|
T | F |
F | T |
# and demo
num = 10
if (num%2 == 0 and num > 0):
print(f"{num} is even and positive")
10 is even and positive
# and demo
num = -14
if num %2 == 0 and num < 0:
print(f"{num} is even and negative")
-14 is even and negative
# or demo
# a retimerment calculator
money = int(input('How much money have you saved? '))
ferrari = input('Do you have a farrari? [y/yes | n/no]: ')
if money >= 1000000 or ferrari == 'y' or ferrari == 'yes':
print('Congrats!! You can retire now.')
else:
print('Sorry, keep working!!')
How much money have you saved? 100000 Do you have a farrari? [y/yes | n/no]: yes Congrats!! You can retire now.
# not example
if not False:
print('True')
True
# not demo
# a retimerment calculator
money = int(input('How much money have you saved? '))
ferrari = input('Do you have a farrari? [y/yes | n/no]: ')
if not (money >= 1000000 or ferrari == 'y' or ferrari == 'yes'):
print('Sorry, keep working!!')
else:
print('Congrats!! You can retire now.')
How much money have you saved? 10 Do you have a farrari? [y/yes | n/no]: y Congrats!! You can retire now.
if condition:
if condition1:
# do something
else:
# do something else
if condition2:
# do something...
else:
# do this...
# program that determines if a given number is even or odd, positive or negative
num = -100
if num == 0:
print(f'{num} is zero!')
elif num%2 == 0:
if num > 0:
print(f"{num} is even and positive")
else:
print(f'{num} is even and negative')
else:
if num > 0:
print(f"{num} is odd and positive")
else:
print(f'{num} is odd and negative')
print('done')
-100 is even and negative done
Exercise 1: Write a program to test whether a given whole number is even, odd or zero.
x = input('Enter a whole number: ')
x = int(x)
if x == 0:
print(x, 'is zero')
elif x%2 == 0:
print(x, 'is even')
else:
print(x, 'is odd')
Enter a whole number:1 1 is odd
Exercise 1.1: Rewrite Exercise 1 using a function and write 2 test cases.
def isEvenOddOrZero(num):
if num == 0:
return 'zero'
elif num%2 == 0:
return 'even'
else:
return 'odd'
def test():
assert isEvenOddOrZero(10) == 'even'
assert isEvenOddOrZero(0) == 'zero'
assert isEvenOddOrZero(19) == 'odd'
print('all test cases passed...')
test()
all test cases passed...
Exercise 2. Write a function that returns whether the given whole number is positive or negative. Also write at least 2 test cases.
def positiveNegativeOrZero(num):
if num == 0:
return 'zero'
elif num > 0:
return 'positive'
else:
return 'negative'
def test_positiveNegativeOrZero():
assert positiveNegativeOrZero(0) == 'zero'
assert positiveNegativeOrZero(100) == 'positive'
assert positiveNegativeOrZero(-99.99) == 'negative'
print('all test cases passed for positiveNegativeOrZero()')
test_positiveNegativeOrZero()
all test cases passed for positiveNegativeOrZero()
Exercise 3: Write a program that converts students' grade value (0-100) to corresponding letter grade (A-F) where
90 and above is A 80 and above is B 70 and above is C 60 and above is B 59 and below is F
Write test cases to test each outcome.
Exercise 4: Write a program that helps someone decide where to go eat lunch depending on amount of money one has in their pocket.
Exercise 5: Given a day of week as integer (say 1 for Sunday) write a program that tells whether that day is weekend, or weekday and the actual name of the day.
Exercise 6: Write a program that determines whether someone is eligible to vote in the US federal election.
Exercise 7: Write a function day_name that converts an integer number 0 to 6 into the name of a day. Assume day 0 is “Sunday”. Once again, return None if the argument to the function is not valid.
def day_name(day):
pass
# Here are some tests that should pass
def test_day_name():
assert day_name(3) == "Wednesday"
assert day_name(6) == "Saturday"
assert day_name(42) == None
print('all test cases passed for day_name()')
test_day_name()
--------------------------------------------------------------------------- AssertionError Traceback (most recent call last) <ipython-input-3-97913494fc66> in <module>() ----> 1 test_day_name() <ipython-input-2-3f7d21d757ff> in test_day_name() 1 def test_day_name(): ----> 2 assert day_name(3) == "Wednesday" 3 assert day_name(6) == "Saturday" 4 assert day_name(42) == None 5 print('all test cases passed for day_name()') AssertionError:
Exercise 8: Write a function that helps answer questions like ‘“Today is Wednesday. I leave on holiday in 19 days time. What day will that be?”’ So the function must take a day name and a delta argument (the number of days to add) and should return the resulting day name.s
def day_add(dayName, delta):
pass
# Here are some tests that should pass
def test_day_add():
assert day_add("Monday", 4) == "Friday"
assert day_add("Tuesday", 0) == "Tuesday"
assert day_add("Tuesday", 14) == "Tuesday"
assert day_add("Sunday", 100) == "Tuesday"
assert day_add("Sunday", -1) == "Saturday"
assert day_add("Sunday", -7) == "Sunday"
assert day_add("Tuesday", -100) == "Sunday"
print('all test cases passed for day_add()')
test_day_add()