|
| 1 | +#Any complex program is made possible thanks to control flow statements |
| 2 | +#control flow statements include if statements and loops |
| 3 | + |
| 4 | +########## LOGIC ########## |
| 5 | + |
| 6 | +#You must first understand booleans. |
| 7 | +#They are either True or False (these are both keywords) |
| 8 | +# |
| 9 | +happy = True |
| 10 | +print(happy) |
| 11 | + |
| 12 | +#There are operators that will return true or false |
| 13 | +#These are known as comparison operators |
| 14 | + |
| 15 | +print(5 > 3) |
| 16 | +age = 21 |
| 17 | +print("age >= 21?", age >= 21) |
| 18 | + |
| 19 | +#You can also compare strings: |
| 20 | +me = "Caleb" |
| 21 | +you = "Nerd" |
| 22 | +print("me == you?", me == you) |
| 23 | + |
| 24 | +#Even lists! |
| 25 | +my_grades = [100, 100, 100] |
| 26 | +your_grades = [100, 100, 1] |
| 27 | +print("Same grade?", my_grades == your_grades) #false |
| 28 | + |
| 29 | +your_grades = [100, 100, 100] |
| 30 | +print("Same grade?", my_grades == your_grades) #true |
| 31 | + |
| 32 | +#in some languages, == compares by memory address to see if same object |
| 33 | +#this is done in python using is. |
| 34 | +#This is known as an identity comparison |
| 35 | +print("are grades the same object?", my_grades is your_grades) #false |
| 36 | + |
| 37 | +my_grades = your_grades |
| 38 | +print("are grades the same object?", my_grades is your_grades) #true |
| 39 | + |
| 40 | +#You can also do order testing for strings |
| 41 | +#Read ABC comes before BCD? |
| 42 | +print("A before B?", "ABC" < "BCD") |
| 43 | + |
| 44 | +#Not operator can be used to negate an equality check |
| 45 | +#Read A is not equal to B? |
| 46 | +print("A != B?", "A" != "B") |
| 47 | + |
| 48 | + |
| 49 | +########### IF STATEMENT ########## |
| 50 | + |
| 51 | + |
| 52 | +#You may be wondering the user of all of these conditions. They are used within ifs and loops. |
| 53 | +#First, the if statement. |
| 54 | +print("What's your name?") |
| 55 | +name = input() |
| 56 | +print("what's your age?") |
| 57 | +age = int(input()) |
| 58 | + |
| 59 | +if name == "Caleb": |
| 60 | + print("Hey, Caleb") |
| 61 | +#otherwise do nothing and continue |
| 62 | +#4 spaces over for indent |
| 63 | +#THIS INDENT IS IMPORTANT! |
| 64 | + |
| 65 | +#BAD: |
| 66 | +if age > 100: |
| 67 | + print("WOW!") |
| 68 | +print("You are so old") #executes EVERY time |
| 69 | + |
| 70 | +#GOOD: |
| 71 | +if age > 100: |
| 72 | + print("WOW!") |
| 73 | + print("You are so old") |
| 74 | + |
| 75 | +#It is recommended to use 4 spaces instead of a tab. |
| 76 | + |
| 77 | +########## if elif else ########## |
| 78 | + |
| 79 | +if age > 100: |
| 80 | + print("You're old") |
| 81 | +elif age > 120: |
| 82 | + print("How are you even alive?") |
| 83 | +else: |
| 84 | + print("You may have some years left in you") |
| 85 | + |
| 86 | + |
| 87 | +########## LOGICAL OPERTATORS ######### |
| 88 | + |
| 89 | +#Logical operators can be used to make complex expressions that ultimately evaluate to true or false. |
| 90 | +#and --> both must be true |
| 91 | +#or --> one must be true |
| 92 | +#not --> negates (flips) |
| 93 | + |
| 94 | + |
| 95 | +#An example with or |
| 96 | +thunder = False |
| 97 | +lighting = True |
| 98 | + |
| 99 | +if lighting or thunder: |
| 100 | + print("Don't go swimming!") |
| 101 | + |
| 102 | +#An example with and |
| 103 | +car_nice = True |
| 104 | +on_sale = False |
| 105 | + |
| 106 | +if car_nice and on_sale: |
| 107 | + print("Buy the car!") #This will not execute! |
| 108 | + |
| 109 | +#An example with not AND and |
| 110 | +temp_outside = 50 |
| 111 | +pool_heated = False |
| 112 | + |
| 113 | +if temp_outside < 60 and not pool_heated: |
| 114 | + print("Don't go swimming!") |
| 115 | + |
| 116 | +#this can be confusing, so it might be best to read it as english |
| 117 | +#however, logically... pool_heated is false so not pool_heated is true. |
| 118 | +#therefore, both sides are true. |
| 119 | + |
0 commit comments