Transcription
Hello! In this lesson, we will study the various conditional constructs that exist in the Python language.
Before we begin, I would like to recommend a website: etipragger.com. On this site, you will find homework assignments and a lot of other useful information. The link to this lesson on the site will be in the description of this video.
To start, what is a conditional construct? With a conditional construct, we can check a certain expression, and if it turns out to be true, we will execute one piece of code. If it turns out to be false, we can execute another piece of code. Thus, with conditional constructs, we can execute different code depending on different conditions.
To implement a conditional construct, we need to use a keyword called "if." In other programming languages, parentheses are always used after "if," but in Python, all unnecessary symbols have been removed. Therefore, after "if," we do not need to add anything extra; we simply specify a certain condition.
Let's start with something very simple. For example, if 5 equals 5, then we will output something to the screen. Here, for instance, it will say "s."
This piece of code can be read as follows: if 5 equals 5, then the piece of code that follows the colon and is indented by the same amount will be executed. You may notice that the editor automatically created a certain indentation for me, and all the code that is at the same level of indentation will be executed if this condition is met.
At the moment, I have only one line of code, which will execute if this condition is true. Of course, this condition will definitely be true because 5 equals 5—it's a mathematical fact. If this runs, the value "s" will be displayed.
At the same time, if I need to execute several lines of code, I can simply move to a new line and, for example, write something like "!!!". If I run this, we will see that "s" is displayed, as well as three exclamation marks.
Thus, we have certain conditions: if this condition is true, the code that follows the colon and is indented by the same amount will be executed.
For example, let me try to specify an incorrect condition. I will say that if 5 is greater than 5—this is clearly false because 5 equals 5, so it cannot be greater. But let's say I specify this condition. If this condition is true, then only in that case will the code execute. If it is false, the code will not execute.
If I run this now, nothing will be displayed on the screen because the condition was initially false.
Let's delete all of this for now and try to write not super primitive conditions, but more complex and interesting ones. For this, I suggest creating a variable, let's call it "userdata." This will hold the information we will receive from the user, and we will get this information through the input method.
I also suggest converting it to an integer right away. Here, let's write something like "Enter a number."
We won't run this yet; let's write a check first. Below, we will write the keyword "if" and say that if "userdata" (the data from the user) is greater than 5, then only in that case will we display a message saying that the number is greater than 5.
If I run this now and enter the number 6, this condition will trigger, and we will see the message that the number is greater than 5.
But if I run this again and enter the number 4, nothing will be displayed on the screen because this check is not true; we entered a number that is less than 5. Therefore, all the code that was after the colon and was indented by the same amount did not execute.
In addition to checking for equality, we can also check for greater than, less than, greater than or equal to, less than or equal to, and not equal to.
Now, let's say I set a check in this format. When do you think this piece of code will be executed? It will be executed every time we enter a number that is not equal to 5 because we have written the following check: if the value from the user is not equal to 5, then we will execute the code that is here in the conditional operator.
So if I run this and enter the number 4, everything works correctly. If I run it again and enter the number 5, the check is now false, and since we checked if it is not equal to 5, the code inside did not execute.
You can certainly write another condition inside one condition. The main thing is to maintain the same level of indentation for all lines of code.
Let's say I will write another condition here. Again, I will check "userdata." If it is greater than 6, then we will display a message saying that we have entered this condition.
If I run this now and enter the number 7, we will see that we have entered this condition. This condition was true, so this piece of code executed.
If I enter the number 4, we will enter this condition, and this piece of code will execute, but this condition will not trigger, so the next piece of code will be executed.
An important point to note is that you need to keep track of all the indentations. Currently, my IDE (PyCharm) automatically adds the same indentations for me. If I delete one space somewhere, an error will occur because the interpreter will not understand what is happening.
This line of code belongs to this condition, and if I change the number of spaces, the interpreter will not understand to which condition this next piece of code belongs.
Therefore, it is very important to keep track of all these spaces and indentations because they indicate nesting and which code belongs to which condition.
For example, if I write "print" here with some text, this print statement belongs to this condition. If I delete one space, this print statement will belong to this condition. If I delete another space, this print statement will not belong to any condition, and it will execute regardless of whether any condition is met or not.
So, indentations are extremely important. IDEs like PyCharm will immediately indicate where an indentation is missing or where there is too much indentation.
You might say that managing all these indentations is not super convenient, but I would argue that it is actually very convenient. In other languages, instead of these colons, curly braces are used, which clutter the code with many extra lines that may just contain one curly brace.
In Python, these curly braces do not exist; instead, we have colons and just indentations. Therefore, I find this method much more comfortable for reading and writing. You just need time to get used to placing all these indentations.
Now, regarding checks, we have learned to check for not equal, equal, greater than, less than, greater than or equal to, and less than or equal to. We can also check various boolean variables.
Let's create a variable, let's call it "is_happy," to indicate whether a user is happy. We will set its value to true.
Next, I want to check this variable. I could check it here, but let's create a new condition to avoid cluttering the code. In this condition, I will check our variable "is_happy."
If "is_happy" is equal to true, then we will print that the user is happy.
Let's run this. It asks for a number, and after entering it, everything works correctly. It shows that the user is happy.
However, the code is not written correctly here. This code can be shortened significantly. We can simply remove the additional check and leave just the variable name.
If we write just the variable name, it is equivalent to saying "is_happy equals true."
If I run this now, it will still show that the user is happy.
If you need to check for false, you can write it like this, but again, this is a longer version, and usually, it is not written this way.
Typically, it is written as follows: if "is_happy" is equal to false, we can write "not" before the variable. In this case, this piece of code will execute.
If I run this now, nothing will be displayed because the variable is true, and we are checking for false.
If I set the value to false, everything will work correctly.
When it comes to boolean variables, you can simply write the variable name or add "not" before it to perform the correct checks for a certain value.
Now, let's explore additional operators that exist in conditional constructs. With the "if" operator, we can check a condition. If the condition is true, we execute the code inside the conditional operator; if it is false, we do not execute the code inside.
In addition to "if," we can also use an operator called "else." "Else" is always written at the end. If we have an "if" statement, we can use "else" to execute code that will run if the "if" condition did not trigger.
Let's write something like "user is not happy." If I run the program now, this condition will be true because the variable is true. Therefore, this piece of code will execute, and it will show that the user is happy, while "else" will be ignored.
If I set the value to false, the "if" condition will not trigger, and the "else" operator will execute, displaying that the user is not happy.
The "else" operator can be used at will and is always used with the "if" operator.
You can also use "elif," which is an intermediate operator that is written between "if" and "else." With this operator, we can check an additional condition.
Let's uncomment "userdata" so we have something to check. Here, we will check if "userdata" equals 5. If it does, we will display a message saying "the number is five."
Now, if I run this and enter the number 5, we will see that the message appears.
The first condition was false, so it was ignored. Then we checked the user data, which was equal to 5, so this condition was true, and the code inside executed.
Since this condition was true, the "else" was ignored.
You can have as many "elif" statements as you want. For example, I could check for the number 7 here.
If I run this and enter an incorrect number, say 3, all conditions will be false, and only the "else" operator will execute, indicating that the user is not happy.
If you want, you can omit "else," and it will still work correctly. If I enter an incorrect value, none of the conditions will be true, and no code will execute.
If you need an additional condition that must trigger, then you should write "else." The main thing is to maintain the structure: "else" is always at the bottom.
You can use these conditions separately or combine them as you wish.
In each of these conditions, we have only checked one variable. Here we checked "is_happy," and here we checked "userdata."
However, in each condition, we can check multiple variables or expressions. To do this, we need to use certain keywords.
For example, I want to check if "is_happy" is true and also check if "userdata" equals 6.
To combine both checks, I will use the keyword "if." I will say that "is_happy" must equal true, and "userdata" must equal 6.
To combine these checks, I will use the "and" operator. This means that both conditions must be true for the code inside to execute.
If I run the program now and enter the number 6, we will see that this operator does not trigger because the first part returns false.
The first part is false, and the second part is true, but since we have "and," both must be true for the code to execute.
Therefore, this did not work, and only the "else" executed, displaying that the user is not happy.
If I set "is_happy" to true and enter an incorrect value, say 4, the condition will not trigger again because one part is true and the other is false.
Finally, if I set "userdata" to 6 and "is_happy" to true, we will see that the user is happy because both conditions are true.
In addition to the "and" operator, there is also the "or" operator. This operator requires that at least one of the conditions is true.
If I run this and enter the number 4, the conditional operator will still work correctly because the first condition is true, even though the second is false.
As long as at least one condition is true, the overall condition will be true.
You can combine as many conditions as you want.
Now, let's take a look at the ternary operator. The ternary operator is essentially the same as "if-else," but in a shortened format.
To illustrate this, let's create a variable to receive information from the user without displaying any text.
Initially, we will write the full construct, and later we will use the ternary operator to see the difference in the number of lines of code.
Let's write a simple condition: if the user inputs the string "five," then we will set the variable "number" to 5; otherwise, we will set it to 0.
At the end, I will simply print this variable.
If I run this now and enter 4, it will show 0, which means everything processed correctly.
If I run it again and enter "five," it will set the variable to 5.
Now, let's use the ternary operator. We will create a variable "nam" and set it to 5 if "data" equals "five"; otherwise, we will set it to 0.
I can comment out the previous code, and if I run this now, we will see that if I enter "five," it will set the variable to 5.
If I enter any other value, it will set it to 0.
Thus, the ternary operator is essentially the same as "if-else," but it is written in one line.
In this case, we are saying that we will set a certain value if the condition is true; if it is not true, we will set a different value.
That’s all for this lesson. We learned how to work with conditional operators in Python.
In the future, we will often use conditional constructs, so if you haven't fully grasped something yet, don't worry; we will revisit these topics.
I hope you enjoyed the lesson. If so, don't forget to subscribe to the channel and join our social media. All the links will be in the description of this video.
That's all from me. See you next time! Goodbye!