Page 66 - Programming With Python 3
P. 66
An Introduction to STEM Programming with Python — 2019-09-03a Page 53
Chapter 4 — Collections of Data
Selected List Methods
List methods work differently than st
Free ring methods, they do not return new strings that you need to
assign to a variable or use. These methods actually change the list in memory, directly. You will get
eBook
unusual results if you try to assign these back to the original list.
Append
The first method you need to know is .append(). It does exactly what you would expect a method with
Edition
name to do, it adds a value (or a list of values) to the end of the list. In the example below we can see
that you have earned a 99% on a new assignment, and appended it to the list of grades.
list.append(expression) Method of List
Add a new item or list to the end of an existing list.
Please support this work at
https://docs.python.org/3/library/stdtypes.html#mutable-sequence-types
http://bit.ly/2CBXhaz
http://syw2l.org
Free
1| Grades = [.89, .96, 1.00, .76]
2| grades.append(.99)
3| n = len(grades)
4| s = sum(grades)
5| print("With", n, "grades in the list you have an average
eBook
of",s/n)
With 5 grades in the list you have an average of 0.92
Insert
Edition
You may want to do more than append an item, especially in a list where order is important. To do this
we use the .insert() method. You will pass two values to insert: 1) the location to put the new value, and
2) the value to insert.
Copyright 2019 — James M. Reneau Ph.D. — http://www.syw2l.org — This work is licensed
under a Creative Commons Attribution-ShareAlike 4.0 International License.

