# 0811 Operators, If, else and elif Benson Chiu @ FDCS 8th X 7th <!-- Put the link to this slide here so people can follow --> slide: https://hackmd.io/@benson-elementary-cpp/ryIMKWpyF --- ## = - In python, "=" stands for **binding** - In C++ or C, "=" stands for **assign** ```python= a = 20 #attach a to 20 ``` ```cpp= int a = 20; //assign 20 to a ``` --- ## Arithmetic Operators | Operator | Function | | -------- | -------------- | | $+$ | Addition | | $-$ | Subtraction | | $*$ | Multiplication | | $/$ | Division | | % | Modulus | | $//$ | Floor division | --- ## Comparison Operators | Operator | Function | | -------- | ------------------------ | | $==$ | Equal | | $!=$ | Not Equal | | $>$ | Greater than | | $<$ | Less than | | $>=$ | Greater than or equal to | | $<=$ | Less than or equal to | --- > The result will be either $True$ or $False$ --- ## Logical Operators | Operator | Function | | -------- |:------------------------------------------------------- | | $and$ | Returns True if both statements are true | | $or$ | Returns True if one of the statements is true | | $not$ | Reverse the result, returns False if the result is true | --- ## Assignment Operators | Operator | Same As | | -------- | ------- | | a += 1 | a = a+1 | | a -= 1 | a = a-1 | | a *= 3 | a = a*3 | | a /= 4 | a = a/4 | > *..... $et. cetera$* --- For more information, please take a look at [W3 Schools](https://www.w3schools.com/python/python_operators.asp) --- ## Python conditions ![](https://i.imgur.com/YtBtKUG.png) --- ## A brief example Benson has difficulty choosing the restaurant for dinner, so he comes up an idea: 1) He select a random number between 1 to 100, and names it $num$ 2) Let $check = (num \mod 5)$ * If $check == 1$, have McDonald's * If $check == 2$, have KFC * If $check == 3$, have Mos Burger * If $check == 4$, have Burger King * If $check == 0$, have 7-11 --- ## Implementation ```python= num = random.randint(1,10) check = num % 5 if check == 1: print("MCD") elif check == 2: print("KFC") #....... and so on else: print("7-11") ``` --- ## Another brief example - Leap Year Each leap year has 366 days instead of 365, by extending February to 29 days rather than the common 28. **These extra days occur in each year which is an integer multiple of 4 (except for years evenly divisible by 100, but not by 400).** > from Wikipedia
{"metaMigratedAt":"2023-06-16T06:22:40.626Z","metaMigratedFrom":"YAML","title":"0811 Operaters, If, else and elif","breaks":true,"description":"View the slide with \"Slide Mode\".","slideOptions":"{\"spotlight\":{\"enabled\":true}}","contributors":"[{\"id\":\"29625303-a0ab-4215-b54d-35c95f11006b\",\"add\":4951,\"del\":2077}]"}
    194 views
   Owned this note