初学Python 3.2.2 版本 注意print 时加()

i = 5
print (i)
i = i + 1
print (i)

s = "This is the first sentence.\
This is the second sentence."

print (s)

print ('-------------------')
length = 5
breadth = 2
area = length * breadth
print ('Area is', area)
print ('Perimeter is', 2 * (length + breadth))

print ('-------------------')
number = 23
guess = 45

if guess == number:
print ('Congratulations, you guessed it.') # New block starts here
print ("(but you do not win any prizes!)") # New block ends here
elif guess < number:
print ('No, it is a little higher than that') # Another block
# 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')

print ('--------while语句 -----------')
print ('--------while语句\
-------------------')

number = 157
running = True

while running:
guess = 156
guess=guess+1
if guess == number:
print ('相等' )
running = False # this causes the while loop to stop
elif guess < number:
print ('No, it is a little higher than that' )
else:
print (guess)
else:
print (guess )
print ('The while loop is over.' )

# Do anything else you want to do here

print ('Done' )


print ('---------for循环----------')

alist = [1,2,3,4,5,6,7,8]
for i in alist:
if alist.index(i) >4:
break
#print blist
else:
alist.extend([i+5])
#alist.append(i+5)
print (alist )

print ('---------函数----------')

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

sayHello()
print ('---------函数形参----------')

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

#printMax(3, 4) # directly give literal values

x = 5
y = 7

printMax(x, y) # give variables as arguments

 


def p(l,k):
if l>k:
print(l,'is max')
else:
print (k,"is max")


p(4,5)

print ('---------局部变量----------')

def func(x):
print ('x is', x)

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

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

 

print ('---------默认参数值----------')

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

say('hello')
say('hello',5)


print ('---------使用关键参数----------')

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)


print ('---------return语句----------')

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

print (maximum(2, 3))

print ('---------DocStrings----------')
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__ )
print ('---------使用sys模块----------')


import sys

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

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

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

print ('---------创建你自己的模块----------')

def sayhi():
print ('hi , this is mymodule speaking')
version='0.1'

import mymodule

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

 

 

 

 

 

 

 

 

posted on 2012-02-15 11:53  xupeng  阅读(937)  评论(0编辑  收藏  举报

导航