中国科学技术大学电子工程与信息科学系 ©2020 写一段代码,允许输入3遍口令。 如果输入的口令是“abc”,显示“OK”,否则显示“wrong password,pls input again” 1.4函数(function) 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段。 你可以定义一个由自己想要功能的函数,以下是简单的规则: 函数代码块以def关键词开头,后接函数标识符名称和圆括号0。 任何传入参数和白变量必须放在圆括号中间。圆括号之间可以用于定义参数。 函数的第一行语句可以选择性地使用文档字符串一用于存放函数说明。 函数内容以冒号起始,并且缩进。 return表达式结束函数,选择性地返回一个值给调用方。不带表达式的return相 当于返回None。 1.4.1 自定义函数 仅用于 https:Iwww runoob com/python/python-functions 例如, def my_add(a,b): return a+b a=3 b=5 print(my_add(a,b)) 信息科学技术 1.4.2 Python内置函数 https://www.runoob.com/python/python-built-in-functions.html abs() divmod() input() open() staticmethod() all() enumerate() intO ordo str() any() eval() isinstance() pow( sum( basestring() execfile() issubclass() print() super() bin( file( iter() property() tuple() bool() filter len() range() type() bytearray() float() listo raw input() unichr() callable() format() locals() reduce() unicode() chro frozenset() long() reload() vars() classmethod() getattr() map() repr() xrange() cmp() globals() max() reverse() zip() compile() hasattr() memoryview() round() import ( complex( hash() min() set() delattr() help( next() setattr() dicto hex() object() slice) dir() idO oct() sorted() exec内置表达 network @ustc.edu.cn
中国科学技术大学电子工程与信息科学系 ©2020 network@ustc.edu.cn 写一段代码,允许输入 3 遍口令。 如果输入的口令是“abc”,显示“OK”,否则显示“wrong password, pls input again” 1.4 函数(function) 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段。 你可以定义一个由自己想要功能的函数,以下是简单的规则: 函数代码块以 def 关键词开头,后接函数标识符名称和圆括号()。 任何传入参数和自变量必须放在圆括号中间。圆括号之间可以用于定义参数。 函数的第一行语句可以选择性地使用文档字符串—用于存放函数说明。 函数内容以冒号起始,并且缩进。 return [表达式] 结束函数,选择性地返回一个值给调用方。不带表达式的 return 相 当于返回 None。 1.4.1 自定义函数 参考 https://www.runoob.com/python/python-functions.html 例如, def my_add(a,b): return a + b a = 3 b = 5 print(my_add(a,b)) 1.4.2 Python 内置函数 https://www.runoob.com/python/python-built-in-functions.html abs() divmod() input() open() staticmethod() all() enumerate() int() ord() str() any() eval() isinstance() pow() sum() basestring() execfile() issubclass() print() super() bin() file() iter() property() tuple() bool() filter() len() range() type() bytearray() float() list() raw_input() unichr() callable() format() locals() reduce() unicode() chr() frozenset() long() reload() vars() classmethod() getattr() map() repr() xrange() cmp() globals() max() reverse() zip() compile() hasattr() memoryview() round() __import__() complex() hash() min() set() delattr() help() next() setattr() dict() hex() object() slice() dir() id() oct() sorted() exec 内 置 表 达
中国科学技术大学电子工程与信息科学系 ©2020 式 1.5模块(module) 随着你的程序变得越来越长,你可能想要将它分割成几个更易于维护的文件。你也可能想在 不同的程序中使用顺手的函数,而不是把代码在它们之间中拷来拷去。 为了满足这些需要,Python提供了一个方法可以从文件中获取定义,在脚本或者解释器的 一个交互式实例中使用。这样的文件被称为模块:模块中的定义可以导入到另一个模块 或主模块中(在脚本执行时可以调用的变量集位于最高级,并且处于计算器模式)。 模块是包括Python定义和声明的文件。文件名就是模块名加上py后缀。模块的模块名 (做为一个字符串)可以由全局变量name得到。例如,你可以用自己惯用的文件编辑 器在当前目录下创建一个叫bo.py的文件,录入如下内容: 1.5.1 自定义模块 https://www.runoob.com/manual/pythontutorial3/docs/html/modules.html 写一个模块 #my-math.py def add(a,b): return a+b def mul(a b): return a *b def div(a,b): return a/b def mod(a,b): 学信息科学技术学院 return a %b 编写另外一个脚本文件,调用上面模块中定义的函数 #my math.py是自己定义的module,里面写好了一些函数 import my_math print("pls input Ist number") i=int(input()) print("pls input 2nd number") J=int(input()) print(my_math.add(i,j)) print(my math.mod(1,1)) 写一段代码,输入一个整数,计算其阶乘的结果并输出。 1.5.1.1 mport和From..import的区别 https://jingyan.baidu.com/article/15622f242e15b6fdfcbea5b5.html network @ustc.edu.cn
中国科学技术大学电子工程与信息科学系 ©2020 network@ustc.edu.cn 式 1.5 模块(module) 随着你的程序变得越来越长,你可能想要将它分割成几个更易于维护的文件。你也可能想在 不同的程序中使用顺手的函数,而不是把代码在它们之间中拷来拷去。 为了满足这些需要,Python 提供了一个方法可以从文件中获取定义,在脚本或者解释器的 一个交互式实例中使用。这样的文件被称为 模块;模块中的定义可以 导入 到另一个模块 或 主模块 中(在脚本执行时可以调用的变量集位于最高级,并且处于计算器模式)。 模块是包括 Python 定义和声明的文件。文件名就是模块名加上 .py 后缀。模块的模块名 (做为一个字符串)可以由全局变量 __name__ 得到。例如,你可以用自己惯用的文件编辑 器在当前目录下创建一个叫 fibo.py 的文件,录入如下内容: 1.5.1 自定义模块 参考 https://www.runoob.com/manual/pythontutorial3/docs/html/modules.html 写一个模块 #my-math.py def add(a,b): return a + b def mul(a,b): return a * b def div(a,b): return a / b def mod(a,b): return a % b 编写另外一个脚本文件,调用上面模块中定义的函数 #my_math.py 是自己定义的 module,里面写好了一些函数 import my_math print("pls input 1st number") i = int(input()) print("pls input 2nd number") j = int(input()) print(my_math.add(i,j)) print(my_math.mod(i,j)) 写一段代码,输入一个整数,计算其阶乘的结果并输出。 1.5.1.1 Import 和 From…import 的区别 https://jingyan.baidu.com/article/15622f242e15b6fdfcbea5b5.html