Introduction to the Python Language Part 1. Python data types.
An Introduction To Python - Lists, Part 1
-
Upload
blue-elephant-consulting -
Category
Education
-
view
90 -
download
5
Transcript of An Introduction To Python - Lists, Part 1
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
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
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
What Does Your List Look Like Inside Of The Computer?
COP 2271c
Introduction to Computation and Programming
3
0
1
2
PythonStartsCountingAt 0
Each item in a listhas an indexnumber associatedwith it…
prerequisites = [“COP 2271c”, “Introduction to Computation and Programming”, 3]
The computer storesyour list data in what iscalled an “array”.
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
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
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
Programming Challenge
• How many credits need to be taken to satisfy Poly prereqs?
Image Credit: www.clker.com4
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 providesaccess to the last element in the list:
last = values[-1]print("The last element in the list is", last)
Image Credit: lessonpix.com
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
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
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
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 , 2015X
Image Credit: blog.poolproducts.com6
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
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
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
What We Covered Today
1. Lists
2. Multidimensional lists
Image Credit: http://www.tswdj.com/blog/2011/05/17/the-grooms-checklist/