python学习笔记(四)
来源:互联网 发布:矩阵连乘a1 a2 编辑:程序博客网 时间:2024/05/16 11:30
第十六讲 序列
1. 列表、元组和字符串的共同点:
a) 通过索引得到每一个元素
b) 索引从0开始
c) 可以通过分片的方法得到一个范围内的元素集合
d) 有很多共同的操作符
2. list方法,help(list)查看帮助
3. tuple方法help(tuple)查看帮助
4. str方法help(str)查看帮助
5. len()方法,返回长度
6. max(),返回参数最大值 《=》 min()
7. sum(a[,b]),可选参数表示是否求和再加上b
8. sorted()
9. reversed(),返回迭代器列表,使用list(reversed(a))转化为list
10. enumerate()枚举,可以使用list强制转换
11. zip(a,b),“打包“
第十七讲 函数
1. 定义:
def XXX():
函数体
2. 调用:
XXX()
第十八讲 函数参数
1. 形参和实参
2. 函数文档:
定义:写函数时候,处于单引号里面
def my(name,string1):
'这是一个函数文档测试'
#我是注释
print (name+string1)
调用:函数名.__doc__(此处一共四个下划线),或者使用help(函数名)查看
my.__doc__
help(my)
3. 关键字参数:
my(string=’hello’,name=’JX’)
4. 默认参数:
def my(name=‘Jx’,string1=‘hello’):
print (name+string1)
可以输入和不输入参数
5. 收集参数:
def test(*params)
print (‘参数的长度是:’,len(params))
print (‘第二个参数是:’,params[1])
params用元组打包
第十九讲 函数与过程
1. 区别:
(编程语言)函数,有返回值;过程,简单无返回值。
Python只有函数没有过程
2. 局部变量、全局变量:
def count(price,rate)
final_price= price * rate
returnfinal_price
old_price= float(input(“原价:”))
rate= float(input(“打折率:”))
new_price = count(old_price,rate)
print(“折后价格”,new_price)
函数内修改全局变量,python会自动定义一个同名的局部变量。也就是函数内无法修改全局变量。函数内只可读取全局变量的值。函数里使用global关键字先定义全局变量,然后才可以在函数内修改全局变量
第二十讲 内嵌函数和闭包
1. 支持函数嵌套:
def fun1()
print (“1”)
def fun2()
print(“2”)
fun2()
2. 闭包
def funx(x)
def funy(y)
return x * y
returnfuny
调用:1. z = funx(8), z(5)
2.z = funx(8)(5)
nonlocal使用:
def fun1()
x = 5
def fun2()
nonlocal x
x*=x
return x
return fun2()
没有nonlocal x时,由于x=5处于fun2()的外部作用域里,试图修改时,外部x会被屏蔽起来。Python 3之前,使用容器类型解决,如:x = [5],x[0] *=x[0] ;在python 3,使用nonlocal关键字
- Python学习笔记(四)
- python学习笔记(四)
- Python学习笔记(四)
- python学习笔记(四)
- Python学习笔记(四)
- python 学习笔记(四)
- python学习笔记(四)
- Python学习笔记(四)
- Python学习笔记(四)
- Python学习笔记(四)
- python学习笔记(四)
- python学习笔记(四)
- python学习笔记(四)
- python学习笔记(四)
- python学习笔记(四)
- Python学习笔记(四)
- Python学习笔记(四)
- Python学习笔记(四)
- Regular Expression Matching
- PHP中的排序函数sort、asort、rsort、krsort、ksort区别分析
- Windows Codepage: 936 (Simplified Chinese GBK)
- cookie作用
- 求职套题2---各大公司
- python学习笔记(四)
- 游戏服务器:到底使用UDP还是TCP
- Macro - KML数据:东西经,南北纬同时存在,负值转正值
- C++设计模式之外观模式
- leetcode第21题-Merge Two Sorted Lists
- js中的apply方法
- POJ 3276 Face The Right Way
- TEA加密算法 多语言实现——C
- .NET chart控件使用方法