# Examples of various comparison operators
# x == y => True if x is equal to y
# x != y => True if x is not equal to y
# x > y => True if x is greater than y
# x < y => True if x is less than y
# x >= y => True if x is greater than or equal to y
# x <= y => True if x is less than or equal to y
x = 5
y = 10
print(x==y)
print(x!=y)
print(x>y)
print(x<y)
print(x<=y)
print(x>=y)
False True False True True False
if <boolean expression> == True:
# execute this block of code
if True:
print('hello')
hello
if 'a' == 'a':
print('a equals to a')
a equals a
if <boolean expression> == True:
# execute this block of code
else:
# do this
if 10 >= 20:
print ('print 10 is greater than or equal to 20')
else:
print ('print 10 is NO greater than or equal to 20')
print 10 is NO greater than or equal to 20
if 100.5 >= 100.5:
print('True')
else:
print('False')
True
if <condition1>:
# block1
# execute this...
elif <condition2>:
# block2
# execute this
...
...
else:
# 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('I do not know what you entered! Hmm....')
Enter a number between 1-5:10 You entered a number larger than 5
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 |
http://www.informit.com/articles/article.aspx?p=459269&seqNum=11
num = 10
if (num %2 ==0 and num > 0):
print(num, "is positive and even")
10 is positive and even
num = -14
if (num %2 ==0 and num < 0):
print(num, "is negative and even")
if not False:
print('True')
True
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')
Exercise 2. Improve the above program to tell whether the given whole number is positive or negative.
Exercise 3. Write a program that helps someone decide where to go eat lunch depending on amount of money one has in their pocket.
Exercise 4. 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.
if condition:
if condition1:
# do something
else:
# do something else
if condition2:
# do something...
else:
# do this...
Exercise 5: Write a program that determines whether someone is eligible to vote in the US federal election.
Exercise 6:
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 arguments to the function are not valid. Here are some tests that should pass: assert day_name(3) == "Wednesday" assert day_name(6) == "Saturday" assert day_name(42) == None
Exercise 7:
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. Here are some tests that should pass: 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"