Python3.0的学习 代码 。

2011-01-30  沈优娴 

1.hello world.py
#!/usr/bin/python
# Filename : helloworld.py
print('Hello World')

2.var.py
# Filename : var.py
i = 5
print (i)
i = i + 1
print (i)
s = '''This is a multi-line string.
This is the second line.'''
print (s)


3.expression.py

#!/usr/bin/python
# Filename: expression.py
length = 5
breadth = 2
area = length * breadth
print ('Area is', area)
print ('Perimeter is', 2 * (length + breadth) )


4.if.py
#!/usr/bin/python
# Filename: if.py 
number = 23
guess =int(input('Enter an integer : ')) #  V3.0 change raw_input() to input()

#guess=int(raw_input("Enter an integer:"))V2.5
if guess == number:
    print ('Congratulations, you guessed it.') # New b
    print ("(but you do not win any prizes!)") # New bl
elif guess < number:
    print ('No, it is a little higher than that') # Anot
    #You can do whatever you want in a block ...
else:
    print ('No, it is a little lower than that' )
    # you must have guess > number to reach here
print ("Done")
# This last statement is always executed, after t


5.while.py



#!/usr/bin/python 
# Filename: while.py

number = 23
running = True

while running:
    guess = int(input('Enter an integer :'))

    if guess == number:
        print ('Congratulations, you guessed it.')
        running = False # this causes the while loop to stop
    elif guess < number:
        print ('No, it is a little higher than that') 
    else:
        print ('No, it is a little lower than that' )
else:
    print ('The while loop is over.' )

    # Do anything else you want to do here

print  ('Done')


6.for.py

#!/usr/bin/python 
# Filename: for.py

for i in range(1, 5):
    print (i)
else:
    print ('The for loop is over')

7.break.py
#!/usr/bin/python 
# Filename: break.py

while True:
    s = input('Enter something : ')
    if s == 'quit':
        break
    print ('Length of the string is', len(s))
print ('Done')


8.function1.py
#!/usr/bin/python 
# Filename: function1.py

def sayHello():
    print ('Hello World!') # block belonging to the function

sayHello() # call the function


9.func_param.py
#!/usr/bin/python 
# Filename: func_param.py

def printMax(a, b):
    if a > b:
        print (a, 'is maximum')
    else:
        print (b, 'is maximum')

printMax(3, 19) # directly give literal values

x = 1
y = 12

printMax(x, y) # give variables as arguments

10.func_local.py

#!/usr/bin/python 
# Filename: func_local.py

def func(x):
    print 'x is', x
    x = 2
    print 'Changed local x to', x

x = 50
func(x)
print 'x is still', x

11.func_global.py
#!/usr/bin/python 
# Filename: func_global.py

def func():
    global x

    print ('x is', x)
    x = 2
    print ('Changed local x to', x)

x = 50
func()
print ('Value of x is', x)


12.func_default.py
#!/usr/bin/python 
# Filename: func_default.py

def say(message, times = 1):
    print (message * times)

say('Hello ',3)
say('World ', 5)

13.func_key.py
#!/usr/bin/python 
# Filename: func_key.py

def func(a, b=5, c=10):
    print ('a is', a, 'and b is', b, 'and c is', c)

func(3, 7)
func(25, c=24)
func(c=50, a=100)


14.func_return.py

#!/usr/bin/python 
# Filename: func_return.py

def maximum(x, y):
    if x > y:
        return x
    else:
        return y

print maximum(2, 3)


15.func_doc.py

#!/usr/bin/python 
# Filename: func_doc.py

def printMax(x, y):
    '''Prints the maximum of two numbers. 
 
    The two values must be integers.'''
    x = int(x) # convert to integers, if  possible
    y = int(y)

    if x > y:
        print (x, 'is maximum')
    else:
        print( y, 'is maximum')

printMax(3, 5)
print (printMax.__doc__)


16. using_sys.py
#!/user/bin/python
# Filename: using_sys.py

import sys

print ('The command line arguments are:')
for i in sys.argv:
    print (i)

print( '\n\nThe PYTHONPATH is', sys.path, '\n')

17.using_name.py
 #!/usr/bin/python 
# Filename: using_name.py

if __name__ == '__main__':
    print ('This program is being run by itself')
else:
    print ('I am being imported from another module')

18.mymodule.py
#!/usr/bin/python 
# Filename: mymodule.py

def sayhi():
    print ('Hi, this is mymodule speaking.')

version = '0.1'

# End of mymodule.py

19.mymodule_demo.py
#!/usr/bin/python 
# Filename: mymodule_demo.py

import mymodule
mymodule.sayhi()
print ('Version', mymodule.version)

20.using_list.py 
#!/usr/bin/python
# Filename: using_list.py 
 
# This is my shopping list
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 )


453°/4510 人阅读/2 条评论 发表评论

梅诚  2011-02-02

python3.0....


熊志男  2011-06-30

用3.0了,我还用2.6呢


登录 后发表评论