第一节
1 介绍了Python的函数组成有三部份,函数头,函数体
2 函数的举例
def ni_sayer():
"""Prints 'Ni!' to the console."""
print "Ni!"
3 练习:写一个函数,输出字符串"Eggs!",函数体增加一行注释
# 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)
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)的使用,传入字符串作为参数
def no_one(sentence):
print sentence
no_one("The Spanish Inquisition")
3 练习:把函数的参数改为base,exponent。调用函数传入37和4
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 练习
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 比如我们在第二个函数里面调用了第一个函数
def fun_one(n):
return n * 5
def fun_two(m):
return fun_one(m) + 7
3 练习:把第二个函数调用第一个函数并输出
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)
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说明调用系统的库函数
# Ask Python to print sqrt(25) on line 3.
import math
print math.sqrt(25)
第八节
1 介绍了我们还可以只是单独的导入模块里面的方法
2 比如from moduleimport
function
3 练习:从math模块里面值导入sqrt函数
# Import *just* the sqrt function from math on line 3!
from math import sqrt
print sqrt(25)
第九节
1 介绍了我们可以使用from moduleimport
*来表示从模块里面导入所有的函数,这样调用的时候就直接调用即可
2 练习:从math模块里面导入所有的方法,然后随便选择一个函数来测试
# Import *everything* from the math module on line 3!
from math import *
print sqrt(25)
第十节
1 介绍了from module import *方法的缺点就是,如果我们导入了很多的模块,那么可能导致出现相同的函数名,因此我们最好是使用import module,然后使用module.name
2 测试以下代码的结果
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函数来得到一个数组的最大值
# 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)的值
absolute = abs(-42)
print absolute
第十三节
1 介绍了type函数的使用,type函数返回的是当前这种数据的类型,比如int , float等
2 type函数的使用举例
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三种类型
# 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')
分享到:
相关推荐
3-python快速入门3-python快速入门3-python快速入门3-python快速入门3-python快速入门3-python快速入门3-python快速入门3-python快速入门3-python快速入门3-python快速入门3-python快速入门3-python快速入门3-python...
Python入门-基础知识-背记手册-使用说明Python入门-基础知识-背记手册-使用说明Python入门-基础知识-背记手册-使用说明Python入门-基础知识-背记手册-使用说明Python入门-基础知识-背记手册-使用说明Python入门-基础...
Python入门教程-超详细1小时学会Python.pdfPython入门教程-超详细1小时学会Python.pdfPython入门教程-超详细1小时学会Python.pdfPython入门教程-超详细1小时学会Python.pdfPython入门教程-超详细1小时学会Python.pdf...
⼩学⽣python⼊门-极度舒适的全套Python⼊门教程,⼩学⽣ 看了也能学会 编程⼏乎已经成为现代⼈的⼀门必修课,特别是 Python ,不仅长期霸占编程趋势榜、薪资榜第⼀,还屡屡进⼊⼩学教材,甚⾄成为浙江省 信息技术⾼...
python入门与实践--作业11
python-gtk 2013年7月31日 python入门基础教程:python-gtk全文共15页,当前为第1页。 安装 sudo aptitude install python-gtk2 文档 ...
python入门游戏源码-中国象棋
Python----入门手册及教程 Python----入门手册及教程 Python----入门手册及教程 Python----入门手册及教程 Python----入门手册及教程 Python----入门手册及教程
python⼉童⼊门教程-Python⼊门基础教程(⼉童版)[分享⼀ 本⼊门级教程] +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1. 推荐书名 No Starch--Python for Kids - A Playful ...
python入门教程-09-状态码介绍.ev4.rar
python入门教程-29-添加学员功能.ev4.rar
Python编程入门-2019-11-06 Python编程入门-2019-11-06
python入门教程-28-程序入口文件功能实现.ev4.rar
python入门教程-03-静态web服务器-面向对象版.ev4.rar
python入门教程-02-静态web服务器-多任务版.ev4.rar
Python学习笔记--皮大庆,非常适合零基础入门。对应英文版本《How to think like a computer scientist》
python 零基础学习篇-Python-第一章 Python入门导入1-3.mp3
python程序实验教程_Python⼊门教程实训-Python⼩程序 ⼤家好我是尚学堂⼩编,今天起每天为⼤家分享Python学员课堂相关实训教程⾥的教程演练或者相关视频。今天我⾯要写⼀个⼩程 序"Hello World" Python执⾏命令 ...