python中show函数的用法_python学习笔记之——函数模块
1、函数参数说明:
def login(name,info,passwd = '123456')
函数参数可以有默认值,调用函数时:
1、如果只传二个参数,则有默认值的一定要放到最后;
def login(name,passwd = '123456',info='welcome to you')
2、如果传二个参数,一定要指明形参;
login(user)
login(user,passwd)
login(user,info='欢迎')
login(user,info='欢迎',passwd='123456')
def login(*arg) 函数参数个数不定,传参数的时候可以多个,包装为列表;例:
def list_show(*args):
for name in args:
print(name)
list_show('a','b','c','d',1,2,3,4) #传多个参数
list_show(['a','b','c',1,2,3]) #直接传个列表参数
def login(**arg) 函数参数个数不定,传参数的时候可以多个,包装为字典;例:
def dict_show(**args):
for name in args.items():
print(name)
dict_show(name='kevin',age=23) #传多个参数时,格式为key=value
print('##################')
dict = {'name':'kevin','age':23}
dict_show(**dict) #直接传个字典参数时,字典前加二个**
2、生成器yield:
for i in range(1000): pass
会导致生成一个 1000 个元素的 List,而代码:for i in xrange(1000): pass
则不会生成一个 1000 个元素的 List,而是在每次迭代中返回下一个数值,内存空间占用很小。因为 xrange 不返回 List,而是返回一个 iterable 对象。
同样类似于文件读行的readlines和xreadlines。
带有 yield 的函数在 Python 中被称之为 generator(生成器),例:
def kevin_readlines():
seek = 0
while True:
with open('D:/eclipse/workspace/python27/first_python27/file/file.txt','r') as f:
#with打开文件无需使用close来结束读取文件
f.seek(seek)
data = f.readline()
if data:
seek = f.tell()
yield data
else:
return #直接返回,退出函数
for line in kevin_readlines():
print(line)
yeild生成器需要用循环来一行一行读取。yeild经常用于读取函数的中间值,适合用于多线程之类的情况。使用next来一次一次调用函数值。
3、函数介绍:
★lamba
python lambda是在python中使用lambda来创建匿名函数,而用def创建的方法是有名称的,除了从表面上的方法名不一样外,python lambda还有哪些和def不一样呢?
1 python lambda会创建一个函数对象,但不会把这个函数对象赋给一个标识符,而def则会把函数对象赋值给一个变量。
2 python lambda它只是一个表达式,而def则是一个语句。
lambda语句中,冒号前是参数,可以有多个,用逗号隔开,冒号右边的返回值。lambda语句构建的其实是一个函数对象。
例:
m = lambda x,y,z: (x-y)*z
print m(234,122,5)
也经常用于生成列表,例:
list = [i ** i for i in range(10)]
print(list)
list_lambda = map(lambda x:x**x,range(10))
print(list_lambda)
★ enumerate(iterable,[start]) iterable为一个可迭代的对象;
enumerate(iterable[, start]) -> iterator for index, value of iterable Return an enumerate object. iterable must be another object that supports iteration. The enumerate object yields pairs containing a count (from start, which defaults to zero) and a value yielded by the iterable
argument. enumerate is useful for obtaining an indexed list: (0, seq[0]), (1, seq[1]), (2, seq[2]), ...
例:
for k,v in enumerate(['a','b','c',1,2,3],10):
print k,v
★S.format(*args, **kwargs) -> string 字符串的格式输出,类似于格式化输出%s
Return a formatted version of S, using substitutions from args and kwargs.
The substitutions are identified by braces ('{' and '}').
s = 'i am {0},{1}'
print(s.format('wang',1))
★map(function,sequence) 将squence每一项做为参数传给函数,并返回值
例:
def add(arg):
return arg + 101
print(map(add,[12,23,34,56]))
★filter(function or None, sequence) -> list, tuple, or string 返还true的序列
Return those items of sequence for which function(item) is true. If
function is None, return the items that are true. If sequence is a tuple
or string, return the same type, else return a list.
例:
def comp(arg):
if arg < 8:
return True
else:
return False
print(filter(comp,[1,19,21,8,5]))
print(filter(lambda x:x % 2,[1,19,20,8,5]))
print(filter(lambda x:x % 2,(1,19,20,8,5)))
print(filter(lambda x:x > 'a','AbcdE'))
★reduce(function, sequence[, initial]) -> value 对二个参数进行计算
Apply a function of two arguments cumulatively to the items of a sequence,
from left to right, so as to reduce the sequence to a single value.For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates((((1+2)+3)+4)+5). If initial is present, it is placed before the
items of the sequence in the calculation, and serves as a default when the sequence is empty.
例:
print(reduce(lambda x,y:x*y,[22,11,8]))
print(reduce(lambda x,y:x*y,[3],10))
print(reduce(lambda x,y:x*y,[],5))
★zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)] 将多个序列转化为新元祖的序列
Return a list of tuples, where each tuple contains the i-th element from each of the argument sequences. The returned list is truncated in length to the length of the shortest argument sequence.
例:
a = [1,2,3,4,5,6]
b = [11,22,33,44,55]
c = [111,222,333,444]
print(zip(a,b,c))
★eval(source[, globals[, locals]]) -> value将表达式字符串执行为值,其中globals为全局命名空间,locals为局部命名空间,从指字的命名空间中执行表达式,
Evaluate the source in the context of globals and locals. The source may be a string representing a Python expression or a code object as returned by compile(). The globals must be a dictionary and locals can be any mapping, defaulting to the current globals and locals. If only globals is given, locals defaults to it.
例:
a = '8*(8+20-5%12*23'
print(eval(a))
d = {'a':5,'b':4}
print(eval('a*b',d))
★exec(source[, globals[, locals]]) 语句用来执行储存在字符串或文件中的Python语句
例:
a = 'print("nihao")'
b = 'for i in range(10): print i'
exec(a)
exec(b)
★execfile(filename[, globals[, locals]])
Read and execute a Python script from a file.The globals and locals are dictionaries, defaulting to the currentglobals and locals. If only globals is given, locals defaults to it.
4、常用模块:
★random 生成随机数
print random.random() 生成0-1之间的小数
print random.randint(1,3) 生成整数,包含endpoint
print random.randrange(1,3,2) 生成整数,不包含endpoint
randrange(self, start, stop=None, step=?)
生成5位随机数,例:
import random
a = []
for i in range(5):
if i == random.randint(1,5):
a.append(str(i))
else:
a.append(chr(random.randint(65,90)))
else:
print(''.join(a))
★生成MD5码
例:
一. 使用md5包
import md5
src = 'this is a md5 test.'
m1 = md5.new()
m1.update(src)
print m1.hexdigest()
二. 使用hashlib
import hashlib
hash = hashlib.md5()
hash.update('this is a md5 test.')
hash.update('admin')
print(hash.digest())
print(hash.hexdigest())
推荐使用第二种方法。
对以上代码的说明:
1.首先从python直接导入hashlib模块
2.调用hashlib里的md5()生成一个md5 hash对象
3.生成hash对象后,就可以用update方法对字符串进行md5加密的更新处理
4.继续调用update方法会在前面加密的基础上更新加密
5.加密后的二进制结果
6.十六进制结果
如果只需对一条字符串进行加密处理,也可以用一条语句的方式:
print(hashlib.new("md5", "Nobody inspects the spammish repetition").hexdigest())
5、python对象与文件之间的序列化和反序列化(pickle和json)
★pickle模块用来实现python对象的序列化和反序列化。通常地pickle将python对象序列化为二进制流或文件。
python对象与文件之间的序列化和反序列化:
pickle.dump()
pickle.load()
如果要实现python对象和字符串间的序列化和反序列化,则使用:
pickle.dumps()
pickle.loads()
可以被序列化的类型有:
* None,True 和 False;
* 整数,浮点数,复数;
* 字符串,字节流,字节数组;
* 包含可pickle对象的tuples,lists,sets和dictionaries;
* 定义在module顶层的函数:
* 定义在module顶层的内置函数;
* 定义在module顶层的类;
* 拥有__dict__()或__setstate__()的自定义类型;
注意:对于函数或类的序列化是以名字来识别的,所以需要import相应的module。
例:
import pickle
data = {
'a': [1, 2.0, 3, 4+6j],
'b': ("character string", "byte string"),
'c': set([None, True, False])
}
du = pickle.dumps(data)
print(pickle.loads(du))
print(du)
with open('data.pickle', 'wb') as f:
pickle.dump(data, f)
with open('data.pickle', 'rb') as f:
data = pickle.load(f)
print(str(data))
★JSON(JavaScript Object Notation):一种轻量级数据交换格式,相对于XML而言更简单,也易于阅读和编写,机器也方便解析和生成,Json是JavaScript中的一个子集。
Python的Json模块序列化与反序列化的过程分别是 encoding和 decoding
encoding:把一个Python对象编码转换成Json字符串
decoding:把Json格式字符串解码转换成Python对象
具体的转化对照如下:
loads方法返回了原始的对象,但是仍然发生了一些数据类型的转化。比如,上例中‘abc’转化为了unicode类型。从json到python的类型转化对照如下:
例:
import json
data = { 'a': [1, 2.0, 3, 4], 'b': ("character string", "byte string"), 'c': 'abc'}
du = json.dumps(data)
print(du)
print(json.loads(du,encoding='ASCII'))
with open('data.json','wb') as f:
json.dump(data,f)
with open('data.json','rb') as f:
data = json.load(f)
print(repr(data))
经测试,2.7版本导出的json文件,3.4版本导入会报错:TypeError: the JSON object must be str, not 'bytes'
6、正则表达式模块
★re.match的函数原型为:re.match(pattern, string, flags)
第一个参数是正则表达式,这里为"(\w+)\s",如果匹配成功,则返回一个Match,否则返回一个None;
第二个参数表示要匹配的字符串;
第三个参数是标致位,用于控制正则表达式的匹配方式,如:是否区分大小写,多行匹配等等。
★re.search的函数原型为: re.search(pattern, string, flags)
每个参数的含意与re.match一样。
re.match与re.search的区别:re.match只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回None;而re.search匹配整个字符串,直到找到一个匹配。
★re.findall可以获取字符串中所有匹配的字符串。如:re.findall(r'\w*oo\w*', text);获取字符串中,包含'oo'的所有单词。
★re.sub的函数原型为:re.sub(pattern, repl, string, count)
其中第二个函数是替换后的字符串;本例中为'-'
第四个参数指替换个数。默认为0,表示每个匹配项都替换。
re.sub还允许使用函数对匹配项的替换进行复杂的处理。如:re.sub(r'\s', lambda m: '[' + m.group(0) + ']', text, 0);将字符串中的空格' '替换为'[ ]'。
★re.split可以使用re.split来分割字符串,如:re.split(r'\s+', text);将字符串按空格分割成一个单词列表。
★re.compile可以把正则表达式编译成一个正则表达式对象。可以把那些经常使用的正则表达式编译成正则表达式对象,这样可以提高一定的效率。下面是一个正则表达式对象的一个例子:
例:
import re
r = re.compile('\d+')
r1 = r.match('adfaf123asdf1asf1123aa')
if r1:
print(r1.group())
else:
print('no match')
r2 = r.search('adfaf123asdf1asf1123aa')
if r2:
print(r2.group())
print(r2.groups())
else:
print('no match')
r3 = r.findall('adfaf123asdf1asf1123aa')
if r3:
print(r3)
else:
print('no match')
r4 = r.sub('###','adfaf123asdf1asf1123aa')
print(r4)
r5 = r.subn('###','adfaf123asdf1asf1123aa')
print(r5)
r6 = r.split('adfaf123asdf1asf1123aa',maxsplit=2)
print(r6)
注:re执行分二步:首先编译,然后执行。故先使用re.compile进行查询的字符串进行编译,之后的操作无需在次编译,可以提高效率。
匹配IP具体实例:
ip = '12aa13.12.15aasdfa12.32aasdf192.168.12.13asdfafasf12abadaf12.13'
res = re.findall('(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})',ip)
print(res)
res1 = re.findall('(?:\d{1,3}\.){3}\d{1,3}',ip)
print(res1)
而group,groups 主要是针对查询的字符串是否分组,一般只是针对search和match,即'\d+' 和('\d+') 输出结果为:
123 和('123',)。
7、time模块
time模块提供各种操作时间的函数
import time
#1、时间戳 1970年1月1日之后的秒
#3、元组 包含了:年、日、星期等... time.struct_time
#4、格式化的字符串 2014-11-11 11:11
print time.time()
print time.mktime(time.localtime())
print time.gmtime() #可加时间戳参数
print time.localtime() #可加时间戳参数
print time.strptime('2014-11-11', '%Y-%m-%d')
print time.strftime('%Y-%m-%d') #默认当前时间
print time.strftime('%Y-%m-%d',time.localtime()) #默认当前时间
print time.asctime()
print time.asctime(time.localtime())
print time.ctime(time.time())
import datetime
'''
datetime.date:表示日期的类。常用的属性有year, month, day
datetime.time:表示时间的类。常用的属性有hour, minute, second, microsecond
datetime.datetime:表示日期时间
datetime.timedelta:表示时间间隔,即两个时间点之间的长度
timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]])
strftime("%Y-%m-%d")
'''
import datetime
print datetime.datetime.now()
print datetime.datetime.now() - datetime.timedelta(days=5)
python中show函数的用法_python学习笔记之——函数模块相关推荐
- python中setup函数的用法_python学习之setUp函数和tearDown函数
1,setUp():就是在一个类中最先被调用的函数,每次执行一个函数都要先执行这个函数,有几个函数就被调用几次,与放的位置无关,随便放到哪里都会先执行这个函数 2,tearDown():就是在一个类中 ...
- python中while循环的格式_Python 学习笔记——for循环和while循环
文章目录 一.for循环 1.for循环的格式 2.for循环遍历字典 3.range()函数 for循环配合range()函数 二.while循环 while循环的格式 break while循环 ...
- python中的命令行参数_python学习笔记6:命令行参数
1. 使用步骤 使用步骤如下: # 导入模块 import argparse # 获取parser, description文本会显示在help信息中 parser = argparse.Argume ...
- python函数是一段具有特定功能的语句组_Python学习笔记(五)函数和代码复用
本文将为您描述Python学习笔记(五)函数和代码复用,具体完成步骤: 函数能提高应用的模块性,和代码的重复利用率.在很多高级语言中,都可以使用函数实现多种功能.在之前的学习中,相信你已经知道Pyth ...
- matlab 调用子函数返回值,matlab学习笔记13_1 函数返回值
一起来学matlab-matlab学习笔记13函数 13_1 函数返回值 觉得有用的话,欢迎一起讨论相互学习~Follow Me 函数返回一个值 返回值不必使用return语句,而是直接将需要返回的变 ...
- python中while的用法_Python学习笔记之While循环用法分析
本文实例讲述了Python学习笔记之While循环用法.分享给大家供大家参考,具体如下: 前面一篇<Python学习笔记之For循环用法>详细介绍了Python for循环,这里再来讲述一 ...
- python中如何查一个函数的用法_Python常见内置函数用法(三)
本文作者:孙雪丽 文字编辑:周聪聪 技术总编:张学人重大通知!!!2019年6月22日至24日在河南郑州举行Stata编程技术特别训练营,招生工作已经圆满结束.另外爬虫俱乐部将于2019年7月5日至7 ...
- python中continue用法_Python学习笔记之Break和Continue用法分析
本文实例讲述了Python学习笔记之Break和Continue用法.分享给大家供大家参考,具体如下: Python 中的Break 和 Continue break:控制何时循环应该结束 conti ...
- python什么时候需要加引号_Python学习笔记(八)-Python中的引号用法总结
Python中的引号: 1.表示多行注释--一对三个单引号或双引号表示多行注释 1 #-*-coding :utf-8-*- #防止乱码,方便在程序中添加中文,把编码统一成UTF-8 2 from s ...
- python elseif用法_python学习笔记(一)
0.前言 本文是根据廖雪峰python教程学习总结而来. 参考<python学习手册>,<流畅的python>以及python官方手册等网络资料 略过了与C和C++ 语言相同的 ...
最新文章
- 部署篇01:Linux 安装配置JDK
- python pipe stdout 实现cat|grep 功能
- jQuery选择器之可见性过滤选择器Demo
- Core Data(3)- 使用绑定
- 10---Net基础加强
- python把汉字转换为二进制数_在Python中,如何将8位二进制数转换为ASCII字符?
- 干货:如何正确描述存储IO类型?
- dup、dup2、fcntl
- php mysql长连接聊天室_PHP之探索MySQL 长连接、连接池
- MySQL下xtrabackup与MTS造成的死锁
- Source Insight 中查看日文注释
- Java RMI之介绍
- python对写作有什么帮助_第47p,Python基础知识,写作总结
- C++指针delete后赋值为NULL的一些问题
- 服务器硬盘数据备份到nas,群晖NAS教程第五节:如何备份 Synology NAS
- 推荐一些2021年整理的PHP毕业设计、毕设参考作品案例
- Linux/Ubuntu 98版五笔安装
- 服务器安装esxi系统
- 利用特性、泛型、反射生成sql操作语句(待修改
- 最好用最清爽的json在线编辑器
热门文章
- b站投稿 您的稿件未能成功转码。原因:该视频时长不足1秒,请检查视频时长并尝试重新上传。解决办法
- Google官方架构MVI
- 机器学习原理与实战 | SVM(支持向量机)实践
- 2012-7-19可樂词汇积累#9316;
- windows 7 64位 刷J-Link固件 无法识别成串口时 安装 sam-ba at91 usb to serial converter 驱动不成功的解决办法
- 软件著作权登记的流程步骤及申报资料整理攻略
- 字节跳动“车轮”收割
- css中实现子父元素高度自适应子元素高度
- 苹果CEO库克亚洲之行总结 未来挑战远大于机遇
- Kettle (1) - 读取 CSV 文件