Comparisons10:56 with Craig Dennis
You can compare objects. Let's explore how.
So we just checked out the ability to check out the equality between objects, 0:00 the double equal sign right? 0:05 So let's explore some more comparison operations. 0:06 Let's explore these in the shell first, so we'll say Python. 0:10 Now to check and see if values are not equal, 0:14 we use the exclamation mark and then equal sign, right? 0:18 So that exclamation mark there is sometimes called bang, 0:23 you'll hear people say bang. 0:26 So bang equals is not equals. 0:27 So, hot-dogs are not equal to sandwiches. 0:30 True, now unfortunately that's not solving the age old debate. 0:35 It's just saying that the strings aren't the same. 0:38 And speaking of an age old debate, my daughters are 6 and 3. 0:41 You can use the greater than symbol to compare numbers, so 0:45 6 greater than 3, true. 0:49 And this confirms much of to the disappointment to my little one, 0:51 that my eldest is indeed older. 0:55 She's greater in age. 0:57 And just to double check we can do the reverse, is 6 less than 3? 0:59 I'm sorry sweetie. 1:04 So, there will be cases where you'll want to check if a value is equal to or 1:05 greater than another. 1:11 So you can combine that with a pretty handy operator, 1:14 you can say, is 42 >= 42? 1:19 And as you can see it compares each of these operands. 1:24 That's what these values are on both sides, they're called operands, and this, 1:30 they're being operated on by this operator. 1:34 It says are they greater than or equal to each other? 1:36 Now, real quick, you can do this with strings as well. 1:39 So we could say, is sunshine greater than rain? 1:44 Which is obviously true, but that's not why it's returning true. 1:49 This is doing alphabetical order, s comes after r, it's greater than r. 1:53 So here's how I want to use our new comparison operators in our program. 1:58 So, what happened was, I showed this program to my six year old and 2:03 she put in her name and the program suggested that she start learning Python. 2:06 Which, I obviously totally agree with, but 2:11 she should really learn to read a little bit better before diving in. 2:13 So, let's do this. 2:17 Assuming first name is an r name or r fellow student's name, 2:19 we should ask for their age. 2:23 So if it's less than or equal to, let's say six, 2:26 let's suggest that they learn to read first and then learn Python. 2:29 So we can do that, right? 2:34 Now, it might not be completely clear to you, but 2:35 it's totally fine to nest an if statement. 2:38 So let's do that, let's come in here inside of our else here, right? 2:41 So it's not us, it's not here so, we don't know who they are. 2:46 So let's go ahead and say age equals input, how old are you? 2:50 And we'll give some space, and 2:56 now remember that input always returns a string. 2:59 But we actually need here what we need is an integer, cuz we're gonna compare to it. 3:04 So, let's coerce what is returned into an integer and you can do this, 3:08 we can do this by wrapping it. 3:13 We can say int, and then on this other int here, we can do this, so what's happening 3:15 is we're coercing whatever is returned from here, which is gonna be a string. 3:19 So we're gonna take the string and put it into here. 3:24 That make sense? 3:26 So we only want to do this if they are less than or equal to six. 3:27 So if age is <= 6, 3:34 I'm gonna start my body. 3:38 And we'll say print. 3:42 Wow, you're like five, four, three, 3:45 two [LAUGH] if you're confident with your reading already. 3:50 And then we'll just go ahead and say what we were gonna say. 3:57 We gotta format this, we'll push in the age and that'll automatically happen. 4:00 I'm gonna make sure that I'm balanced there, okay? 4:04 Great, and so what's gonna happen, 4:06 they will only see this message if they're less than 6, less than or equal to 6, and 4:09 then it will print and should tell when to learn Python. 4:14 Cool, so let's go ahead, let's make sure that we save this. 4:17 I'm gonna come in here, drop out of the console. 4:20 And I will clear and will say python hello.py. 4:26 So run this with my daughter, her name was Hattie and hello, Hattie, how old are you? 4:33 So awesome, we're in the else, we've got a new input here. 4:38 We'll put in 6, which will be a string, I'll turn it into an int. 4:40 It says wow, you're 6, if you're confident with your reading already, 4:43 you should totally learn Python, Hattie, awesome. 4:46 Now if you read this script from the very top, 4:49 it should be pretty clear to you what's going on. 4:51 Well, I mean especially since we wrote it together. 4:53 But, there's a little bit of logic here that is particular to a use 4:56 case that I just introduced. 5:00 It might be a little confusing as to why we are asking for age here. 5:02 So there is a concept called comments, and 5:06 what you can do is you can put in a sentence here. 5:09 So let's do that. 5:13 So to create a comment, you just put in a pound sign, also known as a hashtag, 5:15 depending on your age. 5:18 And then we just put our comment. 5:20 So we can say, this is just in case we 5:21 have a younger user who can't yet read. 5:26 And comments are great. 5:33 And at this point in your learning journey, feel free to put them everywhere. 5:35 A lot of students like to take notes for 5:39 the different parts of the program to write in the comments. 5:40 And that's totally fine. 5:43 You'll see as this language becomes second nature that you'll probably need fewer 5:44 comments. 5:48 Now comments are great for 5:49 reasoning what might not be clear to readers of your code. 5:51 However, if you're needing to explain what a line of code is doing, 5:54 you might want to think about rewriting that line of code, or 5:57 breaking it down until it's more easy to understand. 6:01 Writing readable code is a skill that improves over time and 6:04 we'll build up your skill set together. 6:07 One of the most wonderful things about Python in particular is how easily 6:10 the language can be read. 6:13 Now part of this readable comes from the required indentation of the language. 6:15 This code will not work if I remove some of the indentation. 6:20 So watch this I'll go ahead and I'll take a little bit of this age off. 6:23 And right away, see how it turned red? 6:27 It's like, stop. 6:29 So I'm gonna go ahead and save this even though that's red. 6:30 And let's run it and see what happens. 6:34 And you'll see that the error's actually on line nine, and not line 10. 6:37 And it's saying that there's an indentation error. 6:44 So what I want you to remember, is to remember to look around when that happens. 6:46 It's saying 10 but the error is actually on line nine. 6:50 Let's clear this up. 6:54 Phew, that feels better already. 6:55 Speaking of reading code, what do you say we read through this one more time. 6:58 So we came in here with Hattie, and first name now equals Hattie. 7:03 And we say, hello Hattie? 7:07 And then we check to see if first name is equal to your name, 7:09 it's Craig here in my code. 7:12 And since it doesn't, we drop down to the next else if. 7:14 And it say is equal to our student Maximiliane. 7:18 It doesn't, so therefore, 7:22 since none of these if statements turned out, we run into our final else. 7:24 And that's here, and we ask input from our user of how old they are. 7:30 And this comment directly above shows us why we might be doing that. 7:36 The input function always returns a string. 7:40 And we need to coerce that to an integer, 7:44 because we want to compare that in our next line here. 7:48 We wanna say, are you less than 6? 7:52 Less than or equal to 6. 7:54 And because of my experience, 7:55 7 is really about the time that you don't need to focus too much on reading. 7:57 So much growth happens at five and six, but I don't think you're quite there yet 8:01 reading level wise. 8:05 So this definitely could read less than seven, which is essentially, less than or 8:06 equal to six, right? 8:10 Now, if that expression was true, we print out this message. 8:12 And when we call the format string, we're passing in an integer but 8:18 it's automatically coerced back into a string for us. 8:21 And, of course, that only happens if the age is less than 6. 8:25 Then everybody is suggested to learn Python. 8:29 And then, finally, have a great day. 8:33 So how'd that feel? 8:36 Did that all make sense? 8:37 Now if it didn't, let me remind you that you can rewind me. 8:39 Just scrub your video player back a minute or so, and 8:43 you can walk that code all again. 8:45 Now if it wasn't all crystal clear, remember, that is totally fine. 8:47 This very well could be your first time looking at code. 8:52 Think about how much of it you actually did understand. 8:56 And then try to compare it to what you knew before we met each other 8:59 way back when in that first video. 9:03 It's kind of a lot of new stuff, isn't it? 9:05 Great job immersing yourself, stick with it. 9:07 You got this. 9:09 If you have questions please ask them. 9:10 Every one is super nice in the community, and chances are someone might be having 9:14 the same question that you're having,and they just haven't asked it yet. 9:18 Check the questions attached to this video. 9:22 Can you help somebody? 9:24 I want to reiterate that you're doing great learning the basics. 9:26 Taking input and then branching to display different output 9:29 based on various conditions is how most applications work. 9:33 Think about how most websites, that require you to sign in, 9:37 actually work behind the scenes. 9:40 Now if you aren't logged in, it shows you the buttons to Login or Sign Up. 9:42 If you click the Login button, the form shows up, and you input your username and 9:46 password. 9:50 Now, in the case that you are logged in, it shows your profile picture, but 9:51 you don't see the buttons. 9:55 Think about what that code might look like. 9:56 If the user is logged in, we display the user's avatar. 9:59 Otherwise, we need to show the buttons. 10:05 Now that display profile avatar is a function that we would need to write. 10:08 And we'll get to that in a bit, but see the branching logic at play. 10:13 See how the output is using a boolean variable to determine which branch 10:16 should work. 10:20 Also, that login input, that's just taking input, right? 10:21 Just like our command prompt. 10:25 It returns what the website user entered, so 10:27 that the values can be used to log the user in. 10:29 You just learned a bunch of concepts. 10:33 You've got the building blocks that are present in all sorts of applications that 10:34 you use today. 10:38 I bet you'll start looking at your apps a little bit differently as you start to 10:39 use them. 10:43 I am excited for what's coming up next. 10:44 We're gonna take a look at writing our own functions and 10:46 how to use loops to repeat your code. 10:48 I can't wait to get those functions and 10:50 loops added to your ever growing bag of tricks. 10:52 Let's do this. 10:54
You need to sign up for Treehouse in order to download course files.Sign up