Why we need to check if value equals None?
values = [-50, -80, -100]
max_value = None
for i in values:
if max_value is None or i > max_value:
max_value = i
why we need to include this code max_value is None
Let's say we go through the for loop
Loop 1: i = -50. We can't evaluate -50 with None, right?
python
add a comment |
values = [-50, -80, -100]
max_value = None
for i in values:
if max_value is None or i > max_value:
max_value = i
why we need to include this code max_value is None
Let's say we go through the for loop
Loop 1: i = -50. We can't evaluate -50 with None, right?
python
1
max_value
is set toNone
and evaluated the first time before it is changed.
– Michael Butscher
Nov 25 '18 at 1:06
Exactly. Need it there to avoid an error.
– Anton vBR
Nov 25 '18 at 1:08
So that's only a trick to avoid an error?
– The One
Nov 25 '18 at 1:11
2
@TheOne, You could call it "a trick", but that's not a technical term. It's a way to make your program work, givenmax_value
is initialised asNone
.
– jpp
Nov 25 '18 at 1:21
add a comment |
values = [-50, -80, -100]
max_value = None
for i in values:
if max_value is None or i > max_value:
max_value = i
why we need to include this code max_value is None
Let's say we go through the for loop
Loop 1: i = -50. We can't evaluate -50 with None, right?
python
values = [-50, -80, -100]
max_value = None
for i in values:
if max_value is None or i > max_value:
max_value = i
why we need to include this code max_value is None
Let's say we go through the for loop
Loop 1: i = -50. We can't evaluate -50 with None, right?
python
python
asked Nov 25 '18 at 1:04
The OneThe One
4331520
4331520
1
max_value
is set toNone
and evaluated the first time before it is changed.
– Michael Butscher
Nov 25 '18 at 1:06
Exactly. Need it there to avoid an error.
– Anton vBR
Nov 25 '18 at 1:08
So that's only a trick to avoid an error?
– The One
Nov 25 '18 at 1:11
2
@TheOne, You could call it "a trick", but that's not a technical term. It's a way to make your program work, givenmax_value
is initialised asNone
.
– jpp
Nov 25 '18 at 1:21
add a comment |
1
max_value
is set toNone
and evaluated the first time before it is changed.
– Michael Butscher
Nov 25 '18 at 1:06
Exactly. Need it there to avoid an error.
– Anton vBR
Nov 25 '18 at 1:08
So that's only a trick to avoid an error?
– The One
Nov 25 '18 at 1:11
2
@TheOne, You could call it "a trick", but that's not a technical term. It's a way to make your program work, givenmax_value
is initialised asNone
.
– jpp
Nov 25 '18 at 1:21
1
1
max_value
is set to None
and evaluated the first time before it is changed.– Michael Butscher
Nov 25 '18 at 1:06
max_value
is set to None
and evaluated the first time before it is changed.– Michael Butscher
Nov 25 '18 at 1:06
Exactly. Need it there to avoid an error.
– Anton vBR
Nov 25 '18 at 1:08
Exactly. Need it there to avoid an error.
– Anton vBR
Nov 25 '18 at 1:08
So that's only a trick to avoid an error?
– The One
Nov 25 '18 at 1:11
So that's only a trick to avoid an error?
– The One
Nov 25 '18 at 1:11
2
2
@TheOne, You could call it "a trick", but that's not a technical term. It's a way to make your program work, given
max_value
is initialised as None
.– jpp
Nov 25 '18 at 1:21
@TheOne, You could call it "a trick", but that's not a technical term. It's a way to make your program work, given
max_value
is initialised as None
.– jpp
Nov 25 '18 at 1:21
add a comment |
2 Answers
2
active
oldest
votes
Because you can't compare int
with NoneType
, this gives TypeError
:
-50 > None # TypeError: '>' not supported between instances of 'int' and 'NoneType'
So removing the condition will lead your code to break. But, much better, set a minimal starting value, e.g. -inf
:
values = [-50, -80, -100]
max_value = -float('inf')
for i in values:
if i > max_value:
max_value = i
Of course, for your trivial computation, you can use the built-in max
:
max(values) # -50
I just want to understand about this None. As you said, we can't compare any value to None because it's simpy None so max_value is None here sounds like a trick to me. it doesn't have any meaning as in "i > 3 or i < 10", right?
– The One
Nov 25 '18 at 1:17
@TheOne, Simply put, Python can't evaluate-50 > None
, so it gives an error. That makes sense,None
isn't a numeric type. So that's why your algorithm checks explicitly first that your value isn'tNone
.
– jpp
Nov 25 '18 at 1:18
If so, why "if max_value is not None and i > max_value" doesn't give a correct answer?
– The One
Nov 25 '18 at 1:25
or
is lazy. In the first iteration, the first condition (max_value is not None
) is not satisfied, so it moves on to the next. It then tries-50 > None
and fails.
– jpp
Nov 25 '18 at 1:26
1
You will encounterNone
very often while programming in Python.
– Matthias
Nov 25 '18 at 8:26
|
show 1 more comment
The most idiomatic approach is to assume values[0]
as the maximum and start iterating from the first index:
values = [-50, -80, -100]
max_value = values[0]
for i in values[1:]:
if i > max_value:
max_value = i
And you can get rid of the None
check.
To be honest, i always set the first element in the list as a maximum value. But this None thing confused me.
– The One
Nov 25 '18 at 1:13
@TheOne ifmax_value
is None, theni > max_value
is not defined. Hence the check. But setting to the first element is the accepted method of initialisation.
– coldspeed
Nov 25 '18 at 1:14
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%2f53463811%2fwhy-we-need-to-check-if-value-equals-none%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Because you can't compare int
with NoneType
, this gives TypeError
:
-50 > None # TypeError: '>' not supported between instances of 'int' and 'NoneType'
So removing the condition will lead your code to break. But, much better, set a minimal starting value, e.g. -inf
:
values = [-50, -80, -100]
max_value = -float('inf')
for i in values:
if i > max_value:
max_value = i
Of course, for your trivial computation, you can use the built-in max
:
max(values) # -50
I just want to understand about this None. As you said, we can't compare any value to None because it's simpy None so max_value is None here sounds like a trick to me. it doesn't have any meaning as in "i > 3 or i < 10", right?
– The One
Nov 25 '18 at 1:17
@TheOne, Simply put, Python can't evaluate-50 > None
, so it gives an error. That makes sense,None
isn't a numeric type. So that's why your algorithm checks explicitly first that your value isn'tNone
.
– jpp
Nov 25 '18 at 1:18
If so, why "if max_value is not None and i > max_value" doesn't give a correct answer?
– The One
Nov 25 '18 at 1:25
or
is lazy. In the first iteration, the first condition (max_value is not None
) is not satisfied, so it moves on to the next. It then tries-50 > None
and fails.
– jpp
Nov 25 '18 at 1:26
1
You will encounterNone
very often while programming in Python.
– Matthias
Nov 25 '18 at 8:26
|
show 1 more comment
Because you can't compare int
with NoneType
, this gives TypeError
:
-50 > None # TypeError: '>' not supported between instances of 'int' and 'NoneType'
So removing the condition will lead your code to break. But, much better, set a minimal starting value, e.g. -inf
:
values = [-50, -80, -100]
max_value = -float('inf')
for i in values:
if i > max_value:
max_value = i
Of course, for your trivial computation, you can use the built-in max
:
max(values) # -50
I just want to understand about this None. As you said, we can't compare any value to None because it's simpy None so max_value is None here sounds like a trick to me. it doesn't have any meaning as in "i > 3 or i < 10", right?
– The One
Nov 25 '18 at 1:17
@TheOne, Simply put, Python can't evaluate-50 > None
, so it gives an error. That makes sense,None
isn't a numeric type. So that's why your algorithm checks explicitly first that your value isn'tNone
.
– jpp
Nov 25 '18 at 1:18
If so, why "if max_value is not None and i > max_value" doesn't give a correct answer?
– The One
Nov 25 '18 at 1:25
or
is lazy. In the first iteration, the first condition (max_value is not None
) is not satisfied, so it moves on to the next. It then tries-50 > None
and fails.
– jpp
Nov 25 '18 at 1:26
1
You will encounterNone
very often while programming in Python.
– Matthias
Nov 25 '18 at 8:26
|
show 1 more comment
Because you can't compare int
with NoneType
, this gives TypeError
:
-50 > None # TypeError: '>' not supported between instances of 'int' and 'NoneType'
So removing the condition will lead your code to break. But, much better, set a minimal starting value, e.g. -inf
:
values = [-50, -80, -100]
max_value = -float('inf')
for i in values:
if i > max_value:
max_value = i
Of course, for your trivial computation, you can use the built-in max
:
max(values) # -50
Because you can't compare int
with NoneType
, this gives TypeError
:
-50 > None # TypeError: '>' not supported between instances of 'int' and 'NoneType'
So removing the condition will lead your code to break. But, much better, set a minimal starting value, e.g. -inf
:
values = [-50, -80, -100]
max_value = -float('inf')
for i in values:
if i > max_value:
max_value = i
Of course, for your trivial computation, you can use the built-in max
:
max(values) # -50
answered Nov 25 '18 at 1:07
jppjpp
101k2164114
101k2164114
I just want to understand about this None. As you said, we can't compare any value to None because it's simpy None so max_value is None here sounds like a trick to me. it doesn't have any meaning as in "i > 3 or i < 10", right?
– The One
Nov 25 '18 at 1:17
@TheOne, Simply put, Python can't evaluate-50 > None
, so it gives an error. That makes sense,None
isn't a numeric type. So that's why your algorithm checks explicitly first that your value isn'tNone
.
– jpp
Nov 25 '18 at 1:18
If so, why "if max_value is not None and i > max_value" doesn't give a correct answer?
– The One
Nov 25 '18 at 1:25
or
is lazy. In the first iteration, the first condition (max_value is not None
) is not satisfied, so it moves on to the next. It then tries-50 > None
and fails.
– jpp
Nov 25 '18 at 1:26
1
You will encounterNone
very often while programming in Python.
– Matthias
Nov 25 '18 at 8:26
|
show 1 more comment
I just want to understand about this None. As you said, we can't compare any value to None because it's simpy None so max_value is None here sounds like a trick to me. it doesn't have any meaning as in "i > 3 or i < 10", right?
– The One
Nov 25 '18 at 1:17
@TheOne, Simply put, Python can't evaluate-50 > None
, so it gives an error. That makes sense,None
isn't a numeric type. So that's why your algorithm checks explicitly first that your value isn'tNone
.
– jpp
Nov 25 '18 at 1:18
If so, why "if max_value is not None and i > max_value" doesn't give a correct answer?
– The One
Nov 25 '18 at 1:25
or
is lazy. In the first iteration, the first condition (max_value is not None
) is not satisfied, so it moves on to the next. It then tries-50 > None
and fails.
– jpp
Nov 25 '18 at 1:26
1
You will encounterNone
very often while programming in Python.
– Matthias
Nov 25 '18 at 8:26
I just want to understand about this None. As you said, we can't compare any value to None because it's simpy None so max_value is None here sounds like a trick to me. it doesn't have any meaning as in "i > 3 or i < 10", right?
– The One
Nov 25 '18 at 1:17
I just want to understand about this None. As you said, we can't compare any value to None because it's simpy None so max_value is None here sounds like a trick to me. it doesn't have any meaning as in "i > 3 or i < 10", right?
– The One
Nov 25 '18 at 1:17
@TheOne, Simply put, Python can't evaluate
-50 > None
, so it gives an error. That makes sense, None
isn't a numeric type. So that's why your algorithm checks explicitly first that your value isn't None
.– jpp
Nov 25 '18 at 1:18
@TheOne, Simply put, Python can't evaluate
-50 > None
, so it gives an error. That makes sense, None
isn't a numeric type. So that's why your algorithm checks explicitly first that your value isn't None
.– jpp
Nov 25 '18 at 1:18
If so, why "if max_value is not None and i > max_value" doesn't give a correct answer?
– The One
Nov 25 '18 at 1:25
If so, why "if max_value is not None and i > max_value" doesn't give a correct answer?
– The One
Nov 25 '18 at 1:25
or
is lazy. In the first iteration, the first condition (max_value is not None
) is not satisfied, so it moves on to the next. It then tries -50 > None
and fails.– jpp
Nov 25 '18 at 1:26
or
is lazy. In the first iteration, the first condition (max_value is not None
) is not satisfied, so it moves on to the next. It then tries -50 > None
and fails.– jpp
Nov 25 '18 at 1:26
1
1
You will encounter
None
very often while programming in Python.– Matthias
Nov 25 '18 at 8:26
You will encounter
None
very often while programming in Python.– Matthias
Nov 25 '18 at 8:26
|
show 1 more comment
The most idiomatic approach is to assume values[0]
as the maximum and start iterating from the first index:
values = [-50, -80, -100]
max_value = values[0]
for i in values[1:]:
if i > max_value:
max_value = i
And you can get rid of the None
check.
To be honest, i always set the first element in the list as a maximum value. But this None thing confused me.
– The One
Nov 25 '18 at 1:13
@TheOne ifmax_value
is None, theni > max_value
is not defined. Hence the check. But setting to the first element is the accepted method of initialisation.
– coldspeed
Nov 25 '18 at 1:14
add a comment |
The most idiomatic approach is to assume values[0]
as the maximum and start iterating from the first index:
values = [-50, -80, -100]
max_value = values[0]
for i in values[1:]:
if i > max_value:
max_value = i
And you can get rid of the None
check.
To be honest, i always set the first element in the list as a maximum value. But this None thing confused me.
– The One
Nov 25 '18 at 1:13
@TheOne ifmax_value
is None, theni > max_value
is not defined. Hence the check. But setting to the first element is the accepted method of initialisation.
– coldspeed
Nov 25 '18 at 1:14
add a comment |
The most idiomatic approach is to assume values[0]
as the maximum and start iterating from the first index:
values = [-50, -80, -100]
max_value = values[0]
for i in values[1:]:
if i > max_value:
max_value = i
And you can get rid of the None
check.
The most idiomatic approach is to assume values[0]
as the maximum and start iterating from the first index:
values = [-50, -80, -100]
max_value = values[0]
for i in values[1:]:
if i > max_value:
max_value = i
And you can get rid of the None
check.
answered Nov 25 '18 at 1:11
coldspeedcoldspeed
135k23145230
135k23145230
To be honest, i always set the first element in the list as a maximum value. But this None thing confused me.
– The One
Nov 25 '18 at 1:13
@TheOne ifmax_value
is None, theni > max_value
is not defined. Hence the check. But setting to the first element is the accepted method of initialisation.
– coldspeed
Nov 25 '18 at 1:14
add a comment |
To be honest, i always set the first element in the list as a maximum value. But this None thing confused me.
– The One
Nov 25 '18 at 1:13
@TheOne ifmax_value
is None, theni > max_value
is not defined. Hence the check. But setting to the first element is the accepted method of initialisation.
– coldspeed
Nov 25 '18 at 1:14
To be honest, i always set the first element in the list as a maximum value. But this None thing confused me.
– The One
Nov 25 '18 at 1:13
To be honest, i always set the first element in the list as a maximum value. But this None thing confused me.
– The One
Nov 25 '18 at 1:13
@TheOne if
max_value
is None, then i > max_value
is not defined. Hence the check. But setting to the first element is the accepted method of initialisation.– coldspeed
Nov 25 '18 at 1:14
@TheOne if
max_value
is None, then i > max_value
is not defined. Hence the check. But setting to the first element is the accepted method of initialisation.– coldspeed
Nov 25 '18 at 1:14
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%2f53463811%2fwhy-we-need-to-check-if-value-equals-none%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
1
max_value
is set toNone
and evaluated the first time before it is changed.– Michael Butscher
Nov 25 '18 at 1:06
Exactly. Need it there to avoid an error.
– Anton vBR
Nov 25 '18 at 1:08
So that's only a trick to avoid an error?
– The One
Nov 25 '18 at 1:11
2
@TheOne, You could call it "a trick", but that's not a technical term. It's a way to make your program work, given
max_value
is initialised asNone
.– jpp
Nov 25 '18 at 1:21