Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Python learn 04 Lists

This document provides an overview of Python lists, explaining their characteristics as collections that can hold multiple values and are mutable. It covers list constants, indexing, slicing, and built-in functions, as well as how to check for the presence of items in a list. Additionally, it introduces the concept of the double split pattern for processing strings.

Uploaded by

abhinav.mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python learn 04 Lists

This document provides an overview of Python lists, explaining their characteristics as collections that can hold multiple values and are mutable. It covers list constants, indexing, slicing, and built-in functions, as well as how to check for the presence of items in a list. Additionally, it introduces the concept of the double split pattern for processing strings.

Uploaded by

abhinav.mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Python Lists

Chapter 4
Programming
• Algorithm
- A set of rules or steps used to solve a problem

• Data Structure
- A particular way of organizing data in a computer

https://en.wikipedia.org/wiki/Algorithm
https://en.wikipedia.org/wiki/Data_structure
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

$ python
>>> x = 2
>>> x = 4
>>> print(x)
4
A List is a Kind of
Collection
• A collection allows us to put many values in a single “variable”

• A collection is nice because we can carry all many values


around in one convenient package.

friends = [ 'Joseph', 'Glenn', 'Sally' ]

carryon = [ 'socks', 'shirt', 'perfume' ]


List Constants
• List constants are surrounded by >>> print([1, 24, 76])
square brackets and the elements [1, 24, 76]
>>> print(['red', 'yellow',
in the list are separated by 'blue'])
commas ['red', 'yellow', 'blue']
>>> print(['red', 24, 98.6])
• A list element can be any Python ['red', 24, 98.6]
>>> print([ 1, [5, 6], 7])
object - even another list [1, [5, 6], 7]
>>> print([])
• A list can be empty []
We Already Use Lists!
5
for i in [5, 4, 3, 2, 1] :
print(i) 4
print('Blastoff!') 3
2
1
Blastoff!
Lists and Definite Loops - Best Pals

friends = ['Joseph', 'Glenn', 'Sally']


for friend in friends : Happy New Year: Joseph
print('Happy New Year:', friend)
print('Done!') Happy New Year: Glenn
Happy New Year: Sally
Done!
z = ['Joseph', 'Glenn', 'Sally']
for x in z:
print('Happy New Year:', x)
print('Done!')
Looking Inside Lists

Just like strings, we can get at any single element in a list using an
index specified in square brackets

>>> friends = [ 'Joseph', 'Glenn', 'Sally' ]


Joseph Glenn Sally >>> print(friends[1])
Glenn
0 1 2 >>>
Lists are Mutable
>>> fruit = 'Banana'
>>> fruit[0] = 'b'
• Strings are “immutable” - we Traceback
cannot change the contents of a TypeError: 'str' object does not
string - we must make a new string support item assignment
>>> x = fruit.lower()
to make any change >>> print(x)
banana
>>> lotto = [2, 14, 26, 41, 63]
• Lists are “mutable” - we can >>> print(lotto)
change an element of a list using [2, 14, 26, 41, 63]
>>> lotto[2] = 28
the index operator >>> print(lotto)
[2, 14, 28, 41, 63]
How Long is a List?

• The len() function takes a list as a >>> greet = 'Hello Bob'


parameter and returns the number >>> print(len(greet))
of elements in the list 9
>>> x = [ 1, 2, 'joe', 99]
>>> print(len(x))
• Actually len() tells us the number of 4
elements of any set or sequence >>>
(such as a string...)
Using the range Function
• The range function returns
>>> print(range(4))
a list of numbers that range [0, 1, 2, 3]
from zero to one less than >>> friends = ['Joseph', 'Glenn', 'Sally']
>>> print(len(friends))
the parameter 3
>>> print(list(range(len(friends))))
[0, 1, 2]
• We can construct an index >>>
loop using for and an
integer iterator
Concatenating Lists Using +
>>> a = [1, 2, 3]
We can create a new list >>> b = [4, 5, 6]
by adding two existing >>> c = a + b
>>> print(c)
lists together
[1, 2, 3, 4, 5, 6]
>>> print(a)
[1, 2, 3]
Lists Can Be Sliced Using :
>>> t = [9, 41, 12, 3, 74, 15]
>>> t[1:3]
[41,12] Remember: Just like in
>>> t[:4] strings, the second
[9, 41, 12, 3] number is “up to but not
>>> t[3:]
including”
[3, 74, 15]
>>> t[:]
[9, 41, 12, 3, 74, 15]
Is Something in a List?
• Python provides two operators >>> some = [1, 9, 21, 10, 16]
that let you check if an item is >>> 9 in some
True
in a list
>>> 15 in some
False
• These are logical operators >>> 20 not in some
that return True or False True
>>>
• They do not modify the list
Lists are in Order
• A list can hold many
items and keeps
those items in the
order until we do >>> friends = [ 'Joseph', 'Glenn', 'Sally' ]
>>> friends.sort()
something to change >>> print(friends)
the order ['Glenn', 'Joseph', 'Sally']
>>> print(friends[1])
• A list can be sorted Joseph
(i.e., change its order) >>>

• The sort method


(unlike in strings)
means “sort yourself”
Built-in Functions and Lists
>>> nums = [3, 41, 12, 9, 74, 15]
• There are a number of >>> print(len(nums))
functions built into Python 6
that take lists as >>> print(max(nums))
parameters 74
>>> print(min(nums))
3
• Remember the loops we >>> print(sum(nums))
built? These are much 154
simpler. >>> print(sum(nums)/len(nums))
25.6
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 stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008

words = line.split()
email = words[1]
print pieces[1]
The Double Split Pattern

From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008

words = line.split()
email = words[1] stephen.marquard@uct.ac.za
print pieces[1]
The Double Split Pattern

From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008

words = line.split()
email = words[1] stephen.marquard@uct.ac.za
pieces = email.split('@') ['stephen.marquard', 'uct.ac.za']
print pieces[1]

You might also like