Auto AdSense

Saturday, 24 January 2015

Python Lists



  • Accessing Values in Lists:
    #!/usr/bin/python

    list1 = ['physics', 'chemistry', 1997, 2000];
    list2 = [1, 2, 3, 4, 5, 6, 7 ];

    print "list1[0]: ", list1[0]
    print "list2[1:5]: ", list2[1:5]
  • Output
    list1[0]: physics
    list2[1:5]: [2, 3, 4, 5]
  • Updating Lists:
    #!/usr/bin/python

    list = ['physics', 'chemistry', 1997, 2000];

    print "Value available at index 2 : "
    print list[2];
    list[2] = 2001;
    print "New value available at index 2 : "
    print list[2];
  • Output
    Value available at index 2 :
    1997
    New value available at index 2 :
    2001
  • Delete List Elements:
    #!/usr/bin/python

    list1 = ['physics', 'chemistry', 1997, 2000];

    print list1;
    del list1[2];
    print "After deleting value at index 2 : "
    print list1;
  • Output
    ['physics', 'chemistry', 1997, 2000]
    After deleting value at index 2 :
    ['physics', 'chemistry', 2000]

No comments:

Post a Comment