CP104 Lecture 12: Lecture 12 - Lists

47 views4 pages
School
Course
Professor
hayjayshay and 38575 others unlocked
CP104 Full Course Notes
2
CP104 Full Course Notes
Verified Note
2 documents

Document Summary

A list is an object that contains multiple data items. Lists in python can shrink/grow at any time therefore items in the lists can be changed. Can access items in a list by using index (marks [2] = 82) Tuples (cid:272)a(cid:374)(cid:859)t (cid:271)e (cid:272)ha(cid:374)ged but are much more efficient. Running total ver 1 marks = [30, 16, 32] grade2 = marks[0] + marks[1] +marks[2] Running total ver 2 marks = [30, 16, 32] grade3 = 0 for i in range(len(marks)): grade3 = grade3 + marks[i] s =(cid:859)da(cid:448)id(cid:859) ls = list (s) print(ls) You can concatenate, repeat, slice, length, operator, iteration, membership lists just like with strings. Ex 1) low = [1, 2, 3, 4, 5] high = [6, 7, 8, 9] all = low + high print(all) Functions with lists: def calc_average(marks): total = 0 for item in marks: total = total + item avg = total/len(marks) return avg.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents