Python Tutorial: Introduction to List

Thursday, 30 July 2020

Introduction to List

Lists:

  •          list is a data structure in Python that is a mutable (changeable).
  •          The list values are assigned by placing them within square braces ([ ]) and separating them by commas and each value is identified by an index.
  •          An index is an integer variable or value that indicates an element of a list.
To declare a list, the syntax is :
<Name of list> = [value1,value2,value3,value4,………. valueN]
For example :
Num=[3,32,56,78,65]                                                           # A list with integer values
Mobile=[‘Samsung’, ‘Nokia’, ‘Vivo’, ‘Redmi’, ‘Oppo’]         # A list with string
Elist =[ ]                                                                              # An empty list
Accessing List of Elements


For example:
>>> Num=[3,32,56,78,65]                                # A list with integer values
>>>print(“The list is :”,Num)
How to Display List Elements?
         
0
1
2
3
4
Samsung
Nokia
Vivo
Redmi
Oppo
Source Code:
Mobile=['Samsung', 'Nokia', 'Vivo', 'Redmi', 'Oppo']
print("Print List elements are :", end=" ")
for i in Mobile:
    print(i, end=" ")

output :

Source Code
0
1
2
3
4
3
32
56
78
65


Num=[3,32,56,78,65]
print("Print List elements are :", end=" ")
for i in Num:
    print(i, end=" ")

Concatenating Lists

Concatenating means joining two operands by linking  them end-to-end.Using + operator concatenate two lists with each other and produce a third list. for example
>>>X= [43,55,67]
>>>Y=[32,65,87]
>>>Z=X+Y
>>>print(Z)
[43,55,67,32,65,87]

Replicating a List

List can be replicated or repeated or repeatedly concatenated with the asterisk operator '*'. 
For example :
>>>aList=[11,22,33]
>>>print((aList*2) #list aList repeats two times.
[11,22,33,11,22,33]
>>>Student=['Sachin','Divy','Yashi']
>>>print(Student*2)    #list Student repeats two times
 ['Sachin','Divy','Yashi,'Sachin','Divy','Yashi']

List in Membership operators:

  • in. The in operator tests whether an element is a member of a list or not. If the element is a member in the list, then it will produce True otherwise False. For Example:
>>> Student=['Satyam','Arif', 'Adil','Deepak','Harshita','Ruku']
>>> 'Deepak' in Student
True
>>> 'Riya' in Student
False
  • not in. The not in operator evaluates to true if it does not find the element in the list and otherwise false. For example :
        >>> 'Deepak' not in Student
        False
        >>> 'Riya' not in Student
        True

    Slicing the Lists:

    Syntax : Seq=aList [start:stop:step]
      aList[strat:stop:step] creates a list slice out of list aList with elements falling between indexes start and stop, not including stop, skipping step-1 elements in between.

    Nested Lists : A list inside another list is called nested list or matrix. For example

            # Accessing nested list
            aList=[21,43,54,[65,76],87,98]
            print(aList)
            print("2nd element :",aList[1])
            print("3rd element :",aList[3])
            print("3rd of 1st element :",aList[3][0])

    # print list address with element

                    Marks=[76,87,54,98,43,67,32]
                    Length=len(Marks)
                    for i in range(Length):
                    print("Marks[",i,"]->",Marks[i])


    1.    Write a program to print a list Num in reverse order.
                    Where, Num=[3,32,56,78,65]
    Source Code
    Num=[3,32,56,78,65]
    ln=len(Num) # length of list
    c=-1
    print("Original List\tReverse List")
    for i in range(0,ln):
        print(Num[i],"\t:\t",Num[c])
        c=c-1



     2. Write a program input number from the keyboard and search element.
    Source Code:
    aList=eval(input("Enter the value of list :"))
    Search=int(input("Enter the value to be search :"))
    length=len(aList)
    flag=False
    for i in range(length):
        if(Search==aList[i]):
            flag=True
            break
    if(flag):
        print(Search,' value position is ',i  )
    else:
        print(Search, " is not found")
    Next

    No comments:

    Post a Comment