Lists

Lists

Lists

Lists: 

A list is a collection of items that are ordered and changeable. Lists are written with square brackets and each item is separated by a comma. Here is an example of a list with three items:

				
					my_list = [1, "hello", True] 
				
			

In this example, my_list is a list that contains an integer, a string, and a boolean value. You can access individual items in the list by their index, which starts at 0 for the first item

				
					print(my_list[0])  # prints 1
print(my_list[1])  # prints "hello"
print(my_list[2])  # prints True

				
			

You can also change the value of an item in the list:

				
					my_list[0] = 2 
print(my_list) # prints [2, "hello", True] 

				
			

You can add new items to the end of a list using the append method:

				
					my_list.append("world") 
print(my_list) # prints [2, "hello", True, "world"] 

				
			

You can find the length of a list using the len function:

				
					print(len(my_list)) # prints 4
				
			
Join To Get Our Newsletter
Spread the love