Monday, July 25, 2011

Python Dictionaries and Lists Difference

#debugger example

print "This function creates a list."

def makelist():
    a = [] # list
    for i in range(1, 20):
        a.append(i)
        print "appending", i
        print "now list", a
    return a

def printlist(a):
    for i in a:
        print i,

b=makelist()
printlist(b)



#Dictionaries

#rock, paper, scissors game using dictionary

choice = raw_input("Enter s, r, or p: ")
results = {"s": "cut paper", "r": "crushes scissors", "p":  
    "covers rock"}
print choice, results[choice]

#
#

3 comments:

  1. what is the approved version of the python for AL ICT?

    ReplyDelete
  2. There is no version approved. Python 2.6 and 3.2 has differences.
    Example : print statement python 2.6 is
    print "hello"

    in python 3.x its print ("hello") # with ()
    every major version of python would have major changes. Therefore it's better to mention python version in answers.

    ReplyDelete
  3. Dear sir....
    Could you send all python codings for a/l ict syllabus to my mail.

    mathu2000@gmail.com

    ReplyDelete