jython - why '==' return true but 'is' return false - jython-2.7

I am start use jython2.7.0 now, but there has little different between python2.7.15.
for example, In python
a = "hello", b = "hello", "a is b" and "a == b" will return True.
but In jython2.7.0 "a is b" return False, "a == b" return True, does anyone knows why ??
>>>jython
>>>
>>>a = "hello"
>>>b = "hello"
>>>
>>>a is b
>>>False
>>>
>>>a == b
>>>True
>>>
>>>a = 1
>>>b = 1
>>> a is b
>>>True

Related

How to write a nested if condition inside a function to return 3 different values to 3 dataframe columns

I am trying to create 3 new columns to my existing dataframe using the logic written on top of other columns in the same dataframe. I have a function which returns 3 values based on specific logic written in it. The function is as below.
def fun(a, b, c, d):
if "Category" in a:
if (b == "some text" or b == "some text 2") and c.str.contains("Status1"):
x = 1
elif (b == "some text 3" or b == "some text 4") and c.str.contains("Status2"):
y = 1
elif b == "some text 5" and c.str.contains("Status3"):
z = 1
else:
if (b == "some text" or b == "some text 2") and c.str.contains("Status1"):
x = 1
x2 = 1
elif (b == "some text 3" or b == "some text 4") and x2 == 0 and c.str.contains("Status2"):
y = 1
elif b == "some text 5" and c.str.contains("Status3"):
z = 1
return x, y, z
Now I want to assign x, y, z to 3 new columns in my dataframe i.e. df['x'], df['y'], df['z'].
I tried to use zip as shown below but it gives an error.
df["x"], df["y"], df["z"] = zip(*df.apply(fun("Col1", "Col2", "Col3", "Col4"), axis=1))
TypeError: ("'tuple' object is not callable", 'occurred at index 0')
Can someone suggest what I am doing wrong here?
The end state for me is to assign x, y and z to 3 new columns in df by performing logical operations on columns in df.

Why does a Python "x in xyz" comparison not yield equality when it returns True by itself? [duplicate]

This question already has answers here:
Why does the expression 0 < 0 == 0 return False in Python?
(9 answers)
Closed 7 days ago.
The following comparisons produce True:
>>> '1' in '11'
True
>>> ('1' in '11') == True
True
And with the parentheses the other way, I get a TypeError:
>>> '1' in ('11' == True)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: argument of type 'bool' is not iterable
So how do I get False with no parentheses?
>>> '1' in '11' == True
False
The Python manual says in and == are of equal precedence. Thus, they're evaluated from left to right by default, but there's also chaining to consider. The expression you put above ('1' in '11' == True) is actually being evaluated as...
('1' in '11') and ('11' == True)
which of course is False. If you don't know what chaining is, it's what allows you to do something like...
if 0 < a < 1:
in Python, and have that mean what you expect ("a is greater than 0 but less than 1").
It has nothing to do with precedence. In Python relational operators chain, and containment is considered a relational operator. Therefore:
'1' in '11' == True
is the same as:
('1' in '11') and ('11' == True)
which is false since True is not equal to "11".
Chaining allows you to write x < y < z, and mean x < y and y < z. Look at this interaction:
>>> (False == True) == False
True
>>> False == (True == False)
True
>>> False == True == False
False
>>>
So in your example, '1' in '11' == True is equivalent to ('1' in '11') and ('11' == True)

if-else statement to increment or decrement inside for loop in python

I will take an input n and then print n number of lines.Every line i will input a string if this string is "++X" or "X++" then i will add 1 with 0(The initial value of X is 0). If my string is "--X" or "X--" then i will subtract 1 from 0.(If i already take another string then subtract from this string value).
Here is My code:
n = int(input())
c = 0
for x in range(n):
a = input()
if a == "X++" or "++X":
c = c + 1
elif a == "--X" or "X--":
c = c - 1
print(c)
My input:
2
X++
--X
My output:
2
Expected output:
0
Because,value will 1 for "X++" and 0 again for "--X". So,what is wrong in my code and how can i fix this?
Reference: https://docs.python.org/3/reference/expressions.html#operator-precedence
The order of precedence in the logic expression makes your expression equivalent to if (a == "X++") or "++X": which will always be True because bool("++X") == True. Any non-empty string will be True. So, as other answers suggest, you can use if a == "X++" or a == "++X": since or is evaluated last in the expression, the interp will evaluate the equality operations first, then apply or.
Another more shorthand way to test if a variable has a value that could be a few alternatives is to use the in operator.
n = int(input())
c = 0
for x in range(n):
a = input()
if a in ("X++", "++X"):
c = c + 1
elif a in ("--X", "X--"):
c = c - 1
print(c)
So you had a case where a non-empty string evaluated to True, there are other interesting cases that evaluate to either True or False in expressions. For instance, lists:
li1 = []
li2 = [1, 2]
if li1:
print("li1")
if li2:
print("li2")
Will output li2.
Bonus round - associating values with string input using a dict:
>>> def foo(n):
... d = {'X++': 1, '++X': 1, '--X': -1, 'X--': -1}
... c = 0
... for _ in range(n):
... a = input()
... c += d.get(a, 0)
... return c
...
>>> foo(3)
<-- X++
<-- ++X
<-- ++X
3
n = int(input())
c = 0
for x in range(n):
a = input()
if a == "X++" or a == "++X":
c = c + 1
elif a == "--X" or a == "X--":
c = c - 1
print(c)
:)
Change your condition from
if a == "X++" or "++X":
to
if a == "X++" or a == "++X":
or
if a in ("X++", "++X"):
You forgot to compare with a in every second operand of the ors. A non-empty string will evaluate to True.
n = int(input())
c = 0
for x in range(n):
a = input()
if a == "X++" or a == "++X":
c = c + 1
elif a == "--X" or a == "X--":
c = c - 1
print(c)

Python interpreter evaluates expression 4 < 5 to True. Why then does 4 < 5 == True return False?

Was messing around with values and concept in interpreter and hit a logic stumbling block as 4 < 5 is True but this output is not considered equal to == True?
In Python3: 4 < 5 == True is equivalent to 4 < 5 and 5 == True, which evaluates to False because 5 != True.
Note that < and == have the same precedence.
Doc for reference https://docs.python.org/3/reference/expressions.html#comparisons
From Python documentation
Formally, if a, b, c, …, y, z are expressions and op1, op2, …, opN are
comparison operators, then a op1 b op2 c ... y opN z is equivalent to
a op1 b and b op2 c and ... y opN z, except that each expression is
evaluated at most once.
From this specification is 4 < 5 == True equal to 4 < 5 and 5 == True (and from operator precedence in Python it equals to (4 < 5) and (5 == True)) where 4 < 5 is True, but 5 == True is False. So True and False is False.
>>> 4<5
True
>>> 4<5 == True
False
>>> (4<5) == True
True
>>>
I hope this clears your doubt. 4<5 == True is evaluating as 4<5 and 5 == True which overall returns False as 4<5 is True, but, 5 == True is False. This is due to < and == having the same level of precedence.

Why a Groovy List (of maps) arithmetic doesn't work as expected

I was trying to remove from a list of maps the elements that are present in another list. But found out that it doesn't work as i expected.
I can't understand the reason why.
def p1 = [1,2,3,4,5]
def p2 = [1, 3, 5]
assert p1[0] == p2[0]
assert p1[2] == p2[1]
assert p1[4] == p2[2]
assert [2,4] == p1-p2 // all ok
def q1 = [[1],[2],[3],[4],[5]]
def q2 = [[1], [3], [5]]
assert q1[0] == q2[0]
assert q1[2] == q2[1]
assert q1[4] == q2[2]
assert [[2], [4]] == q1-q2 // all ok
def t1 = [[1,2,3], [4,5,6], [7,8,9], [10,11,12], [13,14,15]]
def t2 = [[1,2,3], [7,8,9], [13,14,15]]
assert t1[0] == t2[0]
assert t1[2] == t2[1]
assert t1[4] == t2[2]
assert [[4,5,6], [10,11,12]] == t1-t2 // all ok
def r1 = [[a:1, b:1], [a:2,b:2], [a:3,b:3],[a:4,b:4], [a:5,b:5]]
def r2 = [[a:1, b:1], [a:3,b:3], [a:5,b:5]]
assert r1[0] == r2[0]
assert r1[2] == r2[1]
assert r1[4] == r2[2]
assert [[a:2,b:2], [a:4,b:4]] == r1-r2 // this is what I expected but fails
assert [] == r1-r2 // this is totally unexpected but it's ok
I can't figure out why. Can someone shed some light on this.
Thanks

Resources