实验目的与要求:

实验步骤:

>>> 1+1
>>> 2**10
>>> r = 5
>>> pi = 3.14159
>>> pi*r*r
>>>
>>> "hello world"
>>> print "hello world"
>>> "hello " + "world\n"
>>> print "hello " + "world\n"
>>> 
>>> x = 54
>>> y = 36
>>> if x>y:
        print x
    else:
        print y
>>>
>>> while y != 0:
        y, x = x%y, y
>>> print x
>>>
>>> import sys
>>> sys.version
>>> sys.platform
>>> 

>>> shoplist = ['apple', 'mango', 'carrot', 'banana']

>>> print 'I have', len(shoplist),'items to purchase.'

>>> print 'These items are:', # Notice the comma at end of the line
>>> for item in shoplist:
>>>     print item,

>>> print '\nI also have to buy rice.'
>>> shoplist.append('rice')
>>> print 'My shopping list is now', shoplist

>>> print 'I will sort my list now'
>>> shoplist.sort()
>>> print 'Sorted shopping list is', shoplist

>>> print 'The first item I will buy is', shoplist[0]
>>> olditem = shoplist[0]
>>> del shoplist[0]
>>> print 'I bought the', olditem
>>> print 'My shopping list is now', shoplist

>>> help()
>>> help(sys)
>>> dir(sys)
>>> (Ctrl+d或者ctrl+z)

   1 def GCD(x,y):
   2     while y != 0:
   3         y, x = x%y, y
   4     return x
   5 
   6 print GCD(36, 54)

或者

   1 def buildConnectionString(params):
   2     """Build a connection string from a dictionary of parameters.
   3 
   4     Returns string."""
   5     return ";".join(["%s=%s" % (k, v) for k, v in params.items()])
   6 
   7 if __name__ == "__main__":
   8     myParams = {"server":"mpilgrim", \
   9                 "database":"master", \
  10                 "uid":"sa", \
  11                 "pwd":"secret" \
  12                 }
  13     print buildConnectionString(myParams)

思考题:

Python游戏开发基础/实验1 (2008-02-23 15:35:37由localhost编辑)

ch3n2k.com | Copyright (c) 2004-2020 czk.