Download presentation
Presentation is loading. Please wait.
1
Introduction to Computer Science
Programming with Python
2
Lists Chapter 8
3
Lists As we want to solve more complex problems in Python, we need more powerful variables. Up to now we have been using simple variables to store numbers or strings where we have a single value in a variable. Starting with lists we will store many values in a single variable using an indexing scheme to store, organize, and retrieve different values from within a single variable. We call these multi-valued variables "collections" or "data structures".
4
Programming Algorithms Data Structures
A set of rules or steps used to solve a problem Data Structures A particular way of organizing data in a computer – ways to use variables differently
5
What is Not A “Collection”?
Most of our variables have one value in them. When we put a new value in the variable, the old value is overwritten What if I want to add both numbers 2 and 4 in variable x?
6
A List is a Kind of Collection
A collection allows us to put many values in a single variable A collection is efficient because we can carry many values around in one convenient package
7
List Constants List constants are surrounded by square brackets and the elements in the list are separated by commas A list element can be any Python object, even another list A list can be empty In other programming languages, lists are known as arrays
8
We Have Already Used Lists
9
Lists and Definite Loops
10
Looking Inside Lists Just like strings, we can get at any single elements in a list using an index specified in square brackets
11
Lists Are Mutable Strings are “immutable” – we cannot change the contents of a string, we must make a new string to make any change Lists are “mutable” – we can change an element of a list using the index operator
12
How Long is a List? The len() function takes a list as a parameter and returns the number of elements in the list Actually len() tells us the number of elements of any set or sequence (such as a string, etc.)
13
Using the Range Function
The range function returns a list of numbers that range from zero to one less than the parameter We can construct an index loop using for and integer iterator
14
A Tale of Two Loops
15
Manipulating Lists Chapter 8
16
Concatenating Lists Using +
We can create a new list by adding two existing lists together
17
Lists Can be Sliced Using :
Remember, just like in strings, the second number is “up to but not including”
18
List Methods
19
Building a List from Scratch
We can create an empty list and then add elements using the append method The list stays in order and new elements are added at the end of the list
20
Is Something in a List? Python provides two operators that let you check if an item is in a list These are logical operators that return True or False They do not modify the list
21
Lists are in Order A list can hold many items and keeps those items in the order until we do something to change the order A list can be sorted (i.e., change its order) The sort method (unlike in strings) means “sort yourself”
22
Built-in Functions There are a number of functions built into Python that takes lists as parameters Remember the loops we built? There much simpler.
24
This will keep numbers in memory
This will keep numbers in memory. If we have millions/billions of numbers, this will affect performance
25
Exercise Instead of using the built-in function sum, implement it yourself and call it whenever you need to sum values
26
Strings and Lists – Best Friends
Split breaks a string into parts and produces a list of strings. We think of these as words. We can access a particular word or loop through all the words
27
Strings and Lists When you do not specify a delimiter, multiple spaces are treated like one delimiter You can specify what delimiter character to use in the splitting
28
List’s Functions remove() – removes the first matching value, not a specified index >>> a = [0, 2, 3, 2] >>> a.remove(2) >>> a [0, 3, 2] del() – removes the item at a specific index >>> a = [3, 2, 2, 1] >>> del a[1] >>> a [3, 2, 1]
29
List’s Functions pop() – removes the item at a specific index and returns it (Queue)? >>> a = [4, 3, 5] >>> a.pop(1) 3 >>> a [4, 5] sort() – will sort the list in ascending order reverse() – reverses the list
30
STOP HERE! Chapter 8
31
From Stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
32
The Double Split Pattern
Sometimes we split a line one way, and then grab one of the pieces of the line and split that piece again From Sat Jan 5 09:14: words = line.split() = words[1]
33
The Double Split Pattern
Sometimes we split a line one way, and then grab one of the pieces of the line and split that piece again From Sat Jan 5 09:14: words = line.split() = words[1]
34
The Double Split Pattern
Sometimes we split a line one way, and then grab one of the pieces of the line and split that piece again From Sat Jan 5 09:14: words = line.split() = words[1] pieces = [‘Stephen.marquard’, ‘uct.ac.za’]
35
The Double Split Pattern
Sometimes we split a line one way, and then grab one of the pieces of the line and split that piece again From Sat Jan 5 09:14: words = line.split() = words[1] pieces = [‘Stephen.marquard’, ‘uct.ac.za’] print(pieces[1]) ‘uct.ac.za’
36
Exercise – Live Demo In here we will read the file from py4e and split some data to get relevant information
37
Summary Concept of a collection Lists and definite loops
Indexing and lookup List mutability Functions: len(), min(), max(), sum() Slicing lists List methods: append, remove Sorting lists Splitting strings into lists of words Using split to parse strings
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.