Day 5 of #30DaysOfCode : List Operations

Day 5 of #30DaysOfCode : List Operations

Question:

In the GitHub repository, Infinity-AI(Infinite Learn and Grow)/30-days-code inside the Day-5 folder create name.py file as defined in the readme.md file.

  • Declare IT_Company list with more than 5 values.

  • Print the first, middle and last company

  • Insert company name at the second position and last position.

  • Reverse the list in descending order using the reverse() method

  • Remove the last IT company from the list

  • Destroy the IT company's list

Solution:

A list is used to store multiple values. In a list, we can store multiple values.

In the list, we have an index that starts from so to access an element of the list we can directly write like name[index]. So for the first element of the list

For the last element of the list, we first need to know the length of the list. So for that, we will be using len() function and saving it in one variable.

For the middle element, we will be doing Last/2. There is one problem with that what if we got an odd number as last so we need to typecast it to integer as shown below?

Now to insert the element on the 2nd position we add the value on index 1 and to add the element we use the insert(index, value) function.

To add the value at the end of the list we use the append() function.

To reverse the list we will be using reverse() method

To remove the last element from the list we use remove() method.

To destroy the whole list we will be using the clear() method.

Thank you :)