Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 2: Guess that number game
Lecture: Core concepts: Conditionals and truthiness

Login or purchase this course to watch this video and the rest of the course contents.
0:00 One of the fundamental components of any programming language is the ability to make decisions, decide to go down one path or the other
0:08 based on some tests you can run. And in Python, this is the if, elif, else block, so let's look at this co de sample we have here.
0:16 We have some command we are trying to get from the user, imagine this is a command line app you can enter commands
0:21 to run or execute at this point in our application. So we are going to ask the user to enter
0:26 either L or X but of course they can enter anything, right, that's just open text they are going to type in, so we also account for that.
0:34 We have these two commands we are going to work with, let me say if the command is equal to L the string L,
0:39 the way you do equality in Python is double equal so command == L then we are going to indent and run some code we are going to list the items
0:47 if it's not that case that it's L, then we want to do another test, we'll say well maybe if it wasn't L,
0:53 else if, which is abbreviated in Python to elif, the command == X so maybe the command is X, in that case we're going to call exit
1:01 and if it's neither of these two things we expect, well we can just print some message like
1:06 sorry I don't know what you meant by whatever it was you typed in this is not a valid command. So these conditional statements always start with if,
1:13 and then they optionally have an else if and they optionally have an else. And of course, this is a key building block
1:20 for our game to decide if the number is too high or too low. So, we've seen that you can do equality test, ==,
1:27 we can also do less than "<", greater than ">", these types of comparisons that are pretty conceptually straightforward.
1:34 But in Python, you can test any object, anything that is a variable you can put into an if statement and it will either evaluate the true or false,
1:42 and there is a simple set of rules around when a thing is going to be true and false a lot of code in Python leverages this concept
1:50 for conciseness which I am calling truthiness. So the rules are like this, there is a set of defined things that are false,
1:56 obviously the word False capital F is itself false, but also collections, lists, sets, dictionaries, and so on that are empty,
2:07 by the fact that they are empty, that considers them to be false. Strings, treat the same way, for all sorts of numbers,
2:15 if the number is 0 then it is false, otherwise it is true. And of course any object can point to an actual thing or it can be pointed at nothing,
2:23 in Python we call this None, other languages call it null or nil, in this case, when you have a variable that points in nothing, that is also false.
2:31 So, if you are not in this short clear set of things that are defined to be false, then it evaluates to true.
2:37 So we'll see a lot of code leveraging this concept around loops, around conditionals, and so on.

Talk Python's Mastodon Michael Kennedy's Mastodon