""" LISTS """ mylist = ["cat","dog","snake","rat"] print len(mylist) print mylist mylist[3] = "guinea pig" #replace 4th animal in list print mylist mylist.append("rat") #bring the rat back! print mylist top3 = mylist[0:3] #save first 3 in list; NOTE: DOES NOT INCLUDE LAST INDEX VALUE; only selects 0,1,2 print top3 bottom = mylist[3:] #the remaining losers print bottom print mylist.index("snake") #where is the snake on this list? #list functions print " ".join(mylist) #creates one string joined up with " " for animal in mylist: print animal + " " + str(mylist.index(animal)) #print animal name ands its index within the list x1 = [1,2,3] x2 = [a*2 for a in x1] print x1, x2 mylist.sort() #sorts alphanumerically print mylist mylist.remove("rat") #rat was eaten by snake... (removes item if found; do NOT input index) print mylist print mylist.pop(0) #.pop() will remove item at given index, and return it print mylist del(mylist[0]) #like .remove(), but deletes at inputed index, doesn't hunt down actual list item like .remove() does print mylist """ DICTIONARIES """ mydic = {"San Jose" : 408, "SLO" : 805, "NYC" : 212} print mydic["San Jose"] mydic["SF"] = 415 del mydic["NYC"] print mydic """ Inventory Example """ inventory = { 'gold' : 500, 'pouch' : ['flint', 'twine', 'gemstone'], 'backpack' : ['xylophone','dagger', 'bedroll','bread loaf'] } # Adding a key 'burlap bag' and assigning a list to it inventory['burlap bag'] = ['apple', 'small ruby', 'three-toed sloth'] # Sorting the list found under the key 'pouch' inventory['pouch'].sort() # Do stuff inventory['pocket'] = ['seashell','strange berry','lint'] inventory['backpack'].sort() inventory['backpack'].remove('dagger') inventory['gold'] += 50 print inventory
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