Learn how to use "if" and "else" statements in Python, and understand the integral role of Boolean data types in logical operations. This tutorial video explains the importance and application of these concepts in Python programming.
Key Insights
- The tutorial introduces the use of "if" and "else" statements in Python and highlights their key role in coding logic.
- Boolean data types, represented as 'True' or 'False', are fundamental to conditional logic in Python.
- A single equal sign is used to assign a value in Python, while a double equal sign is used to check equality.
- The "if" statement in Python evaluates an expression and executes the subsequent code if the expression returns 'True'.
- The "else" statement in Python executes code when the preceding "if" condition is not met, i.e., when it returns 'False'. It doesn't require a condition like "if".
- Apart from simple equality checks, Python's "if" statement can evaluate other conditions like greater than or lesser than.
In this video, we're going to look at how to use IF-Else Statements in Python
Video Transcription
Hi, my name is Art, and today I'm going to show you how to use if and else statements in Python. Even else is very important, because you always want to use some logic in Python.
So, first I want to explain you the built-in data type called Boolean in Python. It works like this—suppose I write 2 times 2. We all know that 2 times 2 will get us 4, but let's say I doubt and I want to check.
Here, I'm going to use a double equal sign. When I get to this point, people always ask me why do I use a double equal sign. So, let me explain—if you don't remember or maybe you don't know, single equal sign is when we're assign a value, and double equal sign means equal.
I'm comparing if 2 times 2 will get me four. Let me run this and you'll see, I'm getting Boolean with a capital T—true. Boolean is very simple—there are just two of them—true or false. So, 2 times 2 equals four, that's true. Now, if I decide to say 2 times 2 equal 5, I'll get false.
All right, so you see that's pretty much it. So, what Python does, it evaluates this expression and then returns true or false. Now, that's the main idea behind if statement. So, in if statement we could write it like this—if 2 times 2 equal four, then I want to print something like yes. And you see, I'm getting yes.
So, let me explain the whole syntax. If evaluates this expression and if it returns true, then it does whatever we have here within the scope of the if statement. Don't forget to use indentation—indentation supposed to be four spaces.
After the colon, you need to use indentation, and again what it does, it evaluates this expression. So, if I place true here, I'll always get yes. However, if I decide to place false here, then I'll never see yes, because it's false. So, what if statement does, it evaluates that statement and if it's true, then it prints yes. If it's false, then it does nothing.
So, let's place this—2 times 2 equal 4 and that's true. However, we could replace this four with a number, and using function input, we could ask user pretty much for any number. So, how will I do it? I'll use a function int to convert my string into integer. I'll use input and here I'm going to write something—"give me a number".
All right, now a number will do. Suppose I'll place here 10. I'm not getting yes, because now 2 times 2 not equal to 10. However, if I place here 4, then I'm gonna see yes, because now this number is 4 and 2 times 2 is 4.
All right, so this is pretty much if. So again, if evaluates this expression, and if these lines returns true, then it does something. And then there is else. Else comes with no condition—rookie mistake people trying to squeeze in a condition, but there is no condition. So, else comes with nothing there and prints. And here I'm gonna do this—else.
So, suppose I'm gonna type in 50—else 50. So, you see the way actually works—it checks and if this condition returns false, then we get into else. Else will capture pretty much anything else. If you do like any number, then it will be else. However, if you do four, since two times two equals to four, then we're gonna get yes. So, else will capture anything else.
So, this is like a decision tree—if and else. All right, so pretty simple. And again, if you replace it with a true, so now no matter what number is, it's always going to be yes—why? Because it's true. So, no matter what we insert there, it always will be true, so it's always going to get us something.
However, there is another option—we could say if number is greater than 10, then we want to print yes. Let's run this—"give us a number". 20—20 is greater than 10, so we're getting yes. However, if I do 5, I'm gonna get else, because this condition returns false. Right, so if it fails, then it goes to else. Pretty simple—if condition is met, then we're getting whatever we have here. Otherwise, we will go to else.
All right, so that's pretty much it about if and else statements. Watch my other videos so you could learn how to use list comprehensions, how to use for loops and while loops, and many other stuff. All right, see you in the next video.