Hey, welcome back. In this lesson we're going to cover IF-Else Statements. But before we jump into the new material, let's review Lists.
A List is a data structure in Python that is changeable with an ordered sequence of elements. Each element or value that's inside of a List is called an Item. An index is the position of an element in the List. Methods are Functions to change or modify a List.
So this week, we're going to focus on IF Else statements. Sometimes when writing a program, you may want to only execute certain code if a condition has been met. For example, services like Netflix will check your age to verify that you're old enough to watch certain movies, or some cars won't start unless all the passenger seatbelts are fastened. But how do they do that? How do they track? Well, they use an if else statement.
Today, we're introducing three concepts. 1. CONDITIONAL STATEMENTS. Conditional statements in Python perform different actions depending on whether a specific condition evaluates to true or false. 2. NESTING. There are situations when you want to check for multiple conditions. In such a situation, you can use the nested IF construct logical expression. 3. Logical expressions are the result of applying logical or BOOLEAN operators to arithmetic expressions. The result of an operation has two possible states - true or false.
First, let's go to Replit and create a new Repl and name it PGD_5_IF_Else.
So let's say Netflix wants to check your age, verify if you're old enough to watch mature movies. So how do we do that? Well, we use an IF Statement. This means if a certain condition has been met, say you're over 12 years old, then an action like watching a movie is allowed to happen. This is also known as a conditional statement.
Here's the syntax, the IF expression defines the condition. Here's an example where the program checks to see if the viewer is over age 15. And if they are allows them to watch the movie. In the example, my age is 16, which meets the condition in the IF statement, age > than 15. So the statement will execute and allow me to watch the movie. The STATEMENT is the code that will execute if the condition is met.
So what would happen if the condition was not met? Say the viewer was aged 12. Well, then the statement would not execute, the program would simply move to the next statement.
Sometimes, we need our program to handle multiple conditions or statements. One way to do that is with an ELSE STATEMENT. An Else Statement executes based on the opposite value of the same logical expression. Does that make sense? So when the IF statement is false, it will execute the code in the ELSE statement. Is it confusing? Don't worry about it. Let's take a look at an example.
So here's the syntax, the IF expression defines the condition. IF the viewers age is old enough to watch this movie, then it will execute the statement. If the viewer, is too young, say the age is 12, then the program will move to the Else Statement and execute that statement. In this example, my age is not greater than 18. So the IF statement is false, and the program moves to execute the else statement.
Sometimes you have more than two conditions you want to check. You could write a bunch of expressions but it's really time confusing and kind of difficult to follow. Or you can use ELIF . The ELIF is short for ELSE IF and it allows us to check multiple conditions. Okay. Sounds really useful, right? But alright, how does it work?
Python will evaluate the IF condition and when the condition is true, then Python will execute the statement. When the IF condition is not true, then Python moves on to the Else IF expression.
Here's the syntax. Now, let's follow the logic. When the IF expression is true, then Python executes the statement. When the IF expression is false, then Python moves to the ELIF expression. When the ELIF expression is true. Then Python executes the ELIF statement when the ELIF expression is false then Python executes the Else statement.
Here's an example.
IF age > than 15, then print “You are old enough”.
Else IF age is less than 10, then print, “You can watch a Disney movie”.
Else print “Hey, you can watch a Disney movie!”.
What would happen if the age is equal to 11? Well, in this example, both of the first two statements are not true. The viewer was not older than 15 or younger than 10. So the conditions are false. The body of Else is executed.
IF statements also work with all data types, not just numbers. Here's an example of an IF ELIF Else statement with a string. What would happen if I use the surname “doctor”? Here's an example of an IF Else statement checking Boolean types. And here's an example of an IF Else statement checking floating point numbers.
Alright, that's a wrap for today. Great work and I'll see you again soon.