python基础教程常用函数整理(Python基础之函数的定义与使用示例)
类别:脚本大全 浏览量:2163
时间:2021-11-03 15:41:46 python基础教程常用函数整理
Python基础之函数的定义与使用示例本文实例讲述了Python基础之函数的定义与使用。分享给大家供大家参考,具体如下:
Python 定义函数使用 def 关键字,一般格式如下:
def 函数名(参数列表):
函数体
让我们使用函数来输出"Hello World!":
|
>>> def hello() : print ( "Hello World!" ) >>> hello() Hello World! >>> |
更复杂点的应用,函数中带上参数变量:
|
def area(width, height): return width * height def print_welcome(name): print ( "Welcome" , name) print_welcome( "Fred" ) w = 4 h = 5 print ( "width =" , w, " height =" , h, " area =" , area(w, h)) |
以上实例输出结果:
Welcome Fred
width = 4 height = 5 area = 20
函数变量作用域
定义在函数内部的变量拥有一个局部作用域,定义在函数外的拥有全局作用域。通过以下实例,你可以清楚了解Python函数变量的作用域:
|
#!/usr/bin/env python3 a = 4 # 全局变量 def print_func1(): a = 17 # 局部变量 print ( "in print_func a = " , a) def print_func2(): print ( "in print_func a = " , a) print_func1() print_func2() print ( "a = " , a) |
以上实例运行结果如下:
in print_func a = 17
in print_func a = 4
a = 4
关键字参数
函数也可以使用 kwarg=value 的关键字参数形式被调用.例如,以下函数:
|
def parrot(voltage, state = 'a stiff' , action = 'voom' , type = 'Norwegian Blue' ): print ( "-- This parrot wouldn't" , action, end = ' ') print ( "if you put" , voltage, "volts through it." ) print ( "-- Lovely plumage, the" , type ) print ( "-- It's" , state, "!" ) |
可以以下几种方式被调用:
|
parrot( 1000 ) # 1 positional argument parrot(voltage = 1000 ) # 1 keyword argument parrot(voltage = 1000000 , action = 'VOOOOOM' ) # 2 keyword arguments parrot(action = 'VOOOOOM' , voltage = 1000000 ) # 2 keyword arguments parrot( 'a million' , 'bereft of life' , 'jump' ) # 3 positional arguments parrot( 'a thousand' , state = 'pushing up the daisies' ) # 1 positional, 1 keyword |
以下为错误调用方法:
|
parrot() # required argument missing parrot(voltage = 5.0 , 'dead' ) # non-keyword argument after a keyword argument parrot( 110 , voltage = 220 ) # duplicate value for the same argument parrot(actor = 'John Cleese' ) # unknown keyword argument |
返回值
Python的函数的返回值使用return
语句,可以将函数作为一个值赋值给指定变量:
|
def return_sum(x,y): c = x + y return c res = return_sum( 4 , 5 ) print (res) |
你也可以让函数返回空值:
|
def empty_return(x,y): c = x + y return res = empty_return( 4 , 5 ) print (res) |
可变参数列表
最后,一个最不常用的选择是可以让函数调用可变个数的参数.这些参数被包装进一个元组(查看元组和序列).在这些可变个数的参数之前,可以有零到多个普通的参数:
|
def arithmetic_mean( * args): sum = 0 for x in args: sum + = x return sum print (arithmetic_mean( 45 , 32 , 89 , 78 )) print (arithmetic_mean( 8989.8 , 78787.78 , 3453 , 78778.73 )) print (arithmetic_mean( 45 , 32 )) print (arithmetic_mean( 45 )) print (arithmetic_mean()) |
以上实例输出结果为:
244
170009.31
77
45
0
希望本文所述对大家Python程序设计有所帮助。
原文链接:https://www.cnblogs.com/xiaofan130/p/8919781.html
您可能感兴趣
- python 暗弱目标提取(Python提取频域特征知识点浅析)
- pythonimage库(Python/Django后端使用PIL Image生成头像缩略图)
- 如何用python爬取最新电影(使用python实现抓取腾讯视频所有电影的爬虫)
- python微信红包代码(php生成微信红包数组的方法)
- python编程中冒号的用法(浅谈python中get pass用法)
- python开启两个线程(Python开启线程,在函数中开线程的实例)
- python给一个数字md5加密(Python写一个基于MD5的文件监听程序)
- python中如何遍历键(Python中按值来获取指定的键)
- python真的能高效处理excel报表吗(Python数据报表之Excel操作模块用法分析)
- python里面的print怎么用(python中的print输出)
- python爬虫入门代码(python爬虫基础教程:requests库二代码实例)
- python最火开源项目(5月份Github上Python开源项目排行)
- python模块光照强度(我如何用 CircuitPython 和开源工具监控温室)
- python 接口测试怎么校验json数据(python接口自动化十七--Json 数据处理---一次爬坑记详解)
- 微信昵称python(Python 微信之获取好友昵称并制作wordcloud的实例)
- python可视化界面实现管理系统(python面向对象实现名片管理系统文件版)
- 美国倒掉数十万加仑牛奶 上热搜第一,这一幕似曾相识(美国倒掉数十万加仑牛奶)
- 深度 倒牛奶 这一幕为何又在美国上演(深度倒牛奶)
- 美国数十万加仑牛奶倒下水道怎么回事 原因曝光令人心痛(美国数十万加仑牛奶倒下水道怎么回事)
- 探索中国神秘文字(探索中国神秘文字)
- 重温《蜗居》 宋思明选中海藻为红颜知己,纯属巧合,与爱无关(宋思明选中海藻为红颜知己)
- 越南旅游攻略(越南旅游攻略自由行)
热门推荐
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9