SlideShare a Scribd company logo
1 of 28
Download to read offline
Python Lists
Chapter 8
Python for Informatics: Exploring Information
www.pythonlearn.com
Unless otherwise noted, the content of this course material is licensed under a Creative
Commons Attribution 3.0 License.
http://creativecommons.org/licenses/by/3.0/.
Copyright 2010- Charles Severance
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' ]
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 over written
$ python
Python 2.5.2 (r252:60911, Feb 22 2008, 07:57:53)
[GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin
>>> x = 2
>>> x = 4
>>> print x
4
List Constants
• List constants are surrounded by
square brakets 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
>>> print [1, 24, 76]
[1, 24, 76]
>>> print ['red', 'yellow', 'blue']
['red', 'yellow', 'blue']
>>> print ['red', 24, 98.6]
['red', 24, 98.599999999999994]
>>> print [ 1, [5, 6], 7]
[1, [5, 6], 7]
>>> print []
[]
We already use lists!
for i in [5, 4, 3, 2, 1] :
print i
print 'Blastoff!'
5
4
3
2
1
Blastoff!
Lists and definite loops - best pals
friends = ['Joseph', 'Glenn', 'Sally']
for friend in friends :
print 'Happy NewYear:', friend
print 'Done!'
Happy NewYear: Joseph
Happy NewYear: Glenn
Happy NewYear: Sally
Done!
Looking Inside Lists
• Just like strings, we can get at any single element in a list using an index
specified in square brackets
0
Joseph
>>> friends = [ 'Joseph', 'Glenn', 'Sally' ]
>>> print friends[1]
Glenn
>>>
1
Glenn
2
Sally
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
>>> fruit = 'Banana'
>>> fruit[0] = 'b'
Traceback
TypeError: 'str' object does not
support item assignment
>>> x = fruit.lower()
>>> print x
bannna
>>> lotto = [2, 14, 26, 41, 63]
>>> print lotto
[2, 14, 26, 41, 63]
>>> lotto[2] = 28
>>> print lotto
[2, 14, 28, 41, 63]
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
(i.e. such as a string...)
>>> greet = 'Hello Bob'
>>> print len(greet)
9
>>> x = [ 1, 2, 'joe', 99]
>>> print len(x)
4
>>>
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 an integer iterator
>>> print range(4)
[0, 1, 2, 3]
>>> friends = ['Joseph', 'Glenn', 'Sally']
>>> print len(friends)
3
>>> print range(len(friends))
[0, 1, 2]
>>>
A tale of two loops...
friends = ['Joseph', 'Glenn', 'Sally']
for friend in friends :
print 'Happy NewYear:', friend
for i in range(len(friends)) :
friend = friends[i]
print 'Happy NewYear:', friend
Happy NewYear: Joseph
Happy NewYear: Glenn
Happy NewYear: Sally
>>> friends = ['Joseph', 'Glenn', 'Sally']
>>> print len(friends)
3
>>> print range(len(friends))
[0, 1, 2]
>>>
Concatenating lists using +
• We can create a new list by adding
two exsiting lists together
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> c = a + b
>>> print c
[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]
>>> t[:4]
[9, 41, 12, 3]
>>> t[3:]
[3, 74, 15]
>>> t[:]
[9, 41, 12, 3, 74, 15]
Remember: Just like in
strings, the second number
is "up to but not
including"
List Methods
>>> x = list()
>>> type(x)
<type 'list'>
>>> dir(x)
['append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>>
http://docs.python.org/tutorial/datastructures.html
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
>>> stuff = list()
>>> stuff.append('book')
>>> stuff.append(99)
>>> print stuff
['book', 99]
>>> stuff.append('cookie')
>>> print stuff
['book', 99, 'cookie']
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
>>> some = [1, 9, 21, 10, 16]
>>> 9 in some
True
>>> 15 in some
False
>>> 20 not in some
True
>>>
A List is an Ordered Sequence
• 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"
>>> friends = [ 'Joseph', 'Glenn', 'Sally' ]
>>> friends.sort()
>>> print friends
['Glenn', 'Joseph', 'Sally']
>>> print friends[1]
Joseph
>>>
Built in Functions and Lists
• There are a number of
functions built into Python
that take lists as
parameters
• Remember the loops we
built? These are much
simpler
>>> nums = [3, 41, 12, 9, 74, 15]
>>> print len(nums)
6
>>> print max(nums)
74
>>> print min(nums)
3
>>> print sum(nums)
154
>>> print sum(nums)/len(nums)
25
http://docs.python.org/lib/built-in-funcs.html
Averaging
with a list
numlist = list()
while True :
inp = raw_input('Enter a number: ')
if inp == 'done' : break
value = float(inp)
numlist.append(value)
average = sum(numlist) / len(numlist)
print 'Average:', average
total = 0
count = 0
while True :
inp = raw_input('Enter a number: ')
if inp == 'done' : break
value = float(inp)
total = total + value
count = count + 1
average = total / count
print 'Average:', average
Enter a number: 3
Enter a number: 9
Enter a number: 5
Enter a number: done
Average: 5.66666666667
Best Friends: Strings and Lists
>>> abc = 'With three words'
>>> stuff = abc.split()
>>> print stuff
['With', 'three', 'words']
>>> print len(stuff)
3
>>> print stuff[0]
With
>>> print stuff
['With', 'three', 'words']
>>> for w in stuff :
... print w
...
With
three
words
>>>
Split breaks a string into parts produces a list of strings. We think of these as
words. We can access a particular word or loop through all the words.
>>> line = 'A lot of spaces'
>>> etc = line.split()
>>> print etc
['A', 'lot', 'of', 'spaces']
>>>
>>> line = 'first;second;third'
>>> thing = line.split()
>>> print thing
['first;second;third']
>>> print len(thing)
1
>>> thing = line.split(';')
>>> print thing
['first', 'second', 'third']
>>> print len(thing)
3
>>>
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.
fhand = open('mbox-short.txt')
for line in fhand:
line = line.rstrip()
if not line.startswith('From ') : continue
words = line.split()
print words[2]
Sat
Fri
Fri
Fri
...
From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
>>> line = 'From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008'
>>> words = line.split()
>>> print words
['From', 'stephen.marquard@uct.ac.za', 'Sat', 'Jan', '5', '09:14:16', '2008']
>>>
From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
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
words = line.split()
email = words[1]
pieces = email.split('@')
print pieces[1]
stephen.marquard@uct.ac.za
['stephen.marquard', 'uct.ac.za']
'uct.ac.za'
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]
pieces = email.split('@')
print pieces[1]
stephen.marquard@uct.ac.za
['stephen.marquard', 'uct.ac.za']
'uct.ac.za'
stephen.marquard uct.ac.za
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]
pieces = email.split('@')
print pieces[1]
stephen.marquard@uct.ac.za
['stephen.marquard', 'uct.ac.za']
'uct.ac.za'
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]
pieces = email.split('@')
print pieces[1]
stephen.marquard@uct.ac.za
['stephen.marquard', 'uct.ac.za']
'uct.ac.za'
List 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

More Related Content

Similar to Python elements list you can study .pdf

python chapter 1
python chapter 1python chapter 1
python chapter 1Raghu nath
 
Python chapter 2
Python chapter 2Python chapter 2
Python chapter 2Raghu nath
 
Python Programming: Lists, Modules, Exceptions
Python Programming: Lists, Modules, ExceptionsPython Programming: Lists, Modules, Exceptions
Python Programming: Lists, Modules, ExceptionsSreedhar Chowdam
 
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲Mohammad Reza Kamalifard
 
Notes3
Notes3Notes3
Notes3hccit
 
Python programming lab3 250215
Python programming lab3 250215Python programming lab3 250215
Python programming lab3 250215profbnk
 
15CS664- Python Application Programming - Module 3
15CS664- Python Application Programming - Module 315CS664- Python Application Programming - Module 3
15CS664- Python Application Programming - Module 3Syed Mustafa
 
15CS664-Python Application Programming - Module 3 and 4
15CS664-Python Application Programming - Module 3 and 415CS664-Python Application Programming - Module 3 and 4
15CS664-Python Application Programming - Module 3 and 4Syed Mustafa
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionarySoba Arjun
 
‏‏chap6 list tuples.pptx
‏‏chap6 list tuples.pptx‏‏chap6 list tuples.pptx
‏‏chap6 list tuples.pptxRamiHarrathi1
 
Pa1 session 3_slides
Pa1 session 3_slidesPa1 session 3_slides
Pa1 session 3_slidesaiclub_slides
 
UNIT III_Python Programming_aditya COllege
UNIT III_Python Programming_aditya COllegeUNIT III_Python Programming_aditya COllege
UNIT III_Python Programming_aditya COllegeRamanamurthy Banda
 
UNIT III_Python Programming_aditya COllege
UNIT III_Python Programming_aditya COllegeUNIT III_Python Programming_aditya COllege
UNIT III_Python Programming_aditya COllegeRamanamurthy Banda
 
Module 2 - Lists, Tuples, Files.pptx
Module 2 - Lists, Tuples, Files.pptxModule 2 - Lists, Tuples, Files.pptx
Module 2 - Lists, Tuples, Files.pptxGaneshRaghu4
 
R_CheatSheet.pdf
R_CheatSheet.pdfR_CheatSheet.pdf
R_CheatSheet.pdfMariappanR3
 

Similar to Python elements list you can study .pdf (20)

Python for Beginners(v3)
Python for Beginners(v3)Python for Beginners(v3)
Python for Beginners(v3)
 
python chapter 1
python chapter 1python chapter 1
python chapter 1
 
Python chapter 2
Python chapter 2Python chapter 2
Python chapter 2
 
Python Programming: Lists, Modules, Exceptions
Python Programming: Lists, Modules, ExceptionsPython Programming: Lists, Modules, Exceptions
Python Programming: Lists, Modules, Exceptions
 
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 
Notes3
Notes3Notes3
Notes3
 
Python lists
Python listsPython lists
Python lists
 
Python programming lab3 250215
Python programming lab3 250215Python programming lab3 250215
Python programming lab3 250215
 
Python-List.pptx
Python-List.pptxPython-List.pptx
Python-List.pptx
 
15CS664- Python Application Programming - Module 3
15CS664- Python Application Programming - Module 315CS664- Python Application Programming - Module 3
15CS664- Python Application Programming - Module 3
 
15CS664-Python Application Programming - Module 3 and 4
15CS664-Python Application Programming - Module 3 and 415CS664-Python Application Programming - Module 3 and 4
15CS664-Python Application Programming - Module 3 and 4
 
Python lecture 04
Python lecture 04Python lecture 04
Python lecture 04
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, Dictionary
 
‏‏chap6 list tuples.pptx
‏‏chap6 list tuples.pptx‏‏chap6 list tuples.pptx
‏‏chap6 list tuples.pptx
 
Python Workshop
Python  Workshop Python  Workshop
Python Workshop
 
Pa1 session 3_slides
Pa1 session 3_slidesPa1 session 3_slides
Pa1 session 3_slides
 
UNIT III_Python Programming_aditya COllege
UNIT III_Python Programming_aditya COllegeUNIT III_Python Programming_aditya COllege
UNIT III_Python Programming_aditya COllege
 
UNIT III_Python Programming_aditya COllege
UNIT III_Python Programming_aditya COllegeUNIT III_Python Programming_aditya COllege
UNIT III_Python Programming_aditya COllege
 
Module 2 - Lists, Tuples, Files.pptx
Module 2 - Lists, Tuples, Files.pptxModule 2 - Lists, Tuples, Files.pptx
Module 2 - Lists, Tuples, Files.pptx
 
R_CheatSheet.pdf
R_CheatSheet.pdfR_CheatSheet.pdf
R_CheatSheet.pdf
 

Recently uploaded

Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 

Recently uploaded (20)

Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 

Python elements list you can study .pdf

  • 1. Python Lists Chapter 8 Python for Informatics: Exploring Information www.pythonlearn.com
  • 2. Unless otherwise noted, the content of this course material is licensed under a Creative Commons Attribution 3.0 License. http://creativecommons.org/licenses/by/3.0/. Copyright 2010- Charles Severance
  • 3. 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' ]
  • 4. 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 over written $ python Python 2.5.2 (r252:60911, Feb 22 2008, 07:57:53) [GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin >>> x = 2 >>> x = 4 >>> print x 4
  • 5. List Constants • List constants are surrounded by square brakets 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 >>> print [1, 24, 76] [1, 24, 76] >>> print ['red', 'yellow', 'blue'] ['red', 'yellow', 'blue'] >>> print ['red', 24, 98.6] ['red', 24, 98.599999999999994] >>> print [ 1, [5, 6], 7] [1, [5, 6], 7] >>> print [] []
  • 6. We already use lists! for i in [5, 4, 3, 2, 1] : print i print 'Blastoff!' 5 4 3 2 1 Blastoff!
  • 7. Lists and definite loops - best pals friends = ['Joseph', 'Glenn', 'Sally'] for friend in friends : print 'Happy NewYear:', friend print 'Done!' Happy NewYear: Joseph Happy NewYear: Glenn Happy NewYear: Sally Done!
  • 8. Looking Inside Lists • Just like strings, we can get at any single element in a list using an index specified in square brackets 0 Joseph >>> friends = [ 'Joseph', 'Glenn', 'Sally' ] >>> print friends[1] Glenn >>> 1 Glenn 2 Sally
  • 9. 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 >>> fruit = 'Banana' >>> fruit[0] = 'b' Traceback TypeError: 'str' object does not support item assignment >>> x = fruit.lower() >>> print x bannna >>> lotto = [2, 14, 26, 41, 63] >>> print lotto [2, 14, 26, 41, 63] >>> lotto[2] = 28 >>> print lotto [2, 14, 28, 41, 63]
  • 10. 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 (i.e. such as a string...) >>> greet = 'Hello Bob' >>> print len(greet) 9 >>> x = [ 1, 2, 'joe', 99] >>> print len(x) 4 >>>
  • 11. 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 an integer iterator >>> print range(4) [0, 1, 2, 3] >>> friends = ['Joseph', 'Glenn', 'Sally'] >>> print len(friends) 3 >>> print range(len(friends)) [0, 1, 2] >>>
  • 12. A tale of two loops... friends = ['Joseph', 'Glenn', 'Sally'] for friend in friends : print 'Happy NewYear:', friend for i in range(len(friends)) : friend = friends[i] print 'Happy NewYear:', friend Happy NewYear: Joseph Happy NewYear: Glenn Happy NewYear: Sally >>> friends = ['Joseph', 'Glenn', 'Sally'] >>> print len(friends) 3 >>> print range(len(friends)) [0, 1, 2] >>>
  • 13. Concatenating lists using + • We can create a new list by adding two exsiting lists together >>> a = [1, 2, 3] >>> b = [4, 5, 6] >>> c = a + b >>> print c [1, 2, 3, 4, 5, 6] >>> print a [1, 2, 3]
  • 14. Lists can be sliced using : >>> t = [9, 41, 12, 3, 74, 15] >>> t[1:3] [41,12] >>> t[:4] [9, 41, 12, 3] >>> t[3:] [3, 74, 15] >>> t[:] [9, 41, 12, 3, 74, 15] Remember: Just like in strings, the second number is "up to but not including"
  • 15. List Methods >>> x = list() >>> type(x) <type 'list'> >>> dir(x) ['append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] >>> http://docs.python.org/tutorial/datastructures.html
  • 16. 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 >>> stuff = list() >>> stuff.append('book') >>> stuff.append(99) >>> print stuff ['book', 99] >>> stuff.append('cookie') >>> print stuff ['book', 99, 'cookie']
  • 17. 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 >>> some = [1, 9, 21, 10, 16] >>> 9 in some True >>> 15 in some False >>> 20 not in some True >>>
  • 18. A List is an Ordered Sequence • 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" >>> friends = [ 'Joseph', 'Glenn', 'Sally' ] >>> friends.sort() >>> print friends ['Glenn', 'Joseph', 'Sally'] >>> print friends[1] Joseph >>>
  • 19. Built in Functions and Lists • There are a number of functions built into Python that take lists as parameters • Remember the loops we built? These are much simpler >>> nums = [3, 41, 12, 9, 74, 15] >>> print len(nums) 6 >>> print max(nums) 74 >>> print min(nums) 3 >>> print sum(nums) 154 >>> print sum(nums)/len(nums) 25 http://docs.python.org/lib/built-in-funcs.html
  • 20. Averaging with a list numlist = list() while True : inp = raw_input('Enter a number: ') if inp == 'done' : break value = float(inp) numlist.append(value) average = sum(numlist) / len(numlist) print 'Average:', average total = 0 count = 0 while True : inp = raw_input('Enter a number: ') if inp == 'done' : break value = float(inp) total = total + value count = count + 1 average = total / count print 'Average:', average Enter a number: 3 Enter a number: 9 Enter a number: 5 Enter a number: done Average: 5.66666666667
  • 21. Best Friends: Strings and Lists >>> abc = 'With three words' >>> stuff = abc.split() >>> print stuff ['With', 'three', 'words'] >>> print len(stuff) 3 >>> print stuff[0] With >>> print stuff ['With', 'three', 'words'] >>> for w in stuff : ... print w ... With three words >>> Split breaks a string into parts produces a list of strings. We think of these as words. We can access a particular word or loop through all the words.
  • 22. >>> line = 'A lot of spaces' >>> etc = line.split() >>> print etc ['A', 'lot', 'of', 'spaces'] >>> >>> line = 'first;second;third' >>> thing = line.split() >>> print thing ['first;second;third'] >>> print len(thing) 1 >>> thing = line.split(';') >>> print thing ['first', 'second', 'third'] >>> print len(thing) 3 >>> 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.
  • 23. fhand = open('mbox-short.txt') for line in fhand: line = line.rstrip() if not line.startswith('From ') : continue words = line.split() print words[2] Sat Fri Fri Fri ... From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008 >>> line = 'From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008' >>> words = line.split() >>> print words ['From', 'stephen.marquard@uct.ac.za', 'Sat', 'Jan', '5', '09:14:16', '2008'] >>>
  • 24. From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008 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 words = line.split() email = words[1] pieces = email.split('@') print pieces[1] stephen.marquard@uct.ac.za ['stephen.marquard', 'uct.ac.za'] 'uct.ac.za'
  • 25. 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] pieces = email.split('@') print pieces[1] stephen.marquard@uct.ac.za ['stephen.marquard', 'uct.ac.za'] 'uct.ac.za' stephen.marquard uct.ac.za
  • 26. 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] pieces = email.split('@') print pieces[1] stephen.marquard@uct.ac.za ['stephen.marquard', 'uct.ac.za'] 'uct.ac.za'
  • 27. 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] pieces = email.split('@') print pieces[1] stephen.marquard@uct.ac.za ['stephen.marquard', 'uct.ac.za'] 'uct.ac.za'
  • 28. List 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