- Hello! Welcome to the Path to Programming; the audio supplementary materials for introduction to computer science! - I am a former student that took this introductory course and today I want to talk to you about conditionals! - Life is filled with making decisions, every moment you weigh the options at hand and make a decision based on what will be most useful for you at a given moment. If it is sunny outside you take your sunglasses, if it is raining outside you take an umbrella with you, if you are hungry you eat food etc. Well you’re not the only one that makes decisions, so can your computer! - How does your computer make decisions, and what goes on behind the scenes for your computer to make a decision? - Today we will talk about booleans which help your computer make decisions, and conditionals which will actually make them. - We talked about different data types in the past weeks like int, float, str (string) and input. - Bool is another Python data-type! - A bool can have two values: it can either be True or False. - These are called Boolean values, named after George Boole, who invented algebraic logic. - The first letter of each Boolean values must be capitalized when you are writing code. - To be able to get Boolean values you need to be able to compare other values. Booleans naturally arise when you compare values, for example, if you assert that a variable x is greater than 5, that comparison will be either True or False. - In order to compare values you need to use relational operators. - There are many relational operators and you should go over each one to make sure that you understand the purpose and meaning of each one. Some of the relational operators are "greater than," "less than or equals," and "not equals." - You can use relational operators to compare different types of values. For example, just like in math you can use it on floats or integers. - When you execute the line of code that states “Five is greater than six” your computer will give you the Boolean value of False as you would expect, just like a mathematical operation that you are used to. - But relational operators are not limited to numbers, you can also use them on strings! This can be done in Python because Python automatically assigns a value to each character in a string. - For example if you execute the line of code that states “bat is less than cat” your computer will give you the Boolean value of True. This is because b comes before c in the alphabet therefore b is assigned a lower number compared to c, and therefore is less than (just like when sorting a dictionary, only if the first letters of two strings are tied will the second letters factor into the comparison). - One thing to look out for when comparing string values in differences in upper and lower case letters. - In most programming languages uppercase letters come before lowercase letters, meaning lowercase letters have a higher value associated with them. So if you executed the line of code that states “uppercase C cat is less than lower case b bat” you would get the Boolean value of True since lower case b has a higher value (97) than the upper-case C (67). - Some other logical operators that you will be using are not, and, or. These logical operators work just like they sound in English. - The not operator evaluates to the opposite of the truth value. For example if you executed a line of code that states “not parenthesis three is greater than five close-parenthesis“ you would get True because three is not greater than five. - The and, or operations are kind of easy to mix up but the most important difference between them is that the "and" operation is used when you want to check that ALL of the statements you are making are true. When you are using the "or" operation you are checking if AT LEAST one of the statements you are making is True. - For example if you executed a line of code that stated “(Three is less than five AND bat is less than cat)” you would get the boolean value of True since both statements are true. And if you executed a line of code that stated “(Three is greater than five OR bat is less than cat)” you would still get True, even though one of those statements is false, as three is not greater than five. But that doesn't matter because the or operator checks to see if at least one of the statements made is True. - Something that really helped me distinguish between using "and" and "or" was making Truth Tables. - A truth table is when you take two expressions and evaluate what the resulting boolean value would be based on the different operators you are using and the different bools the expressions can have. Making your own truth table is a great exercise if you feel stuck trying to figure out the differences between "and" and "or." - Understanding the differences between them is really important because as you improve at coding you will start combining logical operators. Meaning you will execute lines of code that feature multiple logical operators. So you could be executing a line of code that has multiple and’s and or’s! - That’s why it’s really important to know how to keep track of the results. In those cases, you should always use parentheses to explicitly specify how the operators are grouped with their operands. - Now that you have the tools to evaluate boolean values by using logical operators, let’s get more into conditionals! - A conditional is a block of code that starts with the word "if," followed by a condition expression which will evaluate to a Boolean value. When you write if, your computer can’t just run the code like it normally does, it has to make a decision! - That’s why using conditionals is very powerful and now that you have all the tools to code a conditional let’s get into how you can actually code it. - The first thing your computer does when you execute an if statement is to gather all of the lines of code which are indented at the same level as the first line of code following the if line. - This is why indenting is very important, you want to tell your computer which lines of code to apply the if statement to, and you do this through indentation. - If you indented correctly the condition expression that you have written using relational operators will be simplified and you will get a boolean value of either True or False. - If the boolean value of your conditional expression is True, then the lines of code that you had written will be executed, but if it was False then that block of code will be skipped over. - As you can see, using conditionals is very powerful, because you are essentially telling your computer to only execute some code when you know a condition you want is satisfied. By using conditionals you can make your computer essentially make decisions! - Just to go over what we talked about: - There is a new type we learned about called Boolean. A boolean type has two possible values, either True or False. - These are called boolean values. - You can write a conditional by using the keyword "if" and use relational operators in your conditional statement, and essentially tell your computer which line of codes you want it to execute and which ones you want it to skip over. - The power of if statements is definitely undeniable! But you are not just limited to if statements because you can also have else and elif. - What do else and elif statements do? Let’s say you wrote a block of code (indenting multiple lines at the same level) after an if statement and then you wrote a new block of code (indented at the same level as the if block) putting an else before it, indented to match the if. - In this situation, if the first block of code with the if statement was skipped over because the condition of the if was False, then you are guaranteed to execute the else block of code (we call these branches). - Essentially using an else statement with an if statement guarantees that you always execute one branch or the other. Because if the if branch isn't executed the else branch always will be. And vice versa, if the if branch is executed the else branch will always be skipped over. - This means that the if and else branches are mutually exclusive. This quality is what ensures there is always some branch of code that is executed no matter whether the if condition evaluates to True or False! - Besides the else statement there is also an elif statement. - If/else allows you to create two mutually exclusive branches, but what if you need more? - For example, the outcome of a game might be a win, a loss, or a tie. In this case, we might want three different things to happen depending on these three possibilities, and we can add an elif branch between our if and else branches to achieve this. - If a block of code after an if statement block of code (indented at the same level) starts with elif, the condition on the if block is evaluated first, and if it's true, the elif will be skipped, along with the else. - But if the if condition is False, the elif condition will be evaluated, and if it's True, that block will be executed (and the else will be skipped). - If both the if and elif conditions are false, any further elif cases will be checked, and if their conditions are all False, a final else block will execute (if it's present). - Essentially the elif block of code allows you to check multiple expressions for True one-by-one, and execute a block of code as soon as one of the conditions evaluates to True. - Elif and else statements are not required when you are writing conditional statements, the only required statement is the if statement. But they can be very powerful tools, and you will mostly likely need to use them. - Another important note to add is that an elif statement can be used multiple times in your code following an if statement, to set up as many branches as you need, however there can only be one else statement following an if statement! - If this all seems hard to follow, don’t worry. When you are thinking about executing conditionals take each statement one step at a time and evaluate whether each statement will be a True or a False and based on that move on to the next statement. - A very helpful visual tool can be making flow charts. You can basically draw a diagram of what will happen when each line of code will be executed. - At a high level, Booleans are Python's way of representing True or False, and conditionals are set up using if, elif, and/or else to allow Python to use Booleans to make decisions about whether or not to execute certain blocks of code. - To create conditions for conditional statements, we usually use comparison operators or custom functions that return Booleans to specify the conditions that we want to use. - Put all of these things together, and your code can be a lot more flexible than what's possible just using function definitions alone. - This was Path to Programming hope! It was great talking to you, see you next time.