python学习第五天 ----- 函数
1. 内置函数
2.自定义函数:
def funcname(parameter_list):
pass
import sys
sys.setrecursionlimit(100000)
def damage(skill1, skill2):
damage1 = skill1 * 3
damage2 = skill2 * 2 + 10
return damage1, damage2 damages = damage(3, 6)
print(type(damages))
//<class 'tuple'>
def damage(skill1, skill2):
damage1 = skill1 * 3
damage2 = skill2 * 2 + 10
return damage1, damage2 skill1_damage, skill2_damage = damage(3, 6)
print(skill1_damage, skill2_damage) //9 22
3.序列解包与链式赋值:
d = 1,2,3
print(d)
# (1, 2, 3)
# <class 'tuple'>
d = 1,2,3
a, b, c = d
a, b, c = 1,2,3
print(a, b, c)
# 1 2 3
a, b = [1, 2, 3]
#Traceback (most recent call last):
# File ".\c4.py", line 12, in <module>
# a, b = [1, 2, 3]
#ValueError: too many values to unpack (expected 2)
#a, b = [1, 2]
#print(a, b)
## 1 2 a, b = [1]
Traceback (most recent call last):
File ".\c4.py", line 12, in <module>
a, b = [1]
ValueError: not enough values to unpack (expected 2, got 1)
a = b = c = 1
print(a, b, c)
#1 1 1
4.必须参数与关键字参数:
必须参数:
def add(x, y):
result = x + y
return result
add(1)
# Traceback (most recent call last):
# File ".\c1.py", line 12, in <module>
add(1)
# TypeError: add() missing 1 required positional argument: 'y'
关键字参数:
可以指明我传入参数是谁,此时就不需要按照顺序去传入参数。
def add(x, y):
print(x, y) c = add(y = 3, x = 2)
# 2 3
默认参数:
def print_student_files(name, gender='男', age=22, college="华北水利水电大学"):
print('我叫' + name)
print('我今年' + str(age) + '岁了')
print('我是' + gender + '生')
print('我在' + college + '上学') print_student_files('鸡小萌', '男', 18, '人民路小学')
print('_______________________________________________')
print_student_files('五六七')
print('_______________________________________________')
print_student_files('果果', age = 17)
我叫鸡小萌
我今年18岁了
我是男生
我在人民路小学上学
_______________________________________________
我叫五六七
我今年22岁了
我是男生
_______________________________________________
我叫果果
我今年17岁了
我是男生
我在华北水利水电大学上学
print_student_files('果果', gender = '女', 17, college='牛津中学') File "c6.py", line 13
print_student_files('果果', gender = '女', 17, college='牛津中学')
^
SyntaxError: positional argument follows keyword argument
可变参数:
def demo(*param):
print(param)
print(type(param)) demo(1,2,3,4,5,6)
#结果:
#(1, 2, 3, 4, 5, 6)
#<class 'tuple'>
def demo(*param):
print(param)
print(type(param)) a = (1, 2, 3, 4, 5, 6)
demo(*a)
def demo(param1, *param, param2 = 2):
print(param1)
print(param2)
print(param) demo('a', 1,2,3, param2 = '3') #a
#3
#(1, 2, 3)
关键字可变参数:
def city_temp(**param):
print(param)
print(type(param))
pass city_temp(bj = '32', xm = '23', sh = '31') #{'bj': '32', 'xm': '23', 'sh': '31'}
#<class 'dict'>
def city_temp(**param):
for key, value in param:
print(key, ':', value) city_temp(bj = '32', xm = '23', sh = '31') #b : j
#x : m
#s : h
def city_temp(**param):
for key, value in param.items():
print(key, ':', value) city_temp(bj = '32', xm = '23', sh = '31') #bj : 32
#xm : 23
#sh : 31
def city_temp(**param):
for key, value in param.items():
print(key, ':', value) a = {'bj': '32c', 'sh':'31c'}
city_temp(**a) #PS F:\pythonlearn\Demo\eight> python .\c8.py
# bj : 32c
#sh : 31c
5.变量作用域:
此处可以看到与javascript中的作用域有很大的不同。先来看代码:
c = 50 def add(x, y):
c = x + y
print(c) add(1, 2)
print(c)
#3
#50
c = 10 def demo():
print(c) demo() #10
def demo():
c = 50 for i in range(0, 9):
a = 'a'
c += 1
print(c)
print(a) demo() #59
#a
6.作用域链:
c = 1 def func1():
c = 2
def func2():
c = 3
print(c)
func2() func1() //依次将 c = 3, c = 2这两行代码注释,得到打印的结果分别是3, 2, 1
7.global关键字:
def demo():
global c
c = 2 demo() print(c)
#2
import c10
print(c10.c) //2
import c10
print(c) //c是未定义,因此,这个c并不是在项目中全局
python学习第五天 ----- 函数的更多相关文章
- Python学习(五)函数 —— 内置函数 lambda filter map reduce
Python 内置函数 lambda.filter.map.reduce Python 内置了一些比较特殊且实用的函数,使用这些能使你的代码简洁而易读. 下面对 Python 的 lambda.fil ...
- Python学习笔记五,函数及其参数
在Python中如何自定义函数:其格式为 def 函数名(函数参数): 内容
- Python学习(五)函数 —— 自定义函数
Python 自定义函数 函数能提高应用的模块性,和代码的重复利用率.Python提供了许多内建函数,比如print()等.也可以创建用户自定义函数. 函数定义 函数定义的简单规则: 函数代码块以de ...
- python学习第五天--函数进阶
局部变量与全局变量下面代码中,old_price,rite为全局变量,final_price为局部变量 globals() 声明全局变量,在函数内可修改函数外的变量 内嵌函数:函数当中嵌套函数 闭包: ...
- python学习第五次笔记
python学习第五次笔记 列表的缺点 1.列表可以存储大量的数据类型,但是如果数据量大的话,他的查询速度比较慢. 2.列表只能按照顺序存储,数据与数据之间关联性不强 数据类型划分 数据类型:可变数据 ...
- Python学习第五堂课
Python学习第五堂课推荐电影:华尔街之狼 被拯救的姜哥 阿甘正传 辛德勒的名单 肖申克的救赎 上帝之城 焦土之城 绝美之城 #上节内容: 变量 if else 注释 # ""& ...
- Python学习笔记之常用函数及说明
Python学习笔记之常用函数及说明 俗话说"好记性不如烂笔头",老祖宗们几千年总结出来的东西还是有些道理的,所以,常用的东西也要记下来,不记不知道,一记吓一跳,乖乖,函数咋这么多 ...
- python学习交流 - 内置函数使用方法和应用举例
内置函数 python提供了68个内置函数,在使用过程中用户不再需要定义函数来实现内置函数支持的功能.更重要的是内置函数的算法是经过python作者优化的,并且部分是使用c语言实现,通常来说使用内置函 ...
- Python学习(六) —— 函数
一.函数的定义和调用 为什么要用函数:例如,计算一个数据的长度,可以用一段代码实现,每次需要计算数据的长度都可以用这段代码,如果是一段代码,可读性差,重复代码多: 但是如果把这段代码封装成一个函数,用 ...
随机推荐
- python爬虫scrapy框架
Scrapy 框架 关注公众号"轻松学编程"了解更多. 一.简介 Scrapy是用纯Python实现一个为了爬取网站数据.提取结构性数据而编写的应用框架,用途非常广泛. 框架的力量 ...
- 【HNOI】分数分解
题意描述 近来 IOI 专家们正在进行一项有关整数方程的研究,研究涉及到整数方程解集的统计问题,问题是这样的. 对任意的正整数 \(n\),我们有整数方程: \[\frac{1}{x_1}+\frac ...
- (六)HTTP和HTTPS(转)
一.HTTP和HTTPS的基本概念 HTTP:用于从WWW服务器传输超文本到本地浏览器的传输协议,它可以使浏览器更加高效,使网络传输减少. HTTPS:是以安全为目标的HTTP通道,简单讲是HTTP的 ...
- 在springmvc.xml中定义全局的异常处理
在Controller类的内部方法上使用@ExceptionHandler,则此类的方法抛出未处理的异常时,回到此方法上处理. @ExceptionHandler可以指定异常的类型,会自动进行匹配 如 ...
- How to using code post packingSlip on Quality Orders Form[AX2009]
For simple user operation posting packing slip with purchase order. we added a function button on Qu ...
- Pycharm激活码(2020最新永久激活码)
如果下边的Pycharm激活码过期失效了的话,大家可以关注我的微信公众号:Python联盟,然后回复"激活码"即可获取最新Pycharm永久激活码! 56NPDDVEIV-eyJs ...
- Python arange
原文来自DeniuHe.原文链接 >>> np.arange(3) array([0, 1, 2]) >>> np.arange(1,3,0.3) array([ ...
- tcp 保活定时器分析 & Fin_WAIT_2 定时器
tcp keepalive定时器 http server 和client端需要防止"僵死"链接过多!也就是建立了tcp链接,但是没有报文交互, 或者client 由于主机突然掉电! ...
- 使用iptables做端口转发
通过iptables可以做转发 #!/bin/sh IPT="/sbin/iptables" /bin/echo "1" > /proc/sys/net/ ...
- SQL SERVER数据库Left Join用法
Left Join基本语法: SQL LEFT JOIN 关键字 LEFT JOIN 关键字会从左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中没有匹配的 ...