How to define multiple variables in a python for statement

How do I define it while changing the variable name in the for statement?

Use the python built-in function exec

for i in range(5):
    exec('var%d = %d' % (i, i**2))

>>>var0, var1, var2, var3, var4
>>>(0, 1, 4, 9, 16)

There are other ways to put it in an array, but if you want to create multiple variables at once, it is smart to use exec.

Recommended Posts

How to define multiple variables in a python for statement
How to dynamically define variables in Python
[Introduction to Python] How to use the in operator in a for statement?
[Python] How to expand variables in a character string
A simple way to avoid multiple for loops in Python
How to access environment variables in Python
How to get a stacktrace in python
[Introduction to Python] How to output a character string in a Print statement
[For beginners] How to register a library created in Python in PyPI
How to delete multiple specified positions (indexes) in a Python list
How to clear tuples in a list (Python)
How to embed a variable in a python string
How to create a JSON file in Python
How to write a ShellScript Bash for statement
How to notify a Discord channel in Python
[Python] How to draw a histogram in Matplotlib
How to write a string when there are multiple lines in python
How to define Go variables
How to develop in Python
[For beginners] How to use say command in python!
How to convert / restore a string with [] in python
How to run python in virtual space (for MacOS)
Convenient to use matplotlib subplots in a for statement
How to retrieve multiple arrays using slice in python.
When you want to plt.save in a for statement
How to execute a command using subprocess in Python
Turn multiple lists with a for statement at the same time in Python
[Introduction to Python] How to get the index of data with a for statement
How to write a Python class
[Python] How to do PCA in Python
How to collect images in Python
A story about how to specify a relative path in python.
How to use the __call__ method in a Python class
How to use SQLite in Python
How to import a file anywhere you like in Python
[Python] How to write an if statement in one sentence.
How to make a Python package (written for an intern)
I tried "How to get a method decorated in Python"
How to develop in a virtual environment of Python [Memo]
How to specify Cache-Control for blob storage in Azure Storage in Python
How to get the last (last) value in a list in Python
How to implement Python EXE for Windows in Docker container
How to use Mysql in python
How to get a list of built-in exceptions in python
I didn't know how to use the [python] for statement
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to handle Japanese in Python
How to write type hints for variables that are assigned multiple times in one line
How to turn the for statement when there are multiple values for one key in the dictionary
How to determine the existence of a selenium element in Python
How to pass arguments to a Python script in SPSS Modeler Batch
How to make a string into an array or an array into a string in Python
How about Anaconda for building a machine learning environment in Python?
[Python 3.8 ~] How to define a recursive function smartly with a lambda expression
How to check the memory size of a variable in Python
How to configure CORS settings for Azure storage service in Python
How to get a string from a command line argument in python
How to create a heatmap with an arbitrary domain in Python
How to check the memory size of a dictionary in Python