File tree Expand file tree Collapse file tree 1 file changed +8
-1
lines changed
Expand file tree Collapse file tree 1 file changed +8
-1
lines changed Original file line number Diff line number Diff line change @@ -61,7 +61,7 @@ def test_tuples():
6161 # pylint: disable=trailing-comma-tuple
6262 singleton_tuple = 'hello' , # <-- note trailing comma
6363 assert len (singleton_tuple ) == 1
64- assert singleton_tuple == ('hello' , )
64+ assert singleton_tuple == ('hello' ,)
6565
6666 # The following example is called tuple packing:
6767 packed_tuple = 12345 , 54321 , 'hello!'
@@ -76,3 +76,10 @@ def test_tuples():
7676 # right-hand side. Sequence unpacking requires that there are as many variables on the left
7777 # side of the equals sign as there are elements in the sequence. Note that multiple assignment
7878 # is really just a combination of tuple packing and sequence unpacking.
79+
80+ # Swapping using tuples.
81+ first_number = 123
82+ second_number = 456
83+ first_number , second_number = second_number , first_number
84+ # Data can be swapped from one variable to another in python using
85+ # tuples. This eliminates the need to use a 'temp' variable.
You can’t perform that action at this time.
0 commit comments