python batch assignment operation instance

  • 2020-12-26 05:48:06
  • OfStack

The variable name and value change regularly, and the for cycle is used to complete the assignment operation.

Here's an example:


for i in range(1, 10):
 exec("t%d=i"%i)
 
print(t1)
print(t2)
print(t3)
print(t4)
print(t5)
print(t6)
print(t7)
print(t8)
print(t9)

Execution Results:


1
2
3
4
5
6
7
8
9

This is done using the exec() function in python.


Related articles: