Posts

Showing posts with the label Lists

Python Programming Language - Lists

Image
A list is an ordered sequence of elements. It is very flexible and is one of the most used types in Python.  Lists are a mutable type. Creating a list To make a list, we use square brackets [], inside which we list the elements of a list, separated by spaces. For instance, let's make a list of integers: numbers = [ 1 , 2 , 3 , 4 , 5 ] In a similar manner, you can also define lists with other types of data. For instance, let’s define a string list: twd = [ "Carl" , "Rick" , "Lori" ] The list() constructor can also be used to make a list: numbers1 = [] numbers2 = list() These two list definitions are the same, they both produce an empty list. The list doesn't have to be just a collection of objects of one type. We can also add strings, numbers, or other data types to the same list: objects = [ 4 , 5.66 , "Carl" , True ] The standard print function can be used to print the content of a list in a human-readable format: numbers = [ 15 , 25 , 3