SlideShare a Scribd company logo
1 of 22
PPT ON Lists Python
Programming
• Algorithm
- A set of rules or steps used to solve a problem
• Data Structure
- A particular way of organizing data in a computer
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
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
>>> print([1, 24, 76])
[1, 24, 76]
>>> print(['red', 'yellow',
'blue'])
['red', 'yellow', 'blue']
>>> print(['red', 24, 98.6])
['red', 24, 98.6]
>>> 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 New Year:', friend)
print('Done!')
Happy New Year: Joseph
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
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)
banana
>>> 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
(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 New Year:', friend)
for i in range(len(friends)) :
friend = friends[i]
print('Happy New Year:', friend) Happy New Year: Joseph
Happy New Year: Glenn
Happy New Year: 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 existing
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']
>>>
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
>>>
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”
>>> 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.6
numlist = list()
while True :
inp = 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 = 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 and 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
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 Pythonlearn-08-Lists.pptx

Python programming lab3 250215
Python programming lab3 250215Python programming lab3 250215
Python programming lab3 250215profbnk
 
The Ring programming language version 1.8 book - Part 27 of 202
The Ring programming language version 1.8 book - Part 27 of 202The Ring programming language version 1.8 book - Part 27 of 202
The Ring programming language version 1.8 book - Part 27 of 202Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 29 of 210
The Ring programming language version 1.9 book - Part 29 of 210The Ring programming language version 1.9 book - Part 29 of 210
The Ring programming language version 1.9 book - Part 29 of 210Mahmoud Samir Fayed
 
python_avw - Unit-03.pdf
python_avw - Unit-03.pdfpython_avw - Unit-03.pdf
python_avw - Unit-03.pdfAshaWankar1
 
Python Programming: Lists, Modules, Exceptions
Python Programming: Lists, Modules, ExceptionsPython Programming: Lists, Modules, Exceptions
Python Programming: Lists, Modules, ExceptionsSreedhar Chowdam
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionarySoba Arjun
 
Pa1 session 3_slides
Pa1 session 3_slidesPa1 session 3_slides
Pa1 session 3_slidesaiclub_slides
 
List_tuple_dictionary.pptx
List_tuple_dictionary.pptxList_tuple_dictionary.pptx
List_tuple_dictionary.pptxChandanVatsa2
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in pythonhydpy
 
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲Mohammad Reza Kamalifard
 
UNIT III PYTHON.pptx python basic ppt ppt
UNIT III PYTHON.pptx python basic ppt pptUNIT III PYTHON.pptx python basic ppt ppt
UNIT III PYTHON.pptx python basic ppt pptSuganthiDPSGRKCW
 

Similar to Pythonlearn-08-Lists.pptx (20)

Python programming lab3 250215
Python programming lab3 250215Python programming lab3 250215
Python programming lab3 250215
 
The Ring programming language version 1.8 book - Part 27 of 202
The Ring programming language version 1.8 book - Part 27 of 202The Ring programming language version 1.8 book - Part 27 of 202
The Ring programming language version 1.8 book - Part 27 of 202
 
The Ring programming language version 1.9 book - Part 29 of 210
The Ring programming language version 1.9 book - Part 29 of 210The Ring programming language version 1.9 book - Part 29 of 210
The Ring programming language version 1.9 book - Part 29 of 210
 
XI_CS_Notes for strings.docx
XI_CS_Notes for strings.docxXI_CS_Notes for strings.docx
XI_CS_Notes for strings.docx
 
Python Workshop
Python  Workshop Python  Workshop
Python Workshop
 
Python lists &amp; sets
Python lists &amp; setsPython lists &amp; sets
Python lists &amp; sets
 
Unit - 4.ppt
Unit - 4.pptUnit - 4.ppt
Unit - 4.ppt
 
Python for Beginners(v3)
Python for Beginners(v3)Python for Beginners(v3)
Python for Beginners(v3)
 
Python-List.pptx
Python-List.pptxPython-List.pptx
Python-List.pptx
 
6. list
6. list6. list
6. list
 
python_avw - Unit-03.pdf
python_avw - Unit-03.pdfpython_avw - Unit-03.pdf
python_avw - Unit-03.pdf
 
Python Programming: Lists, Modules, Exceptions
Python Programming: Lists, Modules, ExceptionsPython Programming: Lists, Modules, Exceptions
Python Programming: Lists, Modules, Exceptions
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, Dictionary
 
Pa1 session 3_slides
Pa1 session 3_slidesPa1 session 3_slides
Pa1 session 3_slides
 
List_tuple_dictionary.pptx
List_tuple_dictionary.pptxList_tuple_dictionary.pptx
List_tuple_dictionary.pptx
 
1 pythonbasic
1 pythonbasic1 pythonbasic
1 pythonbasic
 
GE3151_PSPP_UNIT_4_Notes
GE3151_PSPP_UNIT_4_NotesGE3151_PSPP_UNIT_4_Notes
GE3151_PSPP_UNIT_4_Notes
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
 
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 
UNIT III PYTHON.pptx python basic ppt ppt
UNIT III PYTHON.pptx python basic ppt pptUNIT III PYTHON.pptx python basic ppt ppt
UNIT III PYTHON.pptx python basic ppt ppt
 

Recently uploaded

Top profile Call Girls In Rampur [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Rampur [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Rampur [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Rampur [ 7014168258 ] Call Me For Genuine Models We...nirzagarg
 
Dating Call Girls inTiruvallur { 9332606886 } VVIP NISHA Call Girls Near 5 St...
Dating Call Girls inTiruvallur { 9332606886 } VVIP NISHA Call Girls Near 5 St...Dating Call Girls inTiruvallur { 9332606886 } VVIP NISHA Call Girls Near 5 St...
Dating Call Girls inTiruvallur { 9332606886 } VVIP NISHA Call Girls Near 5 St...ruksarkahn825
 
obat aborsi pacitan wa 081336238223 jual obat aborsi cytotec asli di pacitan0...
obat aborsi pacitan wa 081336238223 jual obat aborsi cytotec asli di pacitan0...obat aborsi pacitan wa 081336238223 jual obat aborsi cytotec asli di pacitan0...
obat aborsi pacitan wa 081336238223 jual obat aborsi cytotec asli di pacitan0...yulianti213969
 
Call Girl In Gwalior Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top Class C...
Call Girl In Gwalior Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top Class C...Call Girl In Gwalior Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top Class C...
Call Girl In Gwalior Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top Class C...vershagrag
 
207095666-Book-Review-on-Ignited-Minds-Final.pptx
207095666-Book-Review-on-Ignited-Minds-Final.pptx207095666-Book-Review-on-Ignited-Minds-Final.pptx
207095666-Book-Review-on-Ignited-Minds-Final.pptxpawangadkhe786
 
Jual obat aborsi Jakarta ( 085657271886 )Cytote pil telat bulan penggugur kan...
Jual obat aborsi Jakarta ( 085657271886 )Cytote pil telat bulan penggugur kan...Jual obat aborsi Jakarta ( 085657271886 )Cytote pil telat bulan penggugur kan...
Jual obat aborsi Jakarta ( 085657271886 )Cytote pil telat bulan penggugur kan...ZurliaSoop
 
Athwa gate \ Call Girls Service Ahmedabad - 450+ Call Girl Cash Payment 80057...
Athwa gate \ Call Girls Service Ahmedabad - 450+ Call Girl Cash Payment 80057...Athwa gate \ Call Girls Service Ahmedabad - 450+ Call Girl Cash Payment 80057...
Athwa gate \ Call Girls Service Ahmedabad - 450+ Call Girl Cash Payment 80057...gragfaguni
 
Low Cost Coimbatore Call Girls Service 👉📞 6378878445 👉📞 Just📲 Call Ruhi Call ...
Low Cost Coimbatore Call Girls Service 👉📞 6378878445 👉📞 Just📲 Call Ruhi Call ...Low Cost Coimbatore Call Girls Service 👉📞 6378878445 👉📞 Just📲 Call Ruhi Call ...
Low Cost Coimbatore Call Girls Service 👉📞 6378878445 👉📞 Just📲 Call Ruhi Call ...vershagrag
 
B.tech civil major project by Deepak Kumar
B.tech civil major project by Deepak KumarB.tech civil major project by Deepak Kumar
B.tech civil major project by Deepak KumarDeepak15CivilEngg
 
UIowa Application Instructions - 2024 Update
UIowa Application Instructions - 2024 UpdateUIowa Application Instructions - 2024 Update
UIowa Application Instructions - 2024 UpdateUniversity of Iowa
 
9352852248 Call Girls Sanand Escort Service Available 24×7 In Sanand
9352852248 Call Girls  Sanand Escort Service Available 24×7 In Sanand9352852248 Call Girls  Sanand Escort Service Available 24×7 In Sanand
9352852248 Call Girls Sanand Escort Service Available 24×7 In Sanandgargpaaro
 
K Venkat Naveen Kumar | GCP Data Engineer | CV
K Venkat Naveen Kumar | GCP Data Engineer | CVK Venkat Naveen Kumar | GCP Data Engineer | CV
K Venkat Naveen Kumar | GCP Data Engineer | CVK VENKAT NAVEEN KUMAR
 
一比一定(购)中央昆士兰大学毕业证(CQU毕业证)成绩单学位证
一比一定(购)中央昆士兰大学毕业证(CQU毕业证)成绩单学位证一比一定(购)中央昆士兰大学毕业证(CQU毕业证)成绩单学位证
一比一定(购)中央昆士兰大学毕业证(CQU毕业证)成绩单学位证eqaqen
 
Simple, 3-Step Strategy to Improve Your Executive Presence (Even if You Don't...
Simple, 3-Step Strategy to Improve Your Executive Presence (Even if You Don't...Simple, 3-Step Strategy to Improve Your Executive Presence (Even if You Don't...
Simple, 3-Step Strategy to Improve Your Executive Presence (Even if You Don't...Angela Justice, PhD
 
Top profile Call Girls In Hubli [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hubli [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Hubli [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hubli [ 7014168258 ] Call Me For Genuine Models We ...gajnagarg
 
DMER-AYUSH-MIMS-Staff-Nurse-_Selection-List-04-05-2024.pdf
DMER-AYUSH-MIMS-Staff-Nurse-_Selection-List-04-05-2024.pdfDMER-AYUSH-MIMS-Staff-Nurse-_Selection-List-04-05-2024.pdf
DMER-AYUSH-MIMS-Staff-Nurse-_Selection-List-04-05-2024.pdfReemaKhan31
 
Maninagar ^ best call girls in Ahmedabad ₹7.5k Pick Up & Drop With Cash Payme...
Maninagar ^ best call girls in Ahmedabad ₹7.5k Pick Up & Drop With Cash Payme...Maninagar ^ best call girls in Ahmedabad ₹7.5k Pick Up & Drop With Cash Payme...
Maninagar ^ best call girls in Ahmedabad ₹7.5k Pick Up & Drop With Cash Payme...gragchanchal546
 
Specialize in a MSc within Biomanufacturing, and work part-time as Process En...
Specialize in a MSc within Biomanufacturing, and work part-time as Process En...Specialize in a MSc within Biomanufacturing, and work part-time as Process En...
Specialize in a MSc within Biomanufacturing, and work part-time as Process En...Juli Boned
 
Mysore Escorts Service Girl ^ 9332606886, WhatsApp Anytime Mysore
Mysore Escorts Service Girl ^ 9332606886, WhatsApp Anytime MysoreMysore Escorts Service Girl ^ 9332606886, WhatsApp Anytime Mysore
Mysore Escorts Service Girl ^ 9332606886, WhatsApp Anytime Mysoremeghakumariji156
 
Top profile Call Girls In Etawah [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Etawah [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Etawah [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Etawah [ 7014168258 ] Call Me For Genuine Models We...nirzagarg
 

Recently uploaded (20)

Top profile Call Girls In Rampur [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Rampur [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Rampur [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Rampur [ 7014168258 ] Call Me For Genuine Models We...
 
Dating Call Girls inTiruvallur { 9332606886 } VVIP NISHA Call Girls Near 5 St...
Dating Call Girls inTiruvallur { 9332606886 } VVIP NISHA Call Girls Near 5 St...Dating Call Girls inTiruvallur { 9332606886 } VVIP NISHA Call Girls Near 5 St...
Dating Call Girls inTiruvallur { 9332606886 } VVIP NISHA Call Girls Near 5 St...
 
obat aborsi pacitan wa 081336238223 jual obat aborsi cytotec asli di pacitan0...
obat aborsi pacitan wa 081336238223 jual obat aborsi cytotec asli di pacitan0...obat aborsi pacitan wa 081336238223 jual obat aborsi cytotec asli di pacitan0...
obat aborsi pacitan wa 081336238223 jual obat aborsi cytotec asli di pacitan0...
 
Call Girl In Gwalior Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top Class C...
Call Girl In Gwalior Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top Class C...Call Girl In Gwalior Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top Class C...
Call Girl In Gwalior Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top Class C...
 
207095666-Book-Review-on-Ignited-Minds-Final.pptx
207095666-Book-Review-on-Ignited-Minds-Final.pptx207095666-Book-Review-on-Ignited-Minds-Final.pptx
207095666-Book-Review-on-Ignited-Minds-Final.pptx
 
Jual obat aborsi Jakarta ( 085657271886 )Cytote pil telat bulan penggugur kan...
Jual obat aborsi Jakarta ( 085657271886 )Cytote pil telat bulan penggugur kan...Jual obat aborsi Jakarta ( 085657271886 )Cytote pil telat bulan penggugur kan...
Jual obat aborsi Jakarta ( 085657271886 )Cytote pil telat bulan penggugur kan...
 
Athwa gate \ Call Girls Service Ahmedabad - 450+ Call Girl Cash Payment 80057...
Athwa gate \ Call Girls Service Ahmedabad - 450+ Call Girl Cash Payment 80057...Athwa gate \ Call Girls Service Ahmedabad - 450+ Call Girl Cash Payment 80057...
Athwa gate \ Call Girls Service Ahmedabad - 450+ Call Girl Cash Payment 80057...
 
Low Cost Coimbatore Call Girls Service 👉📞 6378878445 👉📞 Just📲 Call Ruhi Call ...
Low Cost Coimbatore Call Girls Service 👉📞 6378878445 👉📞 Just📲 Call Ruhi Call ...Low Cost Coimbatore Call Girls Service 👉📞 6378878445 👉📞 Just📲 Call Ruhi Call ...
Low Cost Coimbatore Call Girls Service 👉📞 6378878445 👉📞 Just📲 Call Ruhi Call ...
 
B.tech civil major project by Deepak Kumar
B.tech civil major project by Deepak KumarB.tech civil major project by Deepak Kumar
B.tech civil major project by Deepak Kumar
 
UIowa Application Instructions - 2024 Update
UIowa Application Instructions - 2024 UpdateUIowa Application Instructions - 2024 Update
UIowa Application Instructions - 2024 Update
 
9352852248 Call Girls Sanand Escort Service Available 24×7 In Sanand
9352852248 Call Girls  Sanand Escort Service Available 24×7 In Sanand9352852248 Call Girls  Sanand Escort Service Available 24×7 In Sanand
9352852248 Call Girls Sanand Escort Service Available 24×7 In Sanand
 
K Venkat Naveen Kumar | GCP Data Engineer | CV
K Venkat Naveen Kumar | GCP Data Engineer | CVK Venkat Naveen Kumar | GCP Data Engineer | CV
K Venkat Naveen Kumar | GCP Data Engineer | CV
 
一比一定(购)中央昆士兰大学毕业证(CQU毕业证)成绩单学位证
一比一定(购)中央昆士兰大学毕业证(CQU毕业证)成绩单学位证一比一定(购)中央昆士兰大学毕业证(CQU毕业证)成绩单学位证
一比一定(购)中央昆士兰大学毕业证(CQU毕业证)成绩单学位证
 
Simple, 3-Step Strategy to Improve Your Executive Presence (Even if You Don't...
Simple, 3-Step Strategy to Improve Your Executive Presence (Even if You Don't...Simple, 3-Step Strategy to Improve Your Executive Presence (Even if You Don't...
Simple, 3-Step Strategy to Improve Your Executive Presence (Even if You Don't...
 
Top profile Call Girls In Hubli [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hubli [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Hubli [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hubli [ 7014168258 ] Call Me For Genuine Models We ...
 
DMER-AYUSH-MIMS-Staff-Nurse-_Selection-List-04-05-2024.pdf
DMER-AYUSH-MIMS-Staff-Nurse-_Selection-List-04-05-2024.pdfDMER-AYUSH-MIMS-Staff-Nurse-_Selection-List-04-05-2024.pdf
DMER-AYUSH-MIMS-Staff-Nurse-_Selection-List-04-05-2024.pdf
 
Maninagar ^ best call girls in Ahmedabad ₹7.5k Pick Up & Drop With Cash Payme...
Maninagar ^ best call girls in Ahmedabad ₹7.5k Pick Up & Drop With Cash Payme...Maninagar ^ best call girls in Ahmedabad ₹7.5k Pick Up & Drop With Cash Payme...
Maninagar ^ best call girls in Ahmedabad ₹7.5k Pick Up & Drop With Cash Payme...
 
Specialize in a MSc within Biomanufacturing, and work part-time as Process En...
Specialize in a MSc within Biomanufacturing, and work part-time as Process En...Specialize in a MSc within Biomanufacturing, and work part-time as Process En...
Specialize in a MSc within Biomanufacturing, and work part-time as Process En...
 
Mysore Escorts Service Girl ^ 9332606886, WhatsApp Anytime Mysore
Mysore Escorts Service Girl ^ 9332606886, WhatsApp Anytime MysoreMysore Escorts Service Girl ^ 9332606886, WhatsApp Anytime Mysore
Mysore Escorts Service Girl ^ 9332606886, WhatsApp Anytime Mysore
 
Top profile Call Girls In Etawah [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Etawah [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Etawah [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Etawah [ 7014168258 ] Call Me For Genuine Models We...
 

Pythonlearn-08-Lists.pptx

  • 1. PPT ON Lists Python
  • 2. Programming • Algorithm - A set of rules or steps used to solve a problem • Data Structure - A particular way of organizing data in a computer
  • 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. 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 >>> print([1, 24, 76]) [1, 24, 76] >>> print(['red', 'yellow', 'blue']) ['red', 'yellow', 'blue'] >>> print(['red', 24, 98.6]) ['red', 24, 98.6] >>> print([ 1, [5, 6], 7]) [1, [5, 6], 7] >>> print([]) []
  • 5. We Already Use Lists! for i in [5, 4, 3, 2, 1] : print(i) print('Blastoff!') 5 4 3 2 1 Blastoff!
  • 6. Lists and Definite Loops - Best Pals friends = ['Joseph', 'Glenn', 'Sally'] for friend in friends : print('Happy New Year:', friend) print('Done!') Happy New Year: Joseph Happy New Year: Glenn Happy New Year: Sally Done! z = ['Joseph', 'Glenn', 'Sally'] for x in z: print('Happy New Year:', x) print('Done!')
  • 7. 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
  • 8. 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) banana >>> lotto = [2, 14, 26, 41, 63] >>> print(lotto) [2, 14, 26, 41, 63] >>> lotto[2] = 28 >>> print(lotto) [2, 14, 28, 41, 63]
  • 9. 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...) >>> greet = 'Hello Bob' >>> print(len(greet)) 9 >>> x = [ 1, 2, 'joe', 99] >>> print(len(x)) 4 >>>
  • 10. 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] >>>
  • 11. A Tale of Two Loops... friends = ['Joseph', 'Glenn', 'Sally'] for friend in friends : print('Happy New Year:', friend) for i in range(len(friends)) : friend = friends[i] print('Happy New Year:', friend) Happy New Year: Joseph Happy New Year: Glenn Happy New Year: Sally >>> friends = ['Joseph', 'Glenn', 'Sally'] >>> print(len(friends)) 3 >>> print(range(len(friends))) [0, 1, 2] >>>
  • 12. Concatenating Lists Using + We can create a new list by adding two existing 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]
  • 13. 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”
  • 14. List Methods >>> x = list() >>> type(x) <type 'list'> >>> dir(x) ['append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] >>>
  • 15. 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']
  • 16. 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 >>>
  • 17. 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” >>> friends = [ 'Joseph', 'Glenn', 'Sally' ] >>> friends.sort() >>> print(friends) ['Glenn', 'Joseph', 'Sally'] >>> print(friends[1]) Joseph >>>
  • 18. 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.6
  • 19. numlist = list() while True : inp = 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 = 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
  • 20. 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 and produces a list of strings. We think of these as words. We can access a particular word or loop through all the words.
  • 21. >>> 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
  • 22. 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