Computer Science

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.

Python List Manipulation

6 Likes

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)

Answered By

5 Likes


Related Questions