This presentation is a part of the COP2271C college level course taught at the Florida Polytechnic University located in Lakeland Florida. The purpose of this course is to introduce Freshmen students to both the process of software development and to the Python language.
The course is one semester in length and meets for 2 hours twice a week. The Instructor is Dr. Jim Anderson.
A video of Dr. Anderson using these slides is available on YouTube at:
https://www.youtube.com/watch?v=AOchqjVB_1o
http://youtu.be/4EaTej-aH0M
1. An Introduction To Software
Development Using Python
Spring Semester, 2015
Class #9:
Lists, Part 1
2. Python Is All About Lists
• What lists do you use in your life today?
• Polytech Florida Common Prerequisites list of
classes: Course ID, Course Name, Course Credits
CHM 2045,Chemistry 1,3
CHM 2045L,Chemistry 1 Laboratory,1
COP 2271c,Introduction to Computation and Programming,3
EEL 3111c,Circuits 1,4
EEL 3112c,Circuits 2,3
MAC 2311,Analytic Geometry and Calculus 1,4
MAC 2312,Analytic Geometry and Calculus 2,4
MAC 2313,Analytic Geometry and Calculus 3,4
MAP 2302,Differential Equations,3
PHY 2048,Physics 1,3
PHY 2048L,Physics 1 Laboratory,1
PHY 2049,Physics 2,3
PHY 2049L,Physics 2 Laboratory,1
Image Credit: Clipart Panda
3. 4 Steps To Creating A Python List
1. Convert each of the names into strings by surrounding the
data with quotes.
2. Separate each of the list items from the next with a comma.
3. Surround the list of items with opening and closing square
brackets.
4. Assign the list to an identifier (movies in the preceding code)
using the assignment operator (=).
“COP 2271c” “Introduction to Computation and Programming” 3
“COP 2271c”, “Introduction to Computation and Programming”, 3
[“COP 2271c”, “Introduction to Computation and Programming”, 3]
prerequisites = [“COP 2271c”, “Introduction to Computation and Programming”, 3]
COP 2271c Introduction to Computation and Programming 3
Image Credit: Clipart Panda1
4. Python Is Different From Other
Programing Languages!
• Other languages (C, C++, Java, etc.) insist that every identifier used in code
has type information declared for it. Not so with Python: identifiers are
simply names that refer to a data object of some type.
char example1;
int example2 = 5;
void example3(void)
• One way to think of Python’s list is as a high-level collection.
• The type of the data items is not important to the list. All Python needs to
know is that you need a list, you’ve given it a name, and the list has some
data items in it.
Image Credit: Clipart Panda
5. What Does Your List Look Like
Inside Of The Computer?
COP 2271c
Introduction to Computation and Programming
3
0
1
2
Python
Starts
Counting
At 0
Each item in a list
has an index
number associated
with it…
prerequisites = [“COP 2271c”, “Introduction to Computation and Programming”, 3]
The computer stores
your list data in what is
called an “array”.
6. How To Access A List
• A list is a sequence of elements, each of which has an
integer position or index.
• To access a list element, you specify which index you
want to use.
• That is done with the subscript operator ([] ) in the
same way that you access individual characters in a
string.
• For example:
print(values[5]) # Prints the element at index 5
Image Credit: imgkid.com
7. Access Your List Data Using The
Square Bracket Notation
print (prerequisites[0]) COP 2271c
print (prerequisites[1]) Introduction to Computation and Programming
print (prerequisites[2]) 3
Image Credit: Clipart Panda2
8. You Can Create
Multidimensional Lists
• Lists can hold data of mixed type.
• But it gets even better than that: lists can hold
collections of anything, including other lists.
• Simply embed the inner list within the
enclosing list as needed.
multiDim = [[123],[456],[789]] =
1 2 3
4 5 6
7 8 9
Image Credit: www.rafainspirationhomedecor.com3
9. Programming Challenge
• How many credits need to be taken to satisfy
Poly prereqs?
Image Credit: www.clker.com4
10. Did You Know?:
Accessing A List Backwards
• Python, unlike many other languages, also
allows you to use negative subscripts when
accessing an element of a list.
• The negative subscripts provide access to the
list elements in reverse order.
• For example, a subscript of –1 provides
access to the last element in the list:
last = values[-1]
print("The last element in the list is", last)
Image Credit: lessonpix.com
11. What Is The Difference Between A
List And A String?
• Both lists and strings are sequences, and the [] operator can
be used to access an element in any sequence.
• There are two differences between lists and strings.
– Lists can hold values of any type, whereas strings are sequences of
characters.
– Moreover, strings are immutable—you cannot change the characters
in the sequence. But lists are mutable. You can replace one list
element with another, like this:
values[5] = 87
Image Credit: www.dreamstime.com
13. Lists Are Much More Than Just
Simple Arrays
• Lists in Python are full-blown Python
collection objects.
• This means that lists come with ready-to-use
functionality in the form of list methods.
BIF
len
Methods
1. append
2. pop
3. remove
4. insert
5. extend
Image Credit: besttoddlertoys.eu
14. Playing With Lists: Append
• Add teacher: Dr. Jim Anderson
• Add year: 2015
Note: Python lists can contain data of mixed types. You can mix strings with
numbers within the same Python list. You can mix more than just strings and
numbers -- you can store data of any type in a single list.
prerequisites = [“COP 2271c”, “Introduction to Computation and Programming”, 3]
Image Credit: ClipArt Best5
15. Playing With Lists: Pop
• Allows you to remove the item that is at the
end of a list
prereqs[course].pop()
CHM 2045 , Chemistry 1 , 3 , Dr. Anderson , 2015
X
Image Credit: blog.poolproducts.com6
16. Playing With Lists: Remove
• Allows you to specify which list item you want
to remove no matter where in the list it is
located
prereqs[course].remove("Dr. Anderson")
CHM 2045 , Chemistry 1 , 3 , Dr. AndersonX
Image Credit: www.pinterest.com7
17. Playing With Lists: Insert
• Allows you to add an item to a list in a
specified location on the list
prereqs[course].insert(2,"Really Hard")
CHM 2045 , Chemistry 1 , Really Hard , 3
Image Credit: ekskavatör8
18. Playing With Lists: Extend
• Allows multiple list items to be added to an
existing list
prereqs[course].extend(["Dr. Anderson", "2015"])
CHM 2045 , Chemistry 1 , Really Hard , 3 , Dr. Anderson , 2015
Image Credit: www.clipartpanda.com9
19. What’s In Your Python Toolbox?
print() math strings I/O IF/Else elif While For
Lists
20. What We Covered Today
1. Lists
2. Multidimensional lists
Image Credit: http://www.tswdj.com/blog/2011/05/17/the-grooms-checklist/
21. What We’ll Be Covering Next Time
1. Lists, Part 2
Image Credit: http://merchantblog.thefind.com/2011/01/merchant-newsletter/resolve-to-take-advantage-of-these-5-e-commerce-trends/attachment/crystal-ball-fullsize/
Editor's Notes
New name for the class
I know what this means
Technical professionals are who get hired
This means much more than just having a narrow vertical knowledge of some subject area.
It means that you know how to produce an outcome that I value.
I’m willing to pay you to do that.