Legacy - IF Statements Workout
- 04:11
Learn how to build "IF" statements in PyCharm.
Downloads
IF Statement - Python Workout FilesTranscript
Let us now look at how to write IF statements in Python to start off with Python supports the usual logical conditions for mathematics such as equals, which is represented as x equals y for two variables x and y not equal to, which is represented as x not equal to y for two variables again x and y. Similarly, less than, represented as x is less than y, for greater than, is represented as x is greater than y, for less than or equal to, it is represented as x is less than or equal to y, for greater than or equal to, it is represented as x is greater than or equal to y. Now, we shall be looking at two to three different workouts on writing if statements. So, firstly, let us define a variable x equals to two and Y, which is equal to three. Now, to write an IF statement, it starts with IF and then we have to write the condition. In this case, we are trying to write a statement, which prints that y is greater than x when this condition is actually true. So, we say that if y is greater than x, colon, enter for indentation, print y is greater than x, because this is the first time we're running the file, we need to right click on the tab, and we need to click Run. So, it's printing y is greater than x, which is perfect. But what would happen if x equals two, and y equals to two, and we say that IF y is greater than x, print, y is greater than x. And if we wrote this using the shortcut, nothing is output, meaning the condition hasn't been met. But what do we do so that by Python outputs a statement saying that x is equal to y, we write a contradicting statement. So we say LIF y is equal to x, print y is equal to x, and we run it. So it's basically an output saying y is equal to x. Perfect. Now, what do we do if neither y is greater than or x and y is equal to x, but rather, y is less than x? We write a lot of statement. So we say IF x equals two and say y equals one meaning Why is less than x, we write the similar statement, IF y is greater than x print, y is greater LIF y is z equal to x, y is equal to x. Now we say else and we do not include any condition because the only remaining condition is x being greater than y. In other words, y being less than x. So Python automatically assumes that we run the code just perfectly paste that why is listed it's simple.