Movatterモバイル変換


[0]ホーム

URL:


Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

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 outHow to Find an Absolute Value in Python.

00:00In this lesson,we’ll look at best practices usingreturn statements with conditionalstatements.A Python function can have more than onereturn statement.

00:10The 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:24As an example, let’s consider the absolute value function for mathematics.The absolute value of a real numberx is the number itselfif that number is zero or positive, and the absolute value is the opposite ofthat number—you change its sign—if the number is negative. The negative sign in front of thex meansjust to change its sign. So ifx is negative,the negative of that is going to be a positive number.

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

01:06It 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 to0,meaning if it’s zero or positive, we return that number.

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

01:42We can call this function on a positive numberand 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 on0 and get0 back.

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

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

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

02:41Again, ifnumber is greater than or equal to0, meaning it’s zero or positive,we go inside theif block,we do thisreturn statement, and the function ends.

02:52If this condition isFalse, the number must have been less than0,we skip the block,come here to what follows theif statement and its block,and we just get thisreturn statement to return the opposite of that number.

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

03:24Try it out on15,

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

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

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

04:11Notice 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’s0 in my firstif statement.

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

04:31it works for15,

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

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

05:27It’s important to make sure every branch in your functionhas an appropriatereturn 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 eitherTrue orFalse.

Become a Member to join the conversation.

Course Contents

Overview
48%

[8]ページ先頭

©2009-2026 Movatter.jp