Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
hi all help about boolean
#1
hi all im new to programming and its logic and i ve read that if length of string longer than 256 and less than -5 the cache memory does nt save it and it turns false. like this:

a = 256
a is 256
True

a = 257
a is 257
False

but when i run this code on vs code it turns all true but when i run this code on idle it turns like this.
Reply
#2
Idle (even though packages with python) is not a good IDE.
use something like VSCode, PyCharm, Thonny or many of the others available
google 'python IDE' for an extensive list

when comparing literals, use '=='
>>> a = 257
>>> a is True
False
>>> a is 257
<stdin>:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
False
>>>a == 257
True
>>> a == 0
False
False
you can use if a to test if 0 or any value
>>> a = 0
>>> if a:
...     print(f'a has a value of {a}')
... else:
...     print(f'a is zero')
... 
a is zero
>>> a = 5
>>> if a:
...     print(f'a has a value of {a}')
... else:
...     print(f'a is zero')
... 
a has a value of 5
>>>
Reply
#3
i already use vs code and did run the code on both ( vscode and idle shell python ) but
on idle shell its ok works correct
but on vs code it doesnt result what it ought to be i guess

a =500
a is 500
should turn false but it turns true
Reply
#4
Why would this ever return false?
a =500
a is 500
"a" references the int object 500. This may not be the only 500 int object, but it is the same one created by "a = 500".
a = 500
print(id(a), id(500))
Output:
2003347601776 2003347601776
I think you were trying to test this:
a = 500
print("500 is not always 500", a is int(str(a)))
b = 255
print("255 is always 255", b is int(str(b)))
Output:
500 is not always 500 False 255 is always 255 True
CPython creates int objects for 0 through 256. These are used whenever an int object is needed in this range instead of creating new int objects. But this is an implementation detail and should not be used in your programs. Limit using "is" to testing if two variables reference the same object. Use "==" to test if two objects have the same value.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020