Learning Python, Day 10 — Conditionals
I was very close to not doing any coursework today. I wasn't really in the mood. But then every time I think "I'm not in the mood", Gurney Halleck bursts into my house, drops his baliset and shouts "Not in the mood?! Mood's a thing for cattle and love play, not fighting."
Okay, Gurney, okay.
After all the variations on lists yesterday, today is focused on "if/then" statements and other conditionals, which are always a bit of fun for me.
12 — Conditionals & 13 — If Statement Examples & 14 — If Statement Variants Examples
I did not realise that elif was a shortened version of "else if". The previous instructor didn't mention that at all, and now I'm annoyed even more with them because come on, it's so much easier to remember if I know what it's for.
It took me a few minutes to remember that I had to include the : and then indent the resulting action, but once I saw it again, I could determine if I could be in a mood.
(This is going to be an experiment where, rather than giving screenshots of my code, I'm going to put the code directly in here. Let me know what you think.)
Paul = ["cattle", "loveplay"]
if Paul[0] != "cattle":
print("You can be in a mood")
elif Paul[1] != "loveplay":
print("You can be in a mood")
else:
print("Defend yourself!")
>> Defend yourself!
I'm not 100% sure when I'd have the If/Else in a variable like the instructor showed in the examples, but it is kind of fun.
Paul = "not in the mood"
Atreides = "Defend Yourself!" if Paul == "not in the mood" else "You can be in a mood"
print(Atreides)
>> Defend yourself!
I like adding a bunch of elif statements in, because I am pedantic.
Paul = "Not In The Mood"
Mood = ["Cattle", "Loveplay"]
if Mood[0] != "Cattle":
print("You can be in a mood")
elif Mood[1] != "Loveplay":
print("You can be in a mood")
elif Paul != Mood:
print("Defend Yourself!")
>> Defend yourself!
(I know that with that last bit of code, I could just go if Paul != Mood but then how can I check that he isn't cattle or loveplay?)
The instructor did throw me for a bit, because he started including code that doesn't actually work with Python and then only caught it at the end. Which does not help. Surely you'd actually just re-record it? Like, if you catch yourself using && but you meant and? Why would you leave that in?
But showing me and means you also showed me or and, finally, we have Gurney Halleck's test.
Paul = "Not In The Mood"
Mood = ["Cattle", "Loveplay"]
if Paul == Mood[0] or Paul == Mood[1]:
print("You can be in a mood")
elif Paul != Mood:
print("Defend Yourself!")
>> Defend yourself!
Time for a Shield Knife Fight!
Day 10 — Results
ifstatements need a:at the end and then indent the results.- You can put an
ifstatement in a variable, but you can't have anelif, just anelse. - You can add as many
elifs after your initialif. You can also putifs within your initialif, but it can get tricky. - You can also have
andandorin yourifto give additional conditions.
And although I am not cattle or loveplay, I am not in the mood to jump into Loops just yet, so we're going to save that for another day.
Today's Sticker

A throwaway line in an episode of If Books Could Kill becomes a Patreon Discord joke and then becomes an official piece of merch. Behold! The Guy!