How to Create a List of Integers in Python?

Estimated read time 1 min read

You can create a list of integers in Python by enclosing a sequence of comma-separated integers within square brackets. Here’s an example:

my_list = [1, 2, 3, 4, 5]

In this example, the list contains the integers 1, 2, 3, 4, and 5.

You can also create an empty list and then append integers to it using the append() method. Here’s an example:

my_list = []  # create an empty list

my_list.append(1)  # add the integer 1 to the list
my_list.append(2)  # add the integer 2 to the list
my_list.append(3)  # add the integer 3 to the list

print(my_list)  # prints [1, 2, 3]

In this example, we first create an empty list my_list and then use the append() method to add integers to it one at a time. Finally, we print the list using the print() function.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply