Notes on exchanging and multiple assignment of Python variable values ​​learned in quiz format

quiz

Answer the output of the following program.

Question 1

1


nums = [1, 2, 3]
i = 0
nums[i], i = i, nums[i]

print(nums)
See answer [0, 2, 3]

Question 2

2


nums = [1, 2, 3]
i = 0
i, nums[i] = nums[i], i

print(nums)
See answer [1, 0, 3]

Execution order of multiple assignments

Python allows you to assign values ​​to multiple variables on a single line.

x, y = 0, 1

print(x)  # 0
print(y)  # 1

You can also use this to exchange the values ​​of variables.

x = 0
y = 1

x, y = y, x

print(x)  # 1
print(y)  # 0

Compared to exchanging values ​​without using multiple assignment as shown below, it is convenient because it does not require new variables and the number of lines can be shortened.

x = 0
y = 1

temp = x
x = y
y = temp

print(x)  # 1
print(y)  # 0

However, if it is not a simple exchange like this quiz, you need to pay attention to the execution order.

The value of the variable on the right side of the assignment statement is fixed by the value before execution of the assignment statement, while the value of the variable on the left side is updated in order from the left.

Question 1 was such a code.

1


nums = [1, 2, 3]
i = 0
nums[i], i = i, nums[i]

print(nums)  # [0, 2, 3]

The value of i on the right side of multiple assignment is 0, and the value ofnums [i]on the right side, that is,nums [0]is 1.

Since the values ​​of the variables on the left side are updated in order from the left, Question 1 is the same as the code below.

1'


nums = [1, 2, 3]
i = 0

#    nums[i], i = i, nums[i]
# => nums[i], i = 0, nums[0]
# => nums[i], i = 0, 1
nums[i] = 0  # => nums[0] = 0
i = 1

print(nums)  # [0, 2, 3]

Question 2 was such a code.

2


nums = [1, 2, 3]
i = 0
i, nums[i] = nums[i], i

print(nums)  # [1, 0, 3]

Question 2 is the same as the code below.

2'


nums = [1, 2, 3]
i = 0

#    i, nums[i] = nums[i], i
# => i, nums[i] = nums[0], 0
# => i, nums[i] = 1, 0
i = 1
nums[i] = 0  # => nums[1] = 0

print(nums)  # [1, 0, 3]

reference

-7.2. Assignment statement — 7. Simple statement — Python 3.9.1 Document

Recommended Posts

Notes on exchanging and multiple assignment of Python variable values ​​learned in quiz format
Notes on reading and writing float32 TIFF images in python
Notes on Python and dictionary types
Notes on tf.function and Tracing
Notes on * args and ** kargs
Notes on pyenv and Atom
Introduction and tips of mlflow.Tracking
Notes on Python and dictionary types
Notes on using post-receive and post-merge
Notes on standard input / output of Go
Notes on building Python and pyenv on Mac
Fill the missing value (null) of DataFrame with the values before and after with pyspark
Notes on installing Python3 and using pip on Windows7
Basic operation of Python Pandas Series and Dataframe (1)
Notes on exchanging and multiple assignment of Python variable values ​​learned in quiz format
Deep Learning from scratch-Chapter 4 tips on deep learning theory and implementation learned in Python
Notes using cChardet and python3-chardet in Python 3.3.1.
Notes on nfc.ContactlessFrontend () for nfcpy in python
Notes on building Python and pyenv on Mac
[Python] Various combinations of strings and values
Notes on using code formatter in Python
Project Euler # 1 "Multiples of 3 and 5" in Python
Python variables and data types learned in chemoinformatics
Notes on installing Python3 and using pip on Windows7
Examine any combination of values in multiple arrays
[Python] Takes representative values ​​of multiple images [Numpy]
Notes on using dict in python [Competition Pro]
Zero padding for dynamic variable values in Python
ABC125_C --GCD on Blackboard [Notes solved in Python]
TensorFlow: Run data learned in Python on Android
Explanation of edit distance and implementation in Python
[Example of Python improvement] I learned the basics of Python on a free site in 2 weeks.
[Introduction to Python] Summary of functions and methods that frequently appear in Python [Problem format]
Notes on how to use StatsModels that can use linear regression and GLM in python
format in python
Full-width and half-width processing of CSV data in Python
Calculation of standard deviation and correlation coefficient in Python
Notes on HDR and RAW image processing with Python
Difference between Ruby and Python in terms of variables
Logical symbols learned in marriage (and implementation examples in Python)
[python] Calculation of months and years of difference in datetime
Display a histogram of image brightness values in python
Overview of generalized linear models and implementation in Python
Sample of getting module name and class name in Python
Summary of date processing in Python (datetime and dateutil)
[Python] Types of statistical values (features) and calculation methods
Let's statically check and format the code of E2E automatic test written in Python [VS Code]