Python: Why else statement can be discarded in this simple expression?












2















I apologize for how obvious this answer must be, but I just can't seem to find out why an else statement isn't needed in the following function which returns True -



def boolean():
x = 1
if x == 1:
return True
return False

boolean()


My beginner coding mind is confused why False isn't being returned. The if statement returns True, then outside of that if statement, False is returned. I would've thought to write -



def boolean():
x = 1
if x == 1:
return True
else:
return False

boolean()


Why isn't the else statement needed here? Thank you very much for enlightening me on this.










share|improve this question




















  • 2





    "The if statement returns True, then outside of that if statement, False is returned", how can two values be returned from a function? And why not just return x == 1?

    – Sean Pianka
    Nov 26 '18 at 18:44













  • For some reason, I wasn't sure that only one value can be returned from a function. And return x == 1 would be a better way of writing that, instead of an if statement which isn't needed. Thank you for pointing that out.

    – Sean
    Nov 26 '18 at 18:50
















2















I apologize for how obvious this answer must be, but I just can't seem to find out why an else statement isn't needed in the following function which returns True -



def boolean():
x = 1
if x == 1:
return True
return False

boolean()


My beginner coding mind is confused why False isn't being returned. The if statement returns True, then outside of that if statement, False is returned. I would've thought to write -



def boolean():
x = 1
if x == 1:
return True
else:
return False

boolean()


Why isn't the else statement needed here? Thank you very much for enlightening me on this.










share|improve this question




















  • 2





    "The if statement returns True, then outside of that if statement, False is returned", how can two values be returned from a function? And why not just return x == 1?

    – Sean Pianka
    Nov 26 '18 at 18:44













  • For some reason, I wasn't sure that only one value can be returned from a function. And return x == 1 would be a better way of writing that, instead of an if statement which isn't needed. Thank you for pointing that out.

    – Sean
    Nov 26 '18 at 18:50














2












2








2








I apologize for how obvious this answer must be, but I just can't seem to find out why an else statement isn't needed in the following function which returns True -



def boolean():
x = 1
if x == 1:
return True
return False

boolean()


My beginner coding mind is confused why False isn't being returned. The if statement returns True, then outside of that if statement, False is returned. I would've thought to write -



def boolean():
x = 1
if x == 1:
return True
else:
return False

boolean()


Why isn't the else statement needed here? Thank you very much for enlightening me on this.










share|improve this question
















I apologize for how obvious this answer must be, but I just can't seem to find out why an else statement isn't needed in the following function which returns True -



def boolean():
x = 1
if x == 1:
return True
return False

boolean()


My beginner coding mind is confused why False isn't being returned. The if statement returns True, then outside of that if statement, False is returned. I would've thought to write -



def boolean():
x = 1
if x == 1:
return True
else:
return False

boolean()


Why isn't the else statement needed here? Thank you very much for enlightening me on this.







python if-statement boolean






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 21:01









handras

491115




491115










asked Nov 26 '18 at 18:35









SeanSean

356




356








  • 2





    "The if statement returns True, then outside of that if statement, False is returned", how can two values be returned from a function? And why not just return x == 1?

    – Sean Pianka
    Nov 26 '18 at 18:44













  • For some reason, I wasn't sure that only one value can be returned from a function. And return x == 1 would be a better way of writing that, instead of an if statement which isn't needed. Thank you for pointing that out.

    – Sean
    Nov 26 '18 at 18:50














  • 2





    "The if statement returns True, then outside of that if statement, False is returned", how can two values be returned from a function? And why not just return x == 1?

    – Sean Pianka
    Nov 26 '18 at 18:44













  • For some reason, I wasn't sure that only one value can be returned from a function. And return x == 1 would be a better way of writing that, instead of an if statement which isn't needed. Thank you for pointing that out.

    – Sean
    Nov 26 '18 at 18:50








2




2





"The if statement returns True, then outside of that if statement, False is returned", how can two values be returned from a function? And why not just return x == 1?

– Sean Pianka
Nov 26 '18 at 18:44







"The if statement returns True, then outside of that if statement, False is returned", how can two values be returned from a function? And why not just return x == 1?

– Sean Pianka
Nov 26 '18 at 18:44















For some reason, I wasn't sure that only one value can be returned from a function. And return x == 1 would be a better way of writing that, instead of an if statement which isn't needed. Thank you for pointing that out.

– Sean
Nov 26 '18 at 18:50





For some reason, I wasn't sure that only one value can be returned from a function. And return x == 1 would be a better way of writing that, instead of an if statement which isn't needed. Thank you for pointing that out.

– Sean
Nov 26 '18 at 18:50












1 Answer
1






active

oldest

votes


















5














The execution of a function always ends as soon as a return statement is run. Nothing past that point is even evaluated. For example, if you added a print statement immediately after the return statement, you would not see it printed in the console.



Similarly, the execution of this function never reaches return False because True was already returned.






share|improve this answer
























  • Awe, now that makes a lot more sense. Thank you!

    – Sean
    Nov 26 '18 at 18:46











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53487112%2fpython-why-else-statement-can-be-discarded-in-this-simple-expression%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









5














The execution of a function always ends as soon as a return statement is run. Nothing past that point is even evaluated. For example, if you added a print statement immediately after the return statement, you would not see it printed in the console.



Similarly, the execution of this function never reaches return False because True was already returned.






share|improve this answer
























  • Awe, now that makes a lot more sense. Thank you!

    – Sean
    Nov 26 '18 at 18:46
















5














The execution of a function always ends as soon as a return statement is run. Nothing past that point is even evaluated. For example, if you added a print statement immediately after the return statement, you would not see it printed in the console.



Similarly, the execution of this function never reaches return False because True was already returned.






share|improve this answer
























  • Awe, now that makes a lot more sense. Thank you!

    – Sean
    Nov 26 '18 at 18:46














5












5








5







The execution of a function always ends as soon as a return statement is run. Nothing past that point is even evaluated. For example, if you added a print statement immediately after the return statement, you would not see it printed in the console.



Similarly, the execution of this function never reaches return False because True was already returned.






share|improve this answer













The execution of a function always ends as soon as a return statement is run. Nothing past that point is even evaluated. For example, if you added a print statement immediately after the return statement, you would not see it printed in the console.



Similarly, the execution of this function never reaches return False because True was already returned.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 18:43









Robo JeevesRobo Jeeves

763




763













  • Awe, now that makes a lot more sense. Thank you!

    – Sean
    Nov 26 '18 at 18:46



















  • Awe, now that makes a lot more sense. Thank you!

    – Sean
    Nov 26 '18 at 18:46

















Awe, now that makes a lot more sense. Thank you!

– Sean
Nov 26 '18 at 18:46





Awe, now that makes a lot more sense. Thank you!

– Sean
Nov 26 '18 at 18:46




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53487112%2fpython-why-else-statement-can-be-discarded-in-this-simple-expression%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks