Python Comma Seperate Variables [duplicate] - python

This question already has answers here:
How does Python's comma operator work during assignment?
(3 answers)
Closed last month.
I am trying to understand how this comma separated solution works and how it is called, example:
b, c=0, 5
print(b,c)
>>0 5
Why is variable 'b' value 0?
Why is variable 'c' value 5?

Comma separated values are interpreted as tuple, which can be unpacked:
t = 0,5 # sames as t = (0,5)
print(type(t))
a,b = t
print(a,b)
Out:
<class 'tuple'>
0 5

Related

or operator does not returns boolean type [duplicate]

This question already has answers here:
How do "and" and "or" act with non-boolean values?
(8 answers)
How to test multiple variables for equality against a single value?
(31 answers)
Closed 1 year ago.
enter image description here
i'm now working on if statements
this is my code
a = [1,2,3]
print(4 or 5 in a)
and the outcome is 4 not False
this code returns what i have expected
a = [1,2,3]
print((4 or 5) in a)
I can't understand how or operator works
why the return is integer..? not True or False
If there are multiple conditions in an if statement, if the first statement is correct, it doesn't check the second statement. Since 4 is true, it doesn't check if 5 is in the variable a or not. Therefore, it returns the value 4 and not False
In this case you have expression written incorrectly and as a result it evaluates the way it does. What you should have written was:
>>> a = [2, 1, 4]
>>> 4 in a or 5 in a
Out[18]: True
In your case the expression evaluates only the condition 4 or 5 which evaluates to 4 as the first truthy value.
>>> 4 or 5 in a
Out[27]: 4
>>> 4 or 5
Out[28]: 4
In fact, variable a is completely skipped here and not evaluated at all. We can do the same but using variable b that has never been defined in this session and we get 4 instead of UnboundLocalError:
>>> 4 or 5 in b
Out[31]: 4
Similarly we can test this behaviour. When you compare two truthy values the result is always the first one the interpreter encounters.
>>> 'a' or 'b'
Out[29]: 'a'
>>> 'd' or 'a'
Out[30]: 'd'
I suggest reading docs on boolean operators in Python to get a better understanding of what's happening (for example, here). It can get confusing.

Why does 'a' and 'b' equal 'b' in Python? [duplicate]

This question already has answers here:
How do "and" and "or" act with non-boolean values?
(8 answers)
How does the logical `and` operator work with integers? [duplicate]
(2 answers)
"4 and 5" is 5, while "4 or 5" is 4. Is there any reason? [duplicate]
(4 answers)
Closed 3 years ago.
What does 'a' and 'b' in Python mean and why does it equal 'b'? Why does it not equal 'a'?
>>> 'a' and 'b'
'b'
From the Pycharm docs:
The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.
Because 'a' is not False 'b' is evaluated and returned.
Also nice to know. What is evaluated as True and what as False:
the following values are interpreted as false: False, None, numeric zero of all types, and empty strings and containers (including strings, tuples, lists, dictionaries, sets and frozensets). All other values are interpreted as true.
Either 'a' or 'b' would be an acceptable answer to 'a' and 'b' (since both are truthy), but only False would be an acceptable answer to 'a' and False, just as only 0 would be an acceptable answer to 'a' and 0 (since the result of this evaluation must be false-y to be logically correct).
Having a short-circuiting boolean evaluation follow the right-hand path when the left-hand is true allows there to be a single rule that applies in all cases.

Python is operator [duplicate]

This question already has answers here:
Understanding the "is" operator [duplicate]
(11 answers)
Two variables in Python have same id, but not lists or tuples
(5 answers)
Closed 5 years ago.
I understand that the "is" operator checks identity between two objects, but what is this?
a=25
b=25
a is b
True
Why is this true if
a = [1,2,3]
b = [1,2,3]
a is b
False
And from this post, https://www.quora.com/Python-is-operator, it says all immutable objects, like integers from the first example, will have the first example's result, True, but tuples are immutable and this happens:
a = (1,2)
b = (1,2)
a is b
False
Can someone explain please?
Thanks.

Why are the lines given as tuples in matplotlib [duplicate]

This question already has answers here:
What is the purpose of the single underscore "_" variable in Python?
(5 answers)
Closed 8 months ago.
Reading through Peter Norvig's Solving Every Sudoku Puzzle essay, I've encountered a few Python idioms that I've never seen before.
I'm aware that a function can return a tuple/list of values, in which case you can assign multiple variables to the results, such as
def f():
return 1,2
a, b = f()
But what is the meaning of each of the following?
d2, = values[s] ## values[s] is a string and at this point len(values[s]) is 1
If len(values[s]) == 1, then how is this statement different than d2 = values[s]?
Another question about using an underscore in the assignment here:
_,s = min((len(values[s]), s) for s in squares if len(values[s]) > 1)
Does the underscore have the effect of basically discarding the first value returned in the list?
d2, = values[s] is just like a,b=f(), except for unpacking 1 element tuples.
>>> T=(1,)
>>> a=T
>>> a
(1,)
>>> b,=T
>>> b
1
>>>
a is tuple, b is an integer.
_ is like any other variable name but usually it means "I don't care about this variable".
The second question: it is "value unpacking". When a function returns a tuple, you can unpack its elements.
>>> x=("v1", "v2")
>>> a,b = x
>>> print a,b
v1 v2
The _ in the Python shell also refers to the value of the last operation. Hence
>>> 1
1
>>> _
1
The commas refer to tuple unpacking. What happens is that the return value is a tuple, and so it is unpacked into the variables separated by commas, in the order of the tuple's elements.
You can use the trailing comma in a tuple like this:
>>> (2,)*2
(2, 2)
>>> (2)*2
4

Meaning of using commas and underscores with Python assignment operator? [duplicate]

This question already has answers here:
What is the purpose of the single underscore "_" variable in Python?
(5 answers)
Closed 8 months ago.
Reading through Peter Norvig's Solving Every Sudoku Puzzle essay, I've encountered a few Python idioms that I've never seen before.
I'm aware that a function can return a tuple/list of values, in which case you can assign multiple variables to the results, such as
def f():
return 1,2
a, b = f()
But what is the meaning of each of the following?
d2, = values[s] ## values[s] is a string and at this point len(values[s]) is 1
If len(values[s]) == 1, then how is this statement different than d2 = values[s]?
Another question about using an underscore in the assignment here:
_,s = min((len(values[s]), s) for s in squares if len(values[s]) > 1)
Does the underscore have the effect of basically discarding the first value returned in the list?
d2, = values[s] is just like a,b=f(), except for unpacking 1 element tuples.
>>> T=(1,)
>>> a=T
>>> a
(1,)
>>> b,=T
>>> b
1
>>>
a is tuple, b is an integer.
_ is like any other variable name but usually it means "I don't care about this variable".
The second question: it is "value unpacking". When a function returns a tuple, you can unpack its elements.
>>> x=("v1", "v2")
>>> a,b = x
>>> print a,b
v1 v2
The _ in the Python shell also refers to the value of the last operation. Hence
>>> 1
1
>>> _
1
The commas refer to tuple unpacking. What happens is that the return value is a tuple, and so it is unpacked into the variables separated by commas, in the order of the tuple's elements.
You can use the trailing comma in a tuple like this:
>>> (2,)*2
(2, 2)
>>> (2)*2
4

Categories

Resources