What's the name of this operator in Python? [duplicate] - python

This question already has answers here:
Python list - True/False first two elements? [duplicate]
(2 answers)
Closed 2 years ago.
In a Clash of Code, I've seen this interesting operator thing:
print(["false","true"][i == n])
I haven't seen this before. What's the name of this and what does it do?

It's not exactly an operator but rather the second condition is being used as an index for the ["false", "true"] list.
In case i == n this will be true. Let me remind you that true in python equals to 1
int(True) // = 1
Therefore if i == n, it will be equal to one and will print the element with index one from the list which is "true".
In case i != n, it will be False, equals to 0 which will print the first element from the array which is "false".

This a comparison Operator,
it compares the value or equality of two objects, whereas the Python is operator checks whether two variables point to the same object in memory. In the vast majority of cases, this means you should use the equality operators == and != , except when you're comparing to None.
Output: True or False
Usage: Is used to check whether 2 expressions give the same value.

Related

Short-circuiting with helper function in print() [duplicate]

This question already has answers here:
How do "and" and "or" act with non-boolean values?
(8 answers)
Closed 5 months ago.
Can someone please explain why the following code comes out as "geeks" in the console?
def check():
return "geeks"
print(0 or check() or 1)
I'm assuming that Python recognizes the boolean operator, thus treating 0 == False?
So does that mean every time boolean operators are used, the arguments are treated as True/False values?
The reason that "geeks" will be printed is that or is defined as follows:
The Python or operator returns the first object that evaluates to true
or the last object in the expression, regardless of its truth value.
When check() returns the string "geeks", that value evaluates to True because it is a non-empty string. That value is then the first term in the or expression that evaluates to True, so that value is the result of the entire expression, and so is what gets printed.

Why does "x and y" return "y" when "x" and "y" are integers? [duplicate]

This question already has answers here:
Python's Logical Operator AND [duplicate]
(7 answers)
Closed last year.
So I came across the following syntax for a coding-game solution:
.
.
n and 9
and I didn't knew how to interpret it, thus I tried
2 and 3 #3
3 and 2 #2
.
Why does x and y (seems to) equal y i.e how is this calculated/understood?
This is called short-circuiting in boolean expressions if the first is true(non zero considered as true) it goes to 2nd if this was or if the first was false(0 is considered false) it goes to 2nd try it or as or integers hope this clears stuff up
Chain of logical operators returns the first (counting from left-most value) item that lets you know the logical value of the whole statement. So, in case of and, there are 2 options - one of the values is False, at which point you know that whole statement is False and you return that value without checking anything further on the right, or all of them are True and you return the last one, as only then you know that it will be True.

Advance Logical Operator in Python [duplicate]

This question already has answers here:
Does Python have a ternary conditional operator?
(31 answers)
How do "and" and "or" act with non-boolean values?
(8 answers)
Closed 3 years ago.
I am confused with Logical 'And' and 'or' combination in python
I got some idea from Python's Logical Operator And but I couldn't understand combination of 'AND' and 'OR'
a=3
print(a%2 and 'odd' or 'even')
I understood how even was printed if a is 2 ie.,
a= 2, 2%2 = 0 => False.
Then Note String object is considered as True so 'odd' and 'even' are True.
So False and True(odd) or True(even) will give be False(even) object.
But when I didn't understand how even works. If 1st object is True then the output should be immediate True without checking other condition(or operations), how is it going further and printing 'even'
If you are thinking of trying to emulate JavaScript or other languages ternary operators, you have to do it something like this:
print("even" if a%2 == 0 else "odd")
edit: even though this question has been closed (and I don't believe it should be) I will edit it with the understanding about what your question is actually asking.
In python bool(1) == True and bool(0) == False notice how when you modulo 2 these are the only two possible values you can get.
Now go back to your origional print statement:
print(a%2 and 'odd' or 'even')
Combine the fact that 1 is true and 0 is false with the fact that the first condition of a statement ... and [...] or ... returns if the statement is true and the second returns if it is false.
It is then clear to see that when the number is odd causing the number modulo 2 to be 1 it will return the first condition "odd" and when it is 0 it will cause the second condition to be returned "even".
I hope this explains everything.

What it Means in python? "Apple" > 30 = True [duplicate]

This question already has answers here:
How does Python 2 compare string and int? Why do lists compare as greater than numbers, and tuples greater than lists?
(2 answers)
Closed 3 years ago.
I wish to understand,
"apple" > 10 return True always.
I have mistakenly compared string with integer. instead of raising error it returns boolean.
I want to reason behind it..
When checking string with greater than Number it always return True.
eg 1: '' > 0 = True
eg 2: 'something' > 10 = True
etc, etc.
what it means actually?
I have tried, bytes of string, id etc. i am not sure what it means.
i can understand when if its string > string
here will get result based on sorting order something like below,
>>> 'a' >= 'a'
True
>>> 'apple' >= 'a'
True
>>> 'apple' > 'a'
True
>>> 'apple' > 'b'
Note: in Python 3 it will raises an error. what about python 2.x?
I know its sorting based. number has less precedence than string.
but, is that precedence is based on memory consumption?
I found this definition:
For python2:
"If the comparison is between numeric and non-numeric, the numeric (int, float) is always less than non-numeric and if the comparison is between two non-numeric it's done by lexicographical ordering(str) or alphabetical order of their type-names(list, dict, tuple)."
For python3:
It will return TypeError.

Python: what difference between 'is' and '=='? [duplicate]

This question already has answers here:
Is there a difference between "==" and "is"?
(13 answers)
Closed 9 years ago.
I'm trying next code:
x = 'asd'
y = 'asd'
z = input() #write here string 'asd'. For Python 2.x use raw_input()
x == y # True.
x is y # True.
x == z # True.
x is z # False.
Why we have false in last expression?
is checks for identity. a is b is True iff a and b are the same object (they are both stored in the same memory address).
== checks for equality, which is usually defined by the magic method __eq__ - i.e., a == b is True if a.__eq__(b) is True.
In your case specifically, Python optimizes the two hardcoded strings into the same object (since strings are immutable, there's no danger in that). Since input() will create a string at runtime, it can't do that optimization, so a new string object is created.
is checks not if the object are equal, but if the objects are actually the same object. Since input() always creates a new string, it never is another string.
Python creates one object for all occurrences of the same string literal, that's why x and y point to the same object.

Categories