上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Applying Lists We need a way to store and manipulate an entire collection of numbers. o We can't just use a bunch of variables, because we don't know many numbers there will be. What do we need?Some way of combining an entire collection of values into one object. 11
Applying Lists • We need a way to store and manipulate an entire collection of numbers. • We can‟t just use a bunch of variables, because we don‟t know many numbers there will be. • What do we need? Some way of combining an entire collection of values into one object. 11
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Lists and Arrays Python lists are ordered sequences of items.For instance,a sequence of n numbers might be called S; S=S0,S1,S2,S3,,Sn-1 Specific values in the sequence can be referenced using subscripts. By using numbers as subscripts,mathematicians can succinctly summarize computations over items in a sequence using subscript variables. i=0 12
Lists and Arrays • Python lists are ordered sequences of items. For instance, a sequence of n numbers might be called S: S = s0 , s1 , s2 , s3 , …, sn-1 • Specific values in the sequence can be referenced using subscripts. • By using numbers as subscripts, mathematicians can succinctly summarize computations over items in a sequence using subscript variables. 12 1 0 n i i s
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Lists and Arrays 。 Suppose the sequence is stored in a variable s. We could write a loop to calculate the sum of the items in the sequence like this: sum 0 for i in range(n): sumsum s[i] Almost all computer languages have a sequence structure like this,sometimes called an array. 13
Lists and Arrays • Suppose the sequence is stored in a variable s. We could write a loop to calculate the sum of the items in the sequence like this: sum = 0 for i in range(n): sum = sum + s[i] • Almost all computer languages have a sequence structure like this, sometimes called an array. 13
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Lists and Arrays A list or array is a sequence of items where the entire sequence is referred to by a single name (i.e.s)and individual items can be selected by indexing (i.e.s [i]). In other programming languages,arrays are generally a fixed size,meaning that when you create the array,you have to specify how many items it can hold. Arrays are generally also homogeneous, meaning they can hold only one data type. 14
Lists and Arrays • A list or array is a sequence of items where the entire sequence is referred to by a single name (i.e. s) and individual items can be selected by indexing (i.e. s[i]). • In other programming languages, arrays are generally a fixed size, meaning that when you create the array, you have to specify how many items it can hold. • Arrays are generally also homogeneous, meaning they can hold only one data type. 14
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Lists A list is a compound type >>range (10) [0,1,2,3,4,5,6,7,8,9] >>string.split("This is an ex-parrot!") ['This','is,'an','ex-parrot!' >>>myList [23,'ab' >>>myList [23,'ab'] Lists are ordered sequences of item Access to the List and all it items All operators applied to sequence applied to Lists 15
Lists • A list is a compound type >>> range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> string.split("This is an ex-parrot!") ['This', 'is„, 'an', 'ex-parrot!'] >>>myList[23,‟ab‟] >>>myList [23, ‟ab‟] • Lists are ordered sequences of item – Access to the List and all it items – All operators applied to sequence applied to Lists 15