Python (1)
1.
##########if begin
number = 23
guess = int(input('Enter an integer : '))
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')
summary: if ....: ...
elif ...: ...
else:
#######if end
2.
### help begin
help ("input")
### help end
3.
### while begin
number = 23
running = True
while running:
guess = int(raw_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'
### while end
4.
### for begin
for i in range(1, 5):
print i
else:
print 'The for loop is over'
# range(1,5,2)给出[1,3]
### for end
5.
### break begin
while True:
s = input('Enter something : ')
if s == 'quit':
break
print 'Length of the string is', len(s)
print 'Done'
### break end
6.
### continue begin
while True:
s = raw_input('Enter something : ')
if s == 'quit':
break
if len(s) < 3:
continue
print 'Input is of sufficient length'
# Do other kinds of processing here...
### continue end
7.
### def function begin
def sayHello():
print 'Hello World!' # block belonging to the function
sayHello() # call the function
### def function end
8.
### def funciton with parameters begin
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 funciton with parameters end
9.
### golbal begin
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
# global x, y, z
### global end
10.
### def print *x begin
def say(message, times = 1):
print message * times
say('Hello')
say('World', 5)
### def print *x end
11.
### return begin
#return语句用来从一个函数 返回 即跳出函数
def maximum(x, y):
if x > y:
return x
else:
return y
print maximum(2, 3)
### return end
12.
### pass begin
def someFunction():
pass
#pass语句在Python中表示一个空的语句块
###pass end
13.
### DocStrings begin
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__
#文档字符串的惯例是一个多行字符串,它的首行以大写字母开始,句号结尾。第二行是空行,从第三行开始是详细的描述。 强烈建议 你在你的函
#数中使用文档字符串时遵循这个惯例。
### DocStrings end
14.
模块的文件名必须以.py为扩展名
from sys import argv
如果你想要输入所有sys模块使用的名字,那么你可以使用from sys import *语句
15.
### Create module
#!/usr/bin/python
# Filename: mymodule.py
def sayhi():
print 'Hi, this is mymodule speaking.'
version = '0.1'
# End of mymodule.py
#!/usr/bin/python
# Filename: mymodule_demo.py
import mymodule
mymodule.sayhi()
print 'Version', mymodule.version
### create module end
### another call module begin
#!/usr/bin/python
# Filename: mymodule_demo.py
from mymodule import sayhi, version
# Alternative:
# from mymodule import *
sayhi()
print 'Version', version
### another call module end
16.
dir函数来列出模块定义的标识符。标识符有函数、类和变量。
当你为dir()提供一个模块名的时候,它返回模块定义的名称列表。如果不提供参数,它返回当前模块中定义的名称列表。
import sys
dir(sys)
dir()
浙公网安备 33010602011771号