Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Using return Statements With Conditionals

If you’d like a refresher on absolute values or would like to learn more about finding them in Python, then check out How to Find an Absolute Value in Python.

00:00 In this lesson, we’ll look at best practices using return statements with conditional statements. A Python function can have more than one return statement.

00:10 The first one encountered ends the function execution. Typically, you’ll see this when the function has conditional statements, when the return value depends on the result of some condition.

00:24 As an example, let’s consider the absolute value function for mathematics. The absolute value of a real number x is the number itself if that number is zero or positive, and the absolute value is the opposite of that number—you change its sign—if the number is negative. The negative sign in front of the x means just to change its sign. So if x is negative, the negative of that is going to be a positive number.

00:54 We can write a version of this in Python. Python has a built-in function for absolute value called abs(), so we’re calling this my_abs() to distinguish it from the built-in one.

01:06 It reads almost the same way as the mathematics one. This function takes a number as an argument. If that number is greater than or equal to 0, meaning if it’s zero or positive, we return that number.

01:21 Otherwise, if the number is less than 0, meaning it’s negative, we return the opposite of that number, and the opposite of a negative number is positive and we will get a non-negative number in each case. Let’s try this out.

01:42 We can call this function on a positive number and get the same number back, we can call this function on a negative number and get the positive number back, and we can call this function on 0 and get 0 back.

01:59 Not a thorough test, but it seems to work in all possible cases. A more Pythonic version of this function leaves out the elif. If the original condition is False, then the number must be less than 0, so there’s no need for a second test.

02:19 If the function gets to this part of the program, the number must’ve been less than 0, and so we know to return its opposite. Let’s modify our version of my_abs() and see that it still works.

02:34 We don’t need this second test.

02:41 Again, if number is greater than or equal to 0, meaning it’s zero or positive, we go inside the if block, we do this return statement, and the function ends.

02:52 If this condition is False, the number must have been less than 0, we skip the block, come here to what follows the if statement and its block, and we just get this return statement to return the opposite of that number.

03:08 Let’s make sure this works as well. We will save it, and we will restart our interpreter.

03:24 Try it out on 15,

03:28 try it out on -15, and try it out on 0. Still works! It’s important to note something. If a branch doesn’t have a return statement, then the default None will be used as the return value.

03:48 Here’s an example of a slightly incorrect implementation of the absolute value function. Notice the inequality in the if statement is different.

03:57 It’s greater than (>), not greater than or equal to (>=). This is a common programming error: not treating the end points of a range of values properly. In this case, we’re not dealing with the 0 case properly.

04:11 Notice I am missing the equal sign (=). I’m just checking to see if the number is positive, I’m not checking to see if it’s 0 in my first if statement.

04:23 And then if I try to use it,

04:31 it works for 15,

04:36 it works for -15, but now if I try 0, we get something weird. It didn’t display anything at all. Well, there was no branch in the function to account for 0, and so we ended up getting Python’s default return, None, which we can see if I actually try to print the result of calling my_abs() on 0.

05:07 What happened is there is a third empty branch, which would be right here, in the case that we get to this elif and this condition is False. Since that’s the end of the method, Python defaults by putting the return None there and we get the None value returned for us.

05:27 It’s important to make sure every branch in your function has an appropriate return statement, otherwise you’ll get results like this. Next, we’ll look at another topic related to conditionals, and that’s when you want a function to return either True or False.

Become a Member to join the conversation.