Does Python have the = operator?

Regardless, you’re pretty familiar with comma-related code:

1. Components of a tuple

A parenthesis without a comma does not constitute a tuple. Even if there is only one element, a comma is required.

In [6]: a = (1,)

In [7] :type(a)
Out[7] :tuple
Copy the code

2. Swap two variables

In [8]: a = 1

In [9]: b = 2

In [10]: a, b = b, a

In [11] :f"{a = } {b = }"
Out[11] :'a = 2 b = 1'
Copy the code

If you want to swap three, four, or more, you can do that.

3. Disassemble lists, tuples, or sets

In [12]: a, b = [1.2]

In [13] :f"{a = } {b = }"
Out[13] :'a = 1 b = 2'

In [14]: a, b = (3.4)

In [15] :f"{a = } {b = }"
Out[15] :'a = 3 b = 4'

In [17]: a, b = {5.6}

In [18] :f"{a = } {b = }"
Out[18] :'a = 5 b = 6'

In [19] :Copy the code

4. The = operator?

With that in mind, what is the result of the following code?

a ,= [2]
b , = [2]
c , = (2,)
d , = {2}
e ,= range(2.3)
print(a,b,c,d,e)
Copy the code

If you have a result in mind, check to see if it meets your expectations:

>>> a,=[2]
>>> b , = [2]
>>> c , = (2.)>>> d , ={2}
>>> e ,     = range(2.3)
>>> print(a,b,c,d,e)
2 2 2 2 2
>>>
Copy the code

As long as the iterable has only one element inside it, you can take the value out of it as equals, and it doesn’t matter if the comma and the equals sign are next to each other.

In a word,,= breaks down lists, tuples, sets, and special cases of iterable objects requiring only one element.

The last

Is there any increase in strange knowledge? If you find something, please give it a thumbs up