Notes - Wednesday 12/30/20
12/30/20 Wednesday
I started Python statements on 12/28/20 Monday but didn't get very far. So I'm including it here.
Python Statements
Let's learn about stuff like If then statements. I don't know a lot of statement examples lol...
if, elif, and else statements
These statements will introduce the idea of control flow. This is where we build a lot of code but only want certain parts of the code to execute depending on certain conditions. For example, if
my dog is hungry then
I will feed the dog. This code will be executed in this condition but otherwise the code will move on to the next part if the condition isn't met.
Control Flow Statements
Some keywords used in control flow are:
- if
- elif
- else
These statements use syntax that takes advantage of whitespaces/indexes and colons :
. The whitespace/indexes are unique to Python and how Python
if statements
if Statements are self explanatory. They begin the block of code that you want to be executed in a certain conditions or situation. Below is the basic syntax. Note that line 2 is indented. Line 2 will be executed when the if statement in line 1 is True and met.
1 if some_condition:
2 execute_some_code_here
if/else statement
It's like a juiced up if statement. The if statement only executes when the condition is met. And then that's it. if/else will execute when the condition is met otherwise it will do something else. That's why it's called if/else. Below is the basic syntax. Note the lines are indented.
So if the some_condition is met in line 1, then the line 2 code will be executed. Otherwise line 3 will trigger and then the line 4 code will be executed and done. The line 4 else line doesn't have a condition associated with it. This is because it relies on the line 1 condition being True.
Syntax-wise, note that the if and else in lines 1 and 3 are lined up.
1 if some_condition:
2 execute_some_code_here
3 else:
4 execute_some_more_code_here
if/else statement with elif statement
In the if/else statement above, if you want to check for other conditions before the else statement kicks in, you can add in some elif statements. elif is Else If so you can stack if statements.
1 if some_condition:
2 execute_some_code_here
3 elif some_other_condition:
4 execute_some_other_code_here
5 else:
6 execute_some_otherother_code_here
Examples on how the above statements would work
if statement
Let's say that we want our code to have a variable called 'hungry'. If it's true, then I want to print out 'Gimme food!'.
So our example would look like the below. And when the code is run, we would get an output of Gimme food!
. If we set 'hungry' to false, there's no output. This is because the if statement isn't activated so there's no expectation to see anything.
1 hungry = True
2
3 # hungry set to True would output "Gimme food!"
4 # hungry set to False wouldn't output anything
5
6 if hungry:
7 print("Gimme food!")
if/else statement
Now with the above statement, let's say that that when hungry = False, we want to see an output of I'm not hungry
. This is so that when the False condition is met, we get some kind of output instead of nothing like above.
1 hungry = True
2
3 # hungry set to True would output "Gimme food!"
4 # hungry set to False wouldn't output anything
5
6 if hungry:
7 print("Gimme food!")
8 else:
9 print("I'm not hungry")
10
11 # see how the else statement is aligned with if and print is indented
if/elif/else Statements
Let's say I want to make a shitty guide around town about places. I want to ask the bot about locations and have them described to me.
Locations:
- Auto shop - "Car library"
- Bank - "Money storage"
- Other not listed locations - "That option isn't available"
I can setup an equality statement to check and see if my variable is 'bank' or "auto shop'.
Example if/elif/else commented code:
1 location = 'Bank'
2 # location is set to bank as a variable
3
4 if location == 'Auto Shop':
5 print('Car library')
6 # I'm checking with an equality statement to see if the location is Auto Shop here. If the location
7 is Auto Shop, then the description will be "Car library".
8
9 elif location == 'Bank':
10 print("Money storage")
11 # I'm checking with an equality statement to see if the location is Bank here. If the location is
12 Bank, then the description will be "Money storage".
13
14 else:
15 print("That option isn't available")
16 # I'm checking with an equality statement to see if none of the defined locations are listed, the
17 code will output a generic "That option isn't available.
Example if/elif/else code w/ no comment:
location = 'Bank'
1 if location == 'Auto Shop':
2 print('Car library')
3 elif location == 'Bank':
4 print("Money storage")
5 else:
6 print("That option isn't available")
Remember that we can stack elif statements so for example, let's say we want Library to output "book storage"
location = 'Bank'
1 if location == 'Auto Shop':
2 print('Car library')
3 elif location == 'Bank':
4 print("Money storage")
5 elif location == 'Library':
6 print("Book storage")
7 else:
8 print("That option isn't available")