Python 入门教程 7 ---- PygLatin
Python 入门教程 7 ---- PygLatin
第一节 1 介绍了Python的函数组成有三部份,函数头,函数体
2 函数的举例
[python]
def ni_sayer():
"""Prints 'Ni!' to the console."""
print "Ni!"
3 练习:写一个函数,输出字符串"Eggs!",函数体增加一行注释
[python]
# Define your spam function starting on line 5. You
# can leave the code on line 11 alone for now--we'll
# explain it soon!
def spam():
"""this is a zhushi"""
print "Eggs!"
# Define the spam function above this line.
spam()
第二节
1 介绍了函数的调用,就是直接函数名
2 练习:调用函数spam(10)
[python]
def square(n):
"""Returns the square of a number."""
squared = n**2
print "%d squared is %d." % (n, squared)
return squared
# Call the square function on line 9! Make sure to
# include the number 10 between the parentheses.
square(10)
第三节
1 介绍了函数可以使用参数的传递
2 比如函数no_one(sentence)的使用,传入字符串作为参数
[python]
def no_one(sentence):
print sentence
no_one("The Spanish Inquisition")
3 练习:把函数的参数改为base,exponent。调用函数传入37和4
[python] view plaincopy
def power(base,exponent): # Add your parameters here!
result = base**exponent
print "%d to the power of %d is %d." % (base, exponent, result)
power(37,4) # Add your arguments here!
第四节
1 介绍了*的用法,比如我们传入一个字符串,那么我们可以使用*name接收,然后可以利用name来输出。不一定使用name,可以是任何的名字
2 练习
[python]
def favorite_actors(*name):
"""Prints out your favorite actorS (plural!)"""
print "Your favorite actors are:" , name
favorite_actors("Michael Palin", "John Cleese", "Graham Chapman")
第五节
1 介绍了函数体里面还可以调用另外的函数
2 比如我们在第二个函数里面调用了第一个函数
[python]
def fun_one(n):
return n * 5
def fun_two(m):
return fun_one(m) + 7
3 练习:把第二个函数调用第一个函数并输出
[python]
def one_good_turn(n):
return n + 1
def deserves_another(n):
return n + one_good_turn(2)
第六节
1 练习
1 定义第一个函数cube(),有一个参数num,返回num的3次方
2 定义另外一个函数by_three(),有一个参数num,如果num能够被3整除那么调用cube并返回值,否则返回False
3 调用函数by_three(9) 和 by_three(4)
[python]
def cube(num):
return num**3
def by_three(num):
if(num%3 == 0):
return cube(num)
else:
return False
by_three(9)
by_three(4)
第七节
1 介绍了Python里面可以导入很多的系统的模块,就像c语言的include
2 假设我们没有导入math模块的时候,那么执行print sqrt(25)的时候会报错
3 练习
1 导入math模块,import math
2 执行print math.sqrt(25),加了一个math说明调用系统的库函数
[python]
# Ask Python to print sqrt(25) on line 3.
import math
print math.sqrt(25)
第八节
1 介绍了我们还可以只是单独的导入模块里面的方法
2 比如from moduleimport
function
3 练习:从math模块里面值导入sqrt函数
[python]
# Import *just* the sqrt function from math on line 3!
from math import sqrt
print sqrt(25)
第九节
1 介绍了我们可以使用from moduleimport
*来表示从模块里面导入所有的函数,这样调用的时候就直接调用即可
2 练习:从math模块里面导入所有的方法,然后随便选择一个函数来测试
[python]
# Import *everything* from the math module on line 3!
from math import *
print sqrt(25)
第十节
1 介绍了from module import *方法的缺点就是,如果我们导入了很多的模块,那么可能导致出现相同的函数名,因此我们最好是使用import module,然后使用module.name
2 测试以下代码的结果
[python]
import math # Imports the math module
everything = dir(math) # Sets everything to a list of things from math
print everything # Prints 'em all!
第十一节
1 介绍了第一个函数max(),比如max(1,2,3)返回的是3 (min函数是类似的)
2 max()函数的参数是一个数组,返回数组中的最大值
3 练习:使用max函数来得到一个数组的最大值
[python]
# Set maximum to the max value of any set of numbers on line 3!
maximum = max(4,0,-3,78)
print maximum
第十二节
1 介绍了第二个函数abs()返回的值永远是正数,比如abs(-5)返回的是5
2 练习:测试输出abs(-42)的值
[python]
absolute = abs(-42)
print absolute
第十三节
1 介绍了type函数的使用,type函数返回的是当前这种数据的类型,比如int , float等
2 type函数的使用举例
[python]
print type(42)
print type(4.2)
print type('spam')
print type({'Name':'John Cleese'})
print type((1,2))
<type 'int'>
<type 'float'>
<type 'unicode'>
<type 'dict'>
<type 'tuple'>
3 练习:使用type函数至少得到int,float,unicode三种类型
[python]
# Print out the types of an integer, a float,
# and a string on separate lines below.
print type(4)
print type(4.2)
print type('spam')
相关内容
- 暂无相关文章
评论关闭