From the course: Programming Foundations: Fundamentals

Making decisions in code - Python Tutorial

From the course: Programming Foundations: Fundamentals

Making decisions in code

- Have you ever received a note from someone that read, "Would you like to go out?" And the options were yes, no, or maybe. As humans, maybe is often a valid response to a question. It's not quite yes, but not exactly no. However, computers are different. Things are absolute. There's only yes or no, true or false, one or zero. Any expression that breaks down to either true or false is called a Conditional or Boolean expression. Yes, that would evaluate to true. But is it greater than five? No, then it will be false. And finally, here's an example that doesn't involve numbers. Is my name Frank? Nope, it's Annyce. This would evaluate a false as well. These are Boolean expressions. But as we've learned, computers don't speak our language. We can't just ask a questions directly the way we have it here. And that's where relational operators come into play. We've already worked with several operators, we've used arithmetic operators, like the plus for addition, or the percent sign for modulo. We've also used the equal sign for assignments. Relational operators work with two operands, and they return a value, true or false, based on their relation to each, thus the name. The first relational operator we'll look at is the equality operator. It's represented by a double equal sign, and it's used to evaluate the equality of the operands. As an example, four double equals two times two will be true. The left side of the operator equals the right side of the operator. Here's another example. We have a variable called name. And we've said it to equal to the string Annyce. to compare it to the string Frank. The result will be false because these two strings are not the same. when you mean to test for equality. So be on the lookout for that potential bug if your program isn't doing what you expect. Let's come over to our python shell, and we'll do a rapid fire round of conditional checks. Let's start out easy. First, we're going to start our shell. We got back false, because of course, five is not the same as four. But what If we wanted to check the fact that they are not the same? Then we would use the not equal sign. That looks like this. Five with an exclamation mark, no space, When we hit Enter, we get back true. Let's try another one. Let's check for five being greater than four. that you already know from mathematics. So we'll put our five, space, the greater than sign, and then the number four. When we hit Enter, Now if you forget which way to place the angle bracket, just remember the hungry alligator opens his mouth to the bigger number. Okay, let's look at another example. then we're checking for five being less than four. So let's do that now. Five, space, less than sign, and then the number four. And of course, we get back false Are you starting to get the hang of relational operators now? Let's do a few more. This time, we're going to create some variables. First, we're going to create a variable named age, and we'll set it equal to 15. Then, we'll create another variable named age to drive. Now, let's do some comparisons with these two variables. Now, let's do some comparisons with these two variables. First, we'll check if age is equal to, First, we'll check if age is equal to, using the double equal sign, age to drive. using the double equal sign, age to drive. And when we hit Enter, we get back true. And when we hit Enter, we get back true. Here the Python interpreter is comparing the values of the two variables, is comparing the values of the two variables, and yes, the value 15 is equal to the value 15. and yes, the value 15 is equal to the value 15. There are a few other relational operators available in Python as well. available in Python as well. Here's a listing of them and how they're used. Here's a listing of them and how they're used. You can compare if two numbers are greater than or equal to, You can compare if two numbers are greater than or equal to, less than or equal to each other, and much more. less than or equal to each other, and much more. I've created a worksheet in the exercise files I've created a worksheet in the exercise files that will help test your skills with Boolean expressions that will help test your skills with Boolean expressions and relational operators. I really encourage you to spend the time going through each item. going through each item. This will help you to master Boolean expressions.

Contents