Why if integer is always True [duplicate]












-1
















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










share|improve this question













marked as duplicate by coldspeed python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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 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 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


















-1
















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










share|improve this question













marked as duplicate by coldspeed python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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 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 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
















-1












-1








-1









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










share|improve this question















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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 29 '18 at 11:12









user456user456

123




123




marked as duplicate by coldspeed python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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 python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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 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 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
















  • 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 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










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














1 Answer
1






active

oldest

votes


















-1














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






share|improve this answer
























  • "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




















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









-1














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






share|improve this answer
























  • "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


















-1














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






share|improve this answer
























  • "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
















-1












-1








-1







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






share|improve this answer













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







share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 29 '18 at 11:15









Flightdoc5242Flightdoc5242

1127




1127













  • "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



















"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





Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas