Why if integer is always True [duplicate]
This question already has an answer here:
What is Truthy and Falsy in python? How is it different from True and False?
3 answers
I am a bit confused with if/else statement. Why the code always prints True while it should be False.
I have tried with different variables like i =10
, i = 'a'
, i = 25
. And it will be False if i=
This is my code:
i =1
if i:
print True
else:
print False
python
marked as duplicate by coldspeed
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 29 '18 at 11:15
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
What is Truthy and Falsy in python? How is it different from True and False?
3 answers
I am a bit confused with if/else statement. Why the code always prints True while it should be False.
I have tried with different variables like i =10
, i = 'a'
, i = 25
. And it will be False if i=
This is my code:
i =1
if i:
print True
else:
print False
python
marked as duplicate by coldspeed
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 29 '18 at 11:15
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
its false for 0. isn't it nice to be able to check if a number is non zero, or a string non empty, and so on? :)
– Paritosh Singh
Dec 29 '18 at 11:13
Why do you think it should beFalse
on any of these examples?
– glglgl
Dec 29 '18 at 11:13
Because as far as I knew, If i: is equal to if i==1.Isn't it?
– user456
Dec 29 '18 at 11:15
1
No, it isnt. Here's some docs. So (tl;dr its more close tobool(i)
which translates toi !=0
for ints. Different languages have different conventions, and you have to be careful assuming things.
– Paritosh Singh
Dec 29 '18 at 11:18
add a comment |
This question already has an answer here:
What is Truthy and Falsy in python? How is it different from True and False?
3 answers
I am a bit confused with if/else statement. Why the code always prints True while it should be False.
I have tried with different variables like i =10
, i = 'a'
, i = 25
. And it will be False if i=
This is my code:
i =1
if i:
print True
else:
print False
python
This question already has an answer here:
What is Truthy and Falsy in python? How is it different from True and False?
3 answers
I am a bit confused with if/else statement. Why the code always prints True while it should be False.
I have tried with different variables like i =10
, i = 'a'
, i = 25
. And it will be False if i=
This is my code:
i =1
if i:
print True
else:
print False
This question already has an answer here:
What is Truthy and Falsy in python? How is it different from True and False?
3 answers
python
python
asked Dec 29 '18 at 11:12
user456user456
123
123
marked as duplicate by coldspeed
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 29 '18 at 11:15
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by coldspeed
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 29 '18 at 11:15
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
its false for 0. isn't it nice to be able to check if a number is non zero, or a string non empty, and so on? :)
– Paritosh Singh
Dec 29 '18 at 11:13
Why do you think it should beFalse
on any of these examples?
– glglgl
Dec 29 '18 at 11:13
Because as far as I knew, If i: is equal to if i==1.Isn't it?
– user456
Dec 29 '18 at 11:15
1
No, it isnt. Here's some docs. So (tl;dr its more close tobool(i)
which translates toi !=0
for ints. Different languages have different conventions, and you have to be careful assuming things.
– Paritosh Singh
Dec 29 '18 at 11:18
add a comment |
2
its false for 0. isn't it nice to be able to check if a number is non zero, or a string non empty, and so on? :)
– Paritosh Singh
Dec 29 '18 at 11:13
Why do you think it should beFalse
on any of these examples?
– glglgl
Dec 29 '18 at 11:13
Because as far as I knew, If i: is equal to if i==1.Isn't it?
– user456
Dec 29 '18 at 11:15
1
No, it isnt. Here's some docs. So (tl;dr its more close tobool(i)
which translates toi !=0
for ints. Different languages have different conventions, and you have to be careful assuming things.
– Paritosh Singh
Dec 29 '18 at 11:18
2
2
its false for 0. isn't it nice to be able to check if a number is non zero, or a string non empty, and so on? :)
– Paritosh Singh
Dec 29 '18 at 11:13
its false for 0. isn't it nice to be able to check if a number is non zero, or a string non empty, and so on? :)
– Paritosh Singh
Dec 29 '18 at 11:13
Why do you think it should be
False
on any of these examples?– glglgl
Dec 29 '18 at 11:13
Why do you think it should be
False
on any of these examples?– glglgl
Dec 29 '18 at 11:13
Because as far as I knew, If i: is equal to if i==1.Isn't it?
– user456
Dec 29 '18 at 11:15
Because as far as I knew, If i: is equal to if i==1.Isn't it?
– user456
Dec 29 '18 at 11:15
1
1
No, it isnt. Here's some docs. So (tl;dr its more close to
bool(i)
which translates to i !=0
for ints. Different languages have different conventions, and you have to be careful assuming things.– Paritosh Singh
Dec 29 '18 at 11:18
No, it isnt. Here's some docs. So (tl;dr its more close to
bool(i)
which translates to i !=0
for ints. Different languages have different conventions, and you have to be careful assuming things.– Paritosh Singh
Dec 29 '18 at 11:18
add a comment |
1 Answer
1
active
oldest
votes
In your code you say if I: True
. But your not comparing it to anything. You need a comparison operator. Like if i == 1
otherwise the if statement will just be true IF I has a value by default
"Not comparing it to anything" is incorrect. In a lineif x:
, x itself is evaluated as a Truthy/Falsy value, and so is equivalent toif bool(x) == True
.
– usr2564301
Dec 29 '18 at 22:10
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
In your code you say if I: True
. But your not comparing it to anything. You need a comparison operator. Like if i == 1
otherwise the if statement will just be true IF I has a value by default
"Not comparing it to anything" is incorrect. In a lineif x:
, x itself is evaluated as a Truthy/Falsy value, and so is equivalent toif bool(x) == True
.
– usr2564301
Dec 29 '18 at 22:10
add a comment |
In your code you say if I: True
. But your not comparing it to anything. You need a comparison operator. Like if i == 1
otherwise the if statement will just be true IF I has a value by default
"Not comparing it to anything" is incorrect. In a lineif x:
, x itself is evaluated as a Truthy/Falsy value, and so is equivalent toif bool(x) == True
.
– usr2564301
Dec 29 '18 at 22:10
add a comment |
In your code you say if I: True
. But your not comparing it to anything. You need a comparison operator. Like if i == 1
otherwise the if statement will just be true IF I has a value by default
In your code you say if I: True
. But your not comparing it to anything. You need a comparison operator. Like if i == 1
otherwise the if statement will just be true IF I has a value by default
answered Dec 29 '18 at 11:15
Flightdoc5242Flightdoc5242
1127
1127
"Not comparing it to anything" is incorrect. In a lineif x:
, x itself is evaluated as a Truthy/Falsy value, and so is equivalent toif bool(x) == True
.
– usr2564301
Dec 29 '18 at 22:10
add a comment |
"Not comparing it to anything" is incorrect. In a lineif x:
, x itself is evaluated as a Truthy/Falsy value, and so is equivalent toif bool(x) == True
.
– usr2564301
Dec 29 '18 at 22:10
"Not comparing it to anything" is incorrect. In a line
if x:
, x itself is evaluated as a Truthy/Falsy value, and so is equivalent to if bool(x) == True
.– usr2564301
Dec 29 '18 at 22:10
"Not comparing it to anything" is incorrect. In a line
if x:
, x itself is evaluated as a Truthy/Falsy value, and so is equivalent to if bool(x) == True
.– usr2564301
Dec 29 '18 at 22:10
add a comment |
2
its false for 0. isn't it nice to be able to check if a number is non zero, or a string non empty, and so on? :)
– Paritosh Singh
Dec 29 '18 at 11:13
Why do you think it should be
False
on any of these examples?– glglgl
Dec 29 '18 at 11:13
Because as far as I knew, If i: is equal to if i==1.Isn't it?
– user456
Dec 29 '18 at 11:15
1
No, it isnt. Here's some docs. So (tl;dr its more close to
bool(i)
which translates toi !=0
for ints. Different languages have different conventions, and you have to be careful assuming things.– Paritosh Singh
Dec 29 '18 at 11:18