This question already has answers here:
What's the canonical way to check for type in Python?
(15 answers)
Closed 5 years ago.
Why does this print False in Python 3?
>>> 1 == int
False
Because that isn't at all doing what you think it is. You are comparing the integer value 1 with the type int; naturally they are not equal.
If you want to check whether an object is of a certain type, use isinstance:
isinstance(1, int)
I guess, what you want to use is this:
>>> type(1) is int
True
or
>>> type(1) == int
True
Related
This question already has answers here:
Is False == 0 and True == 1 an implementation detail or is it guaranteed by the language?
(3 answers)
Closed 6 years ago.
>>> True + 2
3
>>> False + 2
2
I can understand that somehow, True means 1 and False means 0 . So
does it mean, a Boolean and integer operation always gives an integer?
In python bool is a subclass of int, and therefor satisfies the "is-a" relation, meaning a bool is-a int.
To demonstrate:
issubclass(bool, int)
=> True
isinstance(True, int)
=> True
In practice this means that in any operation which works on an int, the int can be substituted with a bool.
This question already has answers here:
Check if string is upper, lower, or mixed case in Python
(2 answers)
Closed 8 years ago.
Is there a property of characters that returns the case? Like maybe char.case()? I need this to get rid of some repetitive code that's bothering me.
The functions isupper() and islower () are what you need
>>> 'a'.isupper()
False
>>> 'A'.isupper()
True
>>> 'b'.islower()
True
>>> 'B'.islower()
False
You can use str.isupper() and str.islower()
>>> 'a'.isupper()
False
>>> 'a'.islower()
True
>>> 'A'.isupper()
True
>>> 'A'.islower()
False
This question already has answers here:
Understanding the "is" operator [duplicate]
(11 answers)
Closed 8 years ago.
I tried this operation in python interactive mode :
>>> (1*1) is 1
True
>>> (377*35) is 13195
False
>>> 377*35
13195
>>> 377*35 is 377*35
False
>>> 1*1 is 1
True
Could anybody explain why ' (377*35) is 13195 ' is false?
Thanks in advance!
A is B checks that A and B refer to the same object. It does not check whether A equals B numerically.
The reason for the different behaviour in your examples is that ints with small values (typically between -1 and 99
inclusive) are "interned" by the interpreter -- whenever a result has such a value,
an existing short int with the same value is returned.
This explains why is returns True for your examples involving small numbers but not for those involving large numbers.
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 8 years ago.
def distance_from_zero (x):
if type(x) == int or float :
return abs(x)
else:
return 'Nope'
The function is supposed to return the abs of x if the input is either an int or float.
If the input is none of those it should return Nope
I don't see what I am doing wrong here.
You have to use type(x) == int or type(x) == float.
This question already has answers here:
Adding the number 1 to a set has no effect
(5 answers)
Closed 9 years ago.
Condider the following example:
>>> {1, True}
set([True])
>>
>>> {True, 1}
set([1])
Why is the set represented differently, depending on the order of the elements?
This happens because 1 and True are equal to each other:
>>> True == 1
True
>>> 1 == True
True
The set retains one element from each equality class.
bool is subclass of int class
>>> issubclass(bool, int)
True
>>> True+1
2
>>> True == 1
True