Change Contents of the Bubble
Welcome to CS1315. Click on the python to add comments.

Looking for the book? They have it at the Engineer's Bookstore at 748 Marietta St NW. Here is there website: http://www.engrbookstore.com/ - Monica

Hotspots: Slides and CodeTA CornerComments?AnnouncementsFAQStatic Webspace
View this PageEdit this Page (locked)Uploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

List Basics and Methods

def listBasicsAndMethods():

  glist = ['cereal', 'milk', 'water', 'bread', 8, ['lemons', ['bananas', 'oranges', 'apples']]]
  print glist 
  print('\n')
  
  print glist[0]
  print('\n')
  
  for i in glist:
    print i
  print('\n')
  
  glist = glist + ['cake']
  print glist
  print('\n')

  print glist[5]
  print glist[5][0]
  print glist[5][1]
  print glist[5][1][0]
  print glist[5][1][1]
  print glist[5][1][2]
  print glist[5][1][2][0]
  print('\n')

  #The .append(something) method
  #Adds something to the end of the list
  print '.append(something) method'
  glist.append('gatorade')
  print glist
  print('\n')

  #The .remove(something) method
  #Removes something from the end of the list
  #Error if not there
  print '.remove(something) method'
  # print glist.remove('bread') #Returns None because it actually changes the glist
  glist.remove('bread')
  print glist
  #glist.remove('cookies')
  #print glist
  #glist.remove('lemons')  #Must access sublist
  #print glist
  glist[4].remove('lemons') #Must access sublist
  print glist
  glist[4][0].remove('oranges') #Must access sublist
  print glist
  print('\n')

  #The .sort() method
  #Puts the list in alphabetical order
  print '.sort() Method'
  glist.sort()
  print glist
  print('\n')

  #The .reverse() method
  #reverses the order of the list
  print '.reverse() Method'
  glist.reverse()
  print glist
  print('\n')

  #The .count(something) method
  #Counts the number of times something is in a list
  print '.count() Method'
  print glist.count('water')
  print glist.count('apples')
  print glist[5][0].count('apples')
  print('\n')

  #The .max(), .min() methods
  #returns the maximum and minimum values in a list
  print '.max(), .min() Methods'
  nlist=[ 23, 45, 88, 67, 12, 99, 100, 1, 62, 13, 54, 90]
  print max(nlist)
  print min(nlist)
  print max(glist)
  print min(glist)
  print('\n')








  


Link to this Page