How to put variables into a list in python.

To add a variable to a list, you can use the append() method of the list. Here is an example:

a = 10
b = "Hello"
c = [1, 2, 3]

my_list = []
my_list.append(a)
my_list.append(b)
my_list.append(c)

print(my_list)  # 输出:[10, 'Hello', [1, 2, 3]]

In the above example, we created three variables a, b, and c and initialized their values. Then, we created an empty list called my_list and used the append() method to add variables a, b, and c to my_list respectively. Finally, we printed the values of my_list, which are [10, ‘Hello’, [1, 2, 3]]. Note that the elements in my_list can have different types.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds