List in python

 List:

  • The list data type is used to store the collection of an item in an ordered sequence.
  • These items are mutable, which means changeable as possible.
  • It is denoted by square bracket [].
  • Its index value is started with 0 and ends with list_length-1.

Syntax:

  1. Declaring list:

    Variable_name=[]

    Example:

    List=[]

    Declaring value into the list:

    Variable_name=[item1,item2,...item n]

    Example:

    a=[1000,’Kurshitha’,’female’,450]

Example program:

  1. a=[5,1,8,3]

    print("Fist value : ",a[0])

    print("Last value : ",a[len(a)-1])

    print("Another method to print last value : ",a[-1])

    print("List of elements : ",a)

Output:

Print list in looping statement:

  1. a=[5,1,8,3]

    for i in a:

        print(i)

Output:

Insert or append an item into the list:

  • To insert an element in the list is used append() function.

  1. pin=[1234,1111,1211,1457]

    p=int(input("Enter an item to add in list : "))

    pin.append(p)

    print("Your list of items are: ")

    for i in pin:

        print(i)

Output:


Next                                                                                                    Preview                                                                                                          

Share:

No comments:

Post a Comment

Popular Posts

Search This Blog

Powered by Blogger.

Service Support

Need our help to Learn or Post New Concepts Contact me

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me