From Think Python How to Think Like a Computer Scientist

Slides:



Advertisements
Similar presentations
Python for Informatics: Exploring Information
Advertisements

Python Mini-Course University of Oklahoma Department of Psychology
ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
Course A201: Introduction to Programming 10/28/2010.
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
F28PL1 Programming Languages Lecture 14: Standard ML 4.
CHAPTER 4 AND 5 Section06: Sequences. General Description "Normal" variables x = 19  The name "x" is associated with a single value Sequence variables:
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 15 Tuples 5/02/09 Python Mini-Course: Day 4 – Lesson 15 1.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Lists Introduction to Computing Science and Programming I.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
Lists in Python.
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
Python November 28, Unit 9+. Local and Global Variables There are two main types of variables in Python: local and global –The explanation of local and.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Built-in Data Structures in Python An Introduction.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
10. Python - Lists The list is a most versatile datatype available in Python, which can be written as a list of comma-separated values (items) between.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Lists CS303E: Elements of Computers and Programming.
Loops & List Intro2CS – week 3 1. Loops -- Motivation Sometimes we want to repeat a certain set of instructions more than once. The number of repetitions.
Tuples Chapter 10 Python for Informatics: Exploring Information
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
Sequences CMSC 120: Visualizing Information 2/26/08.
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
Python Programing: An Introduction to Computer Science
Python Files and Lists. Files  Chapter 9 actually introduces you to opening up files for reading  Chapter 14 has more on file I/O  Python can read.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
11 March 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
CMSC201 Computer Science I for Majors Lecture 08 – Lists
String and Lists Dr. José M. Reyes Álamo.
Data types: Complex types (List)
Python – May 18 Quiz Relatives of the list: Tuple Dictionary Set
Python - Lists.
CMSC201 Computer Science I for Majors Lecture 17 – Dictionaries
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
Containers and Lists CIS 40 – Introduction to Programming in Python
Python Lists Chapter 8 Python for Everybody
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
CISC101 Reminders Quiz 2 this week.
Lists Part 1 Taken from notes by Dr. Neil Moore
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Bryan Burlingame 03 October 2018
Lesson 09: Lists Topic: Introduction to Programming, Zybook Ch 8, P4E Ch 8. Slides on website.
6. Lists Let's Learn Python and Pygame
8 – Lists and tuples John R. Woodward.
CS 1111 Introduction to Programming Fall 2018
Python for Informatics: Exploring Information
String and Lists Dr. José M. Reyes Álamo.
Chapter 5: Lists and Dictionaries
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Lesson 09: Lists Class Chat: Attendance: Participation
Topics Sequences Introduction to Lists List Slicing
Lists Part 1 Taken from notes by Dr. Neil Moore
CS 1111 Introduction to Programming Spring 2019
15-110: Principles of Computing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Sequences Introduction to Lists List Slicing
And now for something completely different . . .
Python Review
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
For loop Using lists.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
def-ining a function A function as an execution control structure
COMPUTER SCIENCE PRESENTATION.
Python - Tuples.
Introduction to Computer Science
Presentation transcript:

From Think Python How to Think Like a Computer Scientist Python LISTS chapter 10 From Think Python How to Think Like a Computer Scientist

Introduction to lists A list is a sequence of values (called elements) that can be any type. Here are some examples of lists [2,4,6,8,10] [‘a’,’b’,’c’,’d’,’e’] [‘hello’,’there’,’Bob’] [‘bob’,23.0,145,[1,2]] # This contains a string, float, int and # another string. Each is an element # We can put these in variables nums = [3,2,5,4.5,3.0,1000] names = [‘Bob’,’Sally’,’Tom’,’Harry] empty = [] #This is the empty string print names  Check this out >>> ['Bob', 'Sally', 'Tom', 'Harry']

Lists are ordered and Mutable #Unlike strings we can modify the individual elements of a list. numbers =[7,-2,3,4,5,6.0] #list indices work like string #indices numbers[3]=10 print numbers # we can print them [7,-2,3,10,5,6.0] #The in operator works here as well >>>3 in numbers True

Traversing a list for val in numbers: print val, 7 -2 3 10 5 6.0 # Square each number in list for I in range(len(numbers)): numbers[i]=numbers[i]**2 print numbers [49, 4, 9, 100, 25, 36.0] The length of the following list is 4. s = [[1,2],3.0,’Harry’,[3,5,6,1,2]] Of course the length of s[3] is 5 The length of [] is 0

Operations on list + works like it does on strings, i.e. it concatinates [1,2,3]+ [4,5,6] becomes [1,2,3,4,5,6] and [1,2,3]*3 becomes [1,2,3,1,2,3,1,2,3] What does 10*[0] give you? [0,0,0,0,0,0,0,0,0,0]

List slices t = [11,21,13,44,56,68] print t[2,4] [13,44] # Remember: It doesn’t include slot 4!! print t[3:] [44,56,68] # check this out t[2:5]=[7,3,1] # You can update multiple elements print t [11,21,7,3,1,68]

List Methods #Sort: sorts the list in place t = [4,3,5,2,7,1] t.sort() # Append : adds a new element # to the end t=[2,4,6] t.append(8) print t [2,4,6,8] #extend: adds a list to end of list t=[‘a’,b’,c’,d’] t.extend([‘w’,’x’]) [‘a’,b’,c’,d’,’w’,’x’] #Sort: sorts the list in place t = [4,3,5,2,7,1] t.sort() print t #t is modified! [1,2,3,4,5,7]

Accumulating a list def add_them(t): # here t is a list of numbers total =0 for x in t: total += x # same as total = total + x return total # Python already has something like this built in, called sum total = sum(t) #would return its sum # of course you could write your own function to do anything #you want to the elements of t

Returning a list #What does the following do? def do_it (s): # Here s is a list of strings res =[] for a in s: res.append(s.capitalize()) return res These guys traverse a list and return parts of it in another list #Returns a list of positive #numbers def get_pos(t): res = [] for i in t: if i>0: res.append(i) return res

Deleting from list t=[6,3,7,8,1,9] x=t.pop(3) #remove element in slot 3 and assign it to x print t [6,3,7,1,9] #note : element in slot 3 ie 8 is now gone ---------------------------------------------------------------------------------------- del t[3] #does the same thing as pop(3) but returns nothing t.remove(8) #use this to remove an 8 from the list # it returns nothing

Processing a list and graphing using Matplotlib from pylab import * x= [-5,-4,-3,-2,-1,0,1,2,3,4,5] y=[] #build y from x for i in x: y.append(i**2-2*i+3) print y plot(x,y) show() Plots parameters are lists!