If-Else Statements in Python

Free Video Tutorial and Guide

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.

Python for Data Science Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

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.

photo of Noble Desktop

Noble Desktop

At Noble Desktop you can learn how to code websites, build iOS apps, make graphics, and more. From our front-end coding bootcamp to Photoshop classes, we offer a variety of comprehensive day, evening, and weekend training classes as well as certificate programs in web design, web development, HTML email, and more. Courses are available in-person (at our training facility in Soho), live online, and customized corporate or private training. Our course materials and workbooks are used by colleges and schools worldwide. Learn a skill today and start using it tomorrow.

More articles by Noble Desktop

How to Learn Python

Master Python with hands-on training. Python is a popular object-oriented programming language used for data science, machine learning, and web development. 

Yelp Facebook LinkedIn YouTube Twitter Instagram