#Lists are more useful, but more complicated than tuples. #difference between lists and tuples are that tuples are *immutable* -- Once you create a tuple, you cannot change its value. #Lists are *Mutable* -- Once you create a list you can actually change it. #assignments don't change the value of an object -- it just changes the object to which an id was bound; mutability is the way to change the value of an object itself # powerful concept, but also an opportunity to create programming blunders # methods have different syntax than functions -- will see the importance when we learn classes # Append is a *method*, method is an alternative syntax for writing a function, instead of function(L,e), you write L.method(e) #slice - subsequence of tople or list #append is a method #instead of append (L, e) #L.append(e) # ## ASSIGNMENT vs. MUTATION - underlying point. # Example 1 --- Lists #Techs = ['MIT', 'Cal Tech'] ##Create Lists, Techs & Ivys #Ivys = ['Harvard', 'Yale', 'Brown'] #Univs = [] #Univs.append(Techs) ##Append Techs to Univs -- modifies the list by appending a list to the end of the empty list, THE LIST ITSELF IS APPENDED TO THE EMPTY LIST #print 'Univs =', Univs #Univs.append(Ivys) #print 'Univs =', Univs #for e in Univs: # print 'e =', e #flat = Techs + Ivys #print "flat =", flat #artSchools = ['RISD', 'Harvard'] #for u2 in artSchools: # if u2 in flat: # flat.remove(u2) #print 'flat =', flat #flat.sort() #print 'flat =', flat #flat[1] = 'UMass' #print 'flat =', flat
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question