CBSE Class 12 Computer Science Question 61 of 105

Python Revision Tour II — Question 10

Back to all questions
10
Question

Question 10

In the Python shell, do the following :

  1. Define a variable named states that is an empty list.
  2. Add 'Delhi' to the list.
  3. Now add 'Punjab' to the end of the list.
  4. Define a variable states2 that is initialized with 'Rajasthan', 'Gujarat', and 'Kerala'.
  5. Add 'Odisha' to the beginning of the list states2.
  6. Add 'Tripura' so that it is the third state in the list states2.
  7. Add 'Haryana' to the list states2 so that it appears before 'Gujarat'. Do this as if you DO NOT KNOW where 'Gujarat' is in the list. Hint. See what states2.index("Rajasthan") does. What can you conclude about what listname.index(item) does ?
  8. Remove the 5th state from the list states2 and print that state's name.
Answer
  1. states = []
  2. states.append('Delhi')
  3. states.append('Punjab')
  4. states2 = ['Rajasthan', 'Gujarat', 'Kerala']
  5. states2.insert(0,'Odisha')
  6. states2.insert(2,'Tripura')
  7. a = states2.index('Gujarat')
    states2.insert(a - 1,'Haryana')
  8. b = states2.pop(4)
    print(b)