Create a List in Python

Create a List in Python

Author:
Price:

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 array

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

         Syntax: 
                        Variable = [element-1, element-2, element-3 …… element-n]

Example:

               Marks = [10, 23, 41, 75]
               Fruits = [“Apple”, “Orange”, “Mango”, “Banana”]
               MyList = [ ]
        
                                         In the above example, the list Marks has four integer elements; second list Fruits has four string elements; third is an empty list. The elements of a list need not be homogenous type of data. 
The following list contains multiple type elements.

                          Mylist = [ “Welcome”, 3.14, 10, [2, 4, 6] ] 

                                          In the above example, Mylist contains another list as an element. This type of list is known as “Nested List”. Nested list is a list containing another list as an element.

0 Reviews