Python: Why else statement can be discarded in this simple expression?
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
add a comment |
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
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 justreturn 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. Andreturn 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
add a comment |
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
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
python if-statement boolean
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 justreturn 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. Andreturn 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
add a comment |
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 justreturn 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. Andreturn 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
add a comment |
1 Answer
1
active
oldest
votes
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.
Awe, now that makes a lot more sense. Thank you!
– Sean
Nov 26 '18 at 18:46
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Awe, now that makes a lot more sense. Thank you!
– Sean
Nov 26 '18 at 18:46
add a comment |
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.
Awe, now that makes a lot more sense. Thank you!
– Sean
Nov 26 '18 at 18:46
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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