Answered step by step
Verified Expert Solution
Link Copied!

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
1. Open Visual Studio Code then open this week's HOS assignment that you just cloned
from GitHub.
1. 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 comma-separated values between square brackets.
For example
list1=['physics', 'chemistry', 1997,2000];
list2=[1,2,3,4,5];
list3=["a","b","c","d"]
Python considers the first item in a list to be at position 0, not position 1.
1. Under Module1 create a file called List_basic.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.
1. In the same file List_basic.py add the below code
2. Type the following in the terminalto check the output of the above code
>>> python3 List_basic.py
Updating Lists
1. In the same file List_basic.py add the below code
2. Type the following in the terminalto check the output of the above code
>>> python3 List_basic.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 append()method
makes it easy to build lists dynamically.
1. In the same file List_basic.py add the below code
2. Type the following in the terminalto check the output of the above code
>>> python3 List_basic.py
Adding element to a list- Inserting elements
You can add a new element at any position in your list by using the insert()method. You do this
by specifying the index of the new element and the value of the new item.
1. In the same file List_basic.py add the below code
2. Type the following in the terminalto check the output of the above code
>>> python3 List_basic.py
Removing element
There are 3 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.
1. Under Module1 create a file List_remove.py and type the following code.
2. Type the following in the terminalto check the output of the above code
>>> python3List_remove.py
The del used at index 1 deletes the value Yamaha. You can no longer access the value
that was removed from the list after thedelstatement is used.
3. Add the below code in the same file
4. Type the following in the terminalto check the output of the above code
>>>python3List_remove.py
Atpop(), a value from the list and store that value in the variablepopped_motorcycle. 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 variablepopped_motorcycle.
5. Add the below code in the same file
6. Type the following in the terminalto check the output of the above code
>>>python3List_remove.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
1. Create a file replaceNegative.py and enter the following code. The program replaces any
negative numbers in the list with positive ones.
2. 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing Fundamentals, Design, and Implementation

Authors: David M. Kroenke, David J. Auer

14th edition

133876705, 9781292107639, 1292107634, 978-0133876703

Students also viewed these Databases questions

Question

Is financial support available for travel to conferences?

Answered: 1 week ago

Question

Why should an individual manager be interested in supporting HR?

Answered: 1 week ago