Not logged in
Talk
Contributions
Create account
Log in
Navigation
Main page
About
People
Publications
Teaching
Resources
Research Blog
Wiki Functions
Recent changes
Help
Licensing
Page
Discussion
Edit
View history
Editing
DS4UX (Spring 2016)/Day 3 lecture
(section)
From CommunityData
Jump to:
navigation
,
search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Review of some important Week 2 concepts == ===Lists=== * Use lists to store data where order matters. * Lists are indexed starting with 0. ====List initialization==== >>> my_list = [] >>> my_list [] >>> your_list = ["a", "b", "c", 1, 2, 3] >>> your_list ['a', 'b', 'c', 1, 2, 3] ====Access and adding elements to a list==== >>> len(my_list) 0 >>> my_list[0] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list index out of range >>> my_list.append("Alice") >>> my_list ['Alice'] >>> len(my_list) 1 >>> my_list[0] 'Alice' >>> my_list.insert(0, "Amy") >>> my_list ['Amy', 'Alice'] >>> my_list = ['Amy', 'Alice'] >>> 'Amy' in my_list True >>> 'Bob' in my_list False ====Changing elements in a list==== >>> your_list = [] >>> your_list.append("apples") >>> your_list[0] 'apples' >>> your_list[0] = "bananas" >>> your_list ['bananas'] ====Slicing lists==== >>> her_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] >>> her_list[0] 'a' >>> her_list[0:3] ['a', 'b', 'c'] >>> her_list[:3] ['a', 'b', 'c'] >>> her_list[-1] 'h' >>> her_list[5:] ['f', 'g', 'h'] >>> her_list[:] ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] ==== sorting lists ==== Use <code>.sort()</code> to sort a list: >>> names = ["Eliza", "Joe", "Henry", "Harriet", "Wanda", "Pat"] >>> names.sort() >>> names ['Eliza', 'Harriet', 'Henry', 'Joe', 'Pat', 'Wanda'] >>> names.sort(reverse=True) ['Wanda', 'Pat', 'Joe', 'Henry', 'Harriet', 'Eliza'] ==== Getting the maximum and minimum values from a list ==== >>> numbers = [0, 3, 10, -1] >>> max(numbers) 10 >>> min(numbers) -1
Summary:
Please note that all contributions to CommunityData are considered to be released under the Attribution-Share Alike 3.0 Unported (see
CommunityData:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
To protect the wiki against automated edit spam, we kindly ask you to solve the following CAPTCHA:
Cancel
Editing help
(opens in new window)
Tools
What links here
Related changes
Special pages
Page information