Mã bên dưới sẽ tự động 'tạo' chính nó. Đối với mỗi lần lặp, lệnh sau sẽ được ban hành:
Trong đó số là giá trị của i trong vòng lặp.number is the value of i in the loop.
Kết quả của đoạn mã cụ thể này là ba biến: List1, List2 và List3 đang được tạo và mỗi biến được gán một danh sách trống.
Bạn có thể khái quát điều này để thực tế làm bất cứ điều gì bạn muốn. Xin lưu ý rằng bạn không thể đặt điều này vào một chức năng theo như tôi biết, vì cách các biến và biến toàn cầu hoạt động.
ITDOES không tạo danh sách độc lập, nhưng các biến đề cập đến cùng một danh sách (trống)! & NBSP;
list1, list2, list3, list4
=
name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
44name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
45 name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
3
name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
53name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
54 name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
55
name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
8 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
9The initialized lists are :
List 1 : [1]
List 2 : [2]
List 3 : [3]
List 4 : [4]
0name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
8 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
9The initialized lists are all the same:
List 1 : ["hello there"]
List 2 : ["hello there"]
List 3 : ["hello there"]
List 4 : ["hello there"]
0ITDOES không tạo danh sách độc lập, nhưng các biến đề cập đến cùng một danh sách (trống)! & NBSP;
list1, list2, list3, list4
=
name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
44name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
45 name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
3
name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
53name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
54 name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
55
name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
8 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
9The initialized lists are :
List 1 : [1]
List 2 : [2]
List 3 : [3]
List 4 : [4]
0name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
8 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
9The initialized lists are all the same:
List 1 : ["hello there"]
List 2 : ["hello there"]
List 3 : ["hello there"]
List 4 : ["hello there"]
0Using loops
name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
8 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
9list1, list2, list3, list4
0Python3
Làm thế nào để bạn tạo nhiều danh sách tại một thời điểm trong Python?
name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
5 name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
6name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
8name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
4 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
5 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
6name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
5 name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
6name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
4 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
5 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
6name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
8 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
9name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
03=
9name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
05name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
5 name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
6name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
4 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
5 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
6name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
8 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
9name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
03=
9name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
05name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
5 name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
6name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
4 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
5 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
6name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
8 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
9name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
03=
9name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
05name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
5 name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
6name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
4 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
5 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
6name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
8 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
9=
0Output:
The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
Phương thức số 2: Sử dụng Phương thức DefaultDict () & NBSP;Using defaultdict() Method
Đây là một phương pháp khác nhau và cũng thực hiện một tiện ích hơi khác so với hai phương pháp trên được thảo luận. Điều này tạo ra một từ điển với một tên cụ thể và chúng tôi có tùy chọn để tạo bất kỳ số khóa nào và thực hiện các hoạt động phụ lục ngay lập tức khi chúng được khởi tạo bởi danh sách. & NBSP;
Python3
=
1 =
2
=
3=
=
5=
6.
=
8=
9([]
0
The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
5The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
0=
8([]
4([]
0
The initialized lists are :
List 1 : [1]
List 2 : [2]
List 3 : [3]
List 4 : [4]
5The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
0=
8([]
9([]
0
The initialized lists are all the same:
List 1 : ["hello there"]
List 2 : ["hello there"]
List 3 : ["hello there"]
List 4 : ["hello there"]
5The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
0=
8for
4([]
0
name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
3The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
0name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
5 name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
6name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
8name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
0name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
5 name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
6name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
4 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
5 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
6name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
8 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
9name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
03=
9name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
05name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
5 name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
6name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
4 The initialized lists are :
List 1 : [1]
List 2 : [2]
List 3 : [3]
List 4 : [4]
5 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
6name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
8 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
9name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
03([]
4name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
05name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
5 name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
6name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
4 The initialized lists are all the same:
List 1 : ["hello there"]
List 2 : ["hello there"]
List 3 : ["hello there"]
List 4 : ["hello there"]
5 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
6name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
8 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
9name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
03([]
9name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
05name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
5 name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
6name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
4 name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
3 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
6name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
8 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
9name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
03for
4name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
05Output:
The initialized lists are :
List 1 : [1]
List 2 : [2]
List 3 : [3]
List 4 : [4]
Phương pháp 4: Sử dụng * toán tử: & nbsp; Using * operator:
ITDOES không tạo danh sách độc lập, nhưng các biến đề cập đến cùng một danh sách (trống)! & NBSP;does not create independent lists, but variables referring to the same (empty) list!
Python3
list1, list2, list3, list4
=
name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
44name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
45 name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
3name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
47name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
48The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
0name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
5 name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
6name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
53name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
54 name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
55name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
0name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
5 name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
6name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
4 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
5 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
6name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
8 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
9name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
03=
9name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
05name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
5 name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
6name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
4 The initialized lists are :
List 1 : [1]
List 2 : [2]
List 3 : [3]
List 4 : [4]
5 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
6name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
8 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
9name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
03([]
4name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
05name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
5 name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
6name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
4 The initialized lists are all the same:
List 1 : ["hello there"]
List 2 : ["hello there"]
List 3 : ["hello there"]
List 4 : ["hello there"]
5 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
6name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
8 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
9name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
03([]
9name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
05name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
5 name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
6name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
4 name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
3 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
6name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
7The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
8 The initialized lists are :
List 1 : []
List 2 : []
List 3 : []
List 4 : []
9name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
03for
4name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).
for i in range(1, amount+1):
command_variable = ""
command_variable = name + str(i) + " = " + value
exec(command_variable)
05Output:
The initialized lists are all the same:
List 1 : ["hello there"]
List 2 : ["hello there"]
List 3 : ["hello there"]
List 4 : ["hello there"]
Bạn có thể chỉ cần sử dụng một vòng lặp để tạo danh sách n.use a for loop to create n lists.
Một danh sách trống trong Python có thể được tạo theo hai cách, bằng cách sử dụng dấu ngoặc vuông [] hoặc bằng cách sử dụng hàm tạo danh sách ().by using square brackets [] or by using the list() constructor.