#!/usr/bin/env python # coding: utf-8 # # Conditionals - flow-control structures # # - conditional statements control the flow of execution of codes # - makes some block of code to skip or execute based on some conditions # - loosely speaking, it helps computer think and make decision # - boolean values - True or False # - boolean expressions - comparison operators are used to compare values that result in True or False value. # ## Comparison operators # - allow us to compare data resulting in True or False outcome # - ==, !=, >, >=, <, <= # In[7]: # 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) # ## Conditional execution # - Three types of conditional selectors # - One-way, Two-way and Multi-way selectors # ## One-way selector # - just one if statement by itself # - syntax: # ```python # if == True: # # execute this block of code # ``` # In[8]: if True: print('hello') # In[9]: if 'a' == 'a': print('a equals to a') # ## Two-way selector # - if statement followed by else statement # - syntax: # # ```python # if == True: # # execute this block of code # else: # # do this # # ``` # In[10]: 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') # In[11]: if 100.5 >= 100.5: print('True') else: print('False') # ## Multi-way selector # - similar to multiple-choice questions with only one valid answer # - start from first if statement and as soon as a if expression is evaluated True all the rest elif, else are ignored # # ```python # if : # # block1 # # execute this... # elif : # # block2 # # execute this # ... # ... # else: # # else block # # execute code in this block # # ``` # In[12]: # 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....') # ### Visualize using pythontutor.com: https://goo.gl/ZSm9KS # ## Logical Operators # **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 | # # ### Order of Evaluations: Highest to Lowest:
# http://www.informit.com/articles/article.aspx?p=459269&seqNum=11 # In[13]: num = 10 if (num %2 ==0 and num > 0): print(num, "is positive and even") # In[15]: num = -14 if (num %2 ==0 and num < 0): print(num, "is negative and even") # In[14]: if not False: print('True') # ## Exercises # Exercise 1: Write a program to test whether a given whole number is even, odd or zero # In[ ]: 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. # ## Nested conditionals # ```python # 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"
# 
# In[ ]: