Read more
Introduction to List
Python programming language has four collections of data types such as List, Tuples, Set and Dictionary. A list in Python is known as a “sequence data type” like strings. It is an ordered collection of values enclosed within square brackets [ ]. Each value of a list is called as element. It can be of any type such as numbers, characters, strings and even the nested lists as well. Th e elements can be modifi ed or mutable which means the elements can be replaced, added or removed. Every element rests at some position in the list. Th e position of an element is indexed with numbers beginning with zero which is used to locate and access a particular element. Th us, lists are similar to arrays
Programming in Python
In Python, programs can be written in two ways namely Interactive mode and Script mode. The Interactive mode allows us to write codes in Python command prompt (>>>) whereas in script mode programs can be written and stored as separate file with the extension .py and executed. Script mode is used to create and edit python source file
Create a List in Python
In python, a list is simply created by using square bracket. Th e elements of list should be specifi ed within square brackets. Th e following syntax explains the creation of list.
Adding more elements in a list
In Python, append( ) function is used to add a single element and extend( ) function is used to add more than one element to an existing list.
Syntax:
List.append (element to be added)
List.extend ( [elements to be added])
In extend( ) function, multiple elements should be specified within square bracket as arguments of the function.
Example:
>>> Mylist=[34, 45, 48]
>>> Mylist.append(90)
>>> print(Mylist)
[34, 45, 48, 90]
In the above example, Mylist is created with three elements. Through >>> Mylist.append(90) statement, an additional value 90 is included with the existing list as last element, following print statement shows all the elements within the list MyList.
0 Reviews