Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Lists, Looping the Lists, Functions 1 . Open Visual Studio Code then open this week's HOS assignment that you just cloned from GitHub. 1 .
Lists, Looping the Lists, Functions
Open Visual Studio Code then open this week's HOS assignment that you just cloned
from GitHub.
Open the repository that you just cloned from GitHub.
Lists
The list is a most versatile datatype available in Python which can be written as a list of commaseparated values items between square brackets. The important thing about a list is that items
in a list need not be of the same type.
Creating a list is as simple as putting different commaseparated values between square brackets.
For example
listphysics 'chemistry', ;
list;
listabcd
Python considers the first item in a list to be at position not position
Under Module create a file called Listbasic.py and type the following code
Accessing values
To access values in lists, use the square brackets for slicing along with the index or indices to
obtain value available at that index.
In the same file Listbasic.py add the below code
Type the following in the terminalto check the output of the above code
python Listbasic.py
Updating Lists
In the same file Listbasic.py add the below code
Type the following in the terminalto check the output of the above code
python Listbasic.py
Adding element to a list Appending
The simplest way to add a new element to a list is to appendthe item to the list. When you
append an item to a list, the new element is added to the end of the list. The appendmethod
makes it easy to build lists dynamically.
In the same file Listbasic.py add the below code
Type the following in the terminalto check the output of the above code
python Listbasic.py
Adding element to a list Inserting elements
You can add a new element at any position in your list by using the insertmethod You do this
by specifying the index of the new element and the value of the new item.
In the same file Listbasic.py add the below code
Type the following in the terminalto check the output of the above code
python Listbasic.py
Removing element
There are ways to remove an element from a list.
i If you know the position of the item you want to remove from a list, you can use
the del statement.
ii The pop method removes the last item in a list, but it lets you work with that item after
removing it The term pop comes from thinking of a list as a stack of items and popping
one item off the top of the stack. In this analogy, the top of a stack corresponds to the end
of a list. You can use pop to remove an item from any position in a list by including the
index of the item you want to remove in parentheses.
iii If you only know the value of the item you want to remove, you can use
the remove method.
Under Module create a file Listremove.py and type the following code.
Type the following in the terminalto check the output of the above code
pythonListremove.py
The del used at index deletes the value Yamaha. You can no longer access the value
that was removed from the list after thedelstatement is used.
Add the below code in the same file
Type the following in the terminalto check the output of the above code
pythonListremove.py
Atpop a value from the list and store that value in the variablepoppedmotorcycle. We print
the listto show that a value has been removed from the list. Then we print the popped value to
prove that we still have access to the value that was removed.
The output shows that the value suzukiwas removed from the end of the list and is now
assigned to the variablepoppedmotorcycle.
Add the below code in the same file
Type the following in the terminalto check the output of the above code
pythonListremove.py
The code removes ducati from the list. You can use the remove method to work with a value
that is being removed from a list.
Using Loop in lists
Create a file replaceNegative.py and enter the following code. The program replaces any
negative numbers in the list with positive ones.
Type the following in the terminalto check the output of the above code.write the answer each step clearly including images
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started