Monday 14 May 2012

Python List Illustrated Code

repl.it displaying code and output for list_practice.py


Code Listing

# list_practise_repl_it.py
# 4.2 Lists
print """a_list = ['a','b','cow',1,2]     # constructs/assigns a list"""
a_list = ['a','b','cow',1,2]
print a_list
print                   # adds extra line to console output

print """a_list.append('antelope')  # Adds item to the end of the list."""
a_list.append('antelope')
print a_list

print
print """a_list.sort()              # Sorts the items of the list, """
a_list.sort()
print a_list
print
print "The built-in function len() also applies to lists."
print "It returns the number of items in the list:"
print "With the following list: " , a_list
print "len(a_list) # currently returns count of  %i"  %  len(a_list)
print
print """ Since Python starts counting from 0
Indexing: L[0]  (first element),
              L[1]  (second element),
          ...
              L[5]  (sixth element)
          or
              L[-1] (last element)
              """

print """a_list [0:0] = ['zebra']   # insert item at start of the list."""
a_list[0:0] = ['zebra']
print a_list

No comments:

Post a Comment