zoukankan      html  css  js  c++  java
  • oldboy_09_03day

    1、split 分割

    2、内置函数  

    __name__ ------- 主动执行还是被其他程序调用; 本程序内 __name__ 值为 __main__ ;其他程序调用时,__文件名__

     __file__ ======当前文件路径、是文件的相对路径

    __doc__ ======当前程序说明文档

    3、函数 def、形式参数

    def test( a,*b):

        print a , b

    *b形参数 不限制参数个数 ---

    test('1', '2','3')

    * 代表可以参数一个元组(2,3)

    def test1(a,b,c):
    print a,b,c
    test1('1', '2', '3')

    def test2(a,b,*c):
    print a,b,c


    d=('1','2','3')

    f=test2('4', '5',d)
    k={'a':1,'b':2,'c':3}

    def test3(a,b,**c):
    print a,b,c

    test3(31, 32,c='1',d='2')

    def test(a,**b)

    ** 代表可以传入 一个字典;

    def 默认值

    def test5(a,b='test5')

      print a,b

    4、函数局部变量

    test1='ceshi' 

    def a():

      print test1

    def b():

      test1='insite ceshi'

    def c():

      golbal test1 #引用全局变量 test1  

    testoutsit='outsite_ceshi'

    def intest():

    print testoutsit

    def intest1():

    testoutsit='insitetest'
    print testoutsit

    def intest2():
    global testoutsit
    testoutsit='changeinstie'
    print testoutsit
    intest()
    intest1()
    intest2()
    print testoutsit



     5、return

    def back():

      for i in range(20):
          print i

          if i==10 :
          return i
      else:
          print i

    a=back()
    if a==10:
    print 'return ok!'

     return 整个函数跳出函数执行

    得到一个数值,或者返回一个结果

    一般return 写在结尾;

    #a=11>6 ? True:False   (三步运算符)

     ceshi='tr' if 2>1 else 'fl' 

    print ceshi

    和 if ,else  一样 

    if  11>6:

      a=Trun

    else:

      a=False

     =================

    6.1 lambda 函数

    lambda x,y:x+y

    6.2 with---as :

    with    as 方便打开文件 ,给了文件打开;和close,

    with open (file.txt ,'r') as f:   # with open 做了两个动作 1、打开文件 2、file.close 

    6.3、bool  布尔值 ;python 内置函数

    print bool('') - -------- print 为 flase

    print bool('date')  ----- print 为True 

    为空 布尔值 都为假;不为空  布尔值都为真

    6.4 yield 迭代器

    保存函数的执行状态;执行一半跳出;后可再进行执行

     大数据读取;

    def readfile():
    tel=0
    while True:
    with open('E:licence.py','r') as f:
    f.seek(tel)
    date=f.readline()
    if date:
    tel=f.tell()
    yield date
    else:
    return


    files=readfile()
    date1=files.next()
    date2=files.next()
    date3=files.next()
    date4=files.next()
    date5=files.next()
    print date1,date2,date3,date4,date5

    print '=================='
    date1=files.next()
    date2=files.next()
    date3=files.next()
    date4=files.next()
    date5=files.next()

    print date1,date2,date3,date4,date5
    print '====================='

    for i in files:
    print i

    6.5 range 、xrange效率更高 (xrange 也是迭代器,一行一行的返回,只记录开始和结尾)

    6.6 内置函数

    1、dir()

    显示当前文件中所有变量名称

    2、vars()

    当前文件中所有变量名称及值

    3、reload() 和import 都是引入别的文件;

    reload 重新加载模块 ;import 只执行一次;

    import   

    import

    reload

    reload

    当配置文件被修改;不重启服务时,重新reload

    4、id()

    id(2)或者 id(c)

    打印的是2或者c在内存中的地址

    5、cmp 进行比较

    cmp(2,10)小于 返回 ‘-1’

    cmp (10,2) 大于 返回 ‘1’

    cmp (10,10)  等于 返回 ‘0’

    6、abs 绝对值

    abs(-1)

    7、divmod()除 取商和余数

    divmod(10,4)

    8、max()

    max 列表也可以取最大

    max([1,2,3,4,6,20]) max(1,2,3,)

    9、min () 比较最小

    10、sum() 求和

    11、pow()幂

    pow(2,4)2的4次方

    12、len()统计字符串长度

    13、all()  全&,全部为真,才为真

    #web 前端登录  user pass info 全为真;为真

    print all('1','c',none)

    14、any() 只要有一个为真,就为真

    print any('1','c',none)

    15、chr()ASICC 码对应的位置

    16、ord()asics 码对应的数值

    17、hex()转化为16进制 oct()八进制bin()二进制

    0X是16进制的开始

    0 是8进制

    0b 是二进制的开始

    18、.format 

    19、zip 把列表列表压缩到列表中

     x=[1,2,3]

    y=[4,5,6]

    z=[7,8,9]

    zip(x,y,z)

    20、map()

    lamdba 匿名函数 可与定义 lambda x:x+1 ; 定义匿名函数x,函数意义 x+1 ;ruturn x+1值

    map(lambda x:x+1,[3,4,5]) 

    def fun(x):
    return x+1

    a=fun(2)
    print type(a)
    print map(fun,[20,30])

    map 遍历列表[3,4,5] 添加到函数 jia的中进行处理,并返回

    引用函数是引用函数的名字

    def fun1(x):
    return x+1

    c=[1,2,3,4,5,6,7,8,9,10]
    a=fun1(2)
    print type(a)
    print 'this is map %s'%(map(fun1,c))
    print 'this is map lambda %s'%(map(lambda x:x+1, c))

    22、filter()

    filter (lamdba x:x>5 ,[3,4,5,6,7,8] )

    def fun2(a):
    return a>6

    print 'this is filter %s' %(filter(fun2,c))

    print 'this is filter lambda%s'%(filter(lambda x:x>5, c))

    只是过滤;

    23、reduce()累计 加入下次计算

    reduce(lambda x,y:x+y ,[1,2,3,4,5,6])

    def fun3(x,y):
    return x+y

    print 'this is reduce %s' % (reduce(fun3, c))


    print 'this is reduce lambda %s'%(reduce(lambda x,y:x+y,c))

    只能传进去2个值

    24、random 随机验证码

    练习

    25、MD5 加密

    import md5
    ceshi=md5.new()
    print dir(md5) 查看md5的方法

    print vars(md5) 查看md5 的方法和值

    验证是,md5 加密一次 再去数据库验证

    import md5
    ceshi=md5.new()
    #print dir(md5)
    #print vars(md5)
    ceshi.update('userpasswd')
    print ceshi.hexdigest()

    26、re python正则表达式

    import re

    dir(re)

    a=‘ceshi,name,dfsdndsfsdnn’

    a1=re.match(‘c.*’,a)'.' 代表任意字符 ‘*’ 代表出现0次或者最多  # match 必须是字符串 开头匹配

    print a.group

    b=re.search('e.*',a) 整个字符串检索一遍;进行匹配

    c=re.search('e,*d',a) 匹配从e到d 

    d=re.findall('n.',a) 所有的全部查找 防到一个列表中;

    f=re.split ('n.',a) 字符串 按照表达式进行分割;剩下的放到一个表中

    g=re.sub(‘n.’,'ww',s)查找后替换 查找n 替换为ww;在s 中查找

    k=h[a-z]o

    k1=h[a|b|c]0 匹配 hao 或者hbo或者hco   ‘|’ 竖线 是或的意思

    正则表达式获取网址、ip、手机号、邮箱

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    import re
    import sys

    def getIPAddFromFile(fobj):
    regex = re.compile(r'(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)', re.IGNORECASE)
    ipadds = re.findall(regex, fobj)
    print ipadds
    return ipadds

    def getPhoneNumFromFile(fobj):
    regex = re.compile(r'1d{10}', re.IGNORECASE)
    phonenums = re.findall(regex, fobj)
    print phonenums
    return phonenums

    def getMailAddFromFile(fobj):
    regex = re.compile(r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}", re.IGNORECASE)
    mails = re.findall(regex, fobj)
    print mails
    return mails

    def getUrlFromFile(fobj):
    regex = re.compile(r"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+", re.IGNORECASE)
    urls = regex.findall(fobj)
    print urls
    return urls

    def main(FilefilePath):
    fobj = open(FilefilePath, 'rb').read()
    urllist = getUrlFromFile(fobj)
    mailList = getMailAddFromFile(fobj)
    phoneNum = getPhoneNumFromFile(fobj)
    ipaddlist = getIPAddFromFile(fobj)

    if __name__ == '__main__':
    main(sys.argv[1])

    ======================

    rn=r'[0-9]+(?:.[0-9]+){3}' #匹配IP地址 

    26、序列化和json

    内存中的数据保存

    字典等不能直接写入到文件中

    序列化  内存中的类型转化为字符串存储到硬盘;

        读入中通过序列化读取回来

    import pickle

    dir(pickle)

    pickle.dump

    pickle.load

    f=file('dump.txt','w')

    import os
    import pickle as p
    c={'a':1,'b':2,'c':3}
    #with open('dumpfile','wb') as f:

    f=open('dumpfile','w')

    p.dump(c,f)

    f.close()

    e=open('dumpfile','r')
    c=p.load(e)
    print c
    print os.system('dir')

    ======================

    内存数据交换 通过pickle.dumps;发送过去;对端pickle.load

    json 通用存储;各种编程语言都能读取;内存数据转化为字符串

    json 只能序列化常用的数据类型 (字符串、列表、字典、元组、等; ) python 的类 、函数、等不能被序列化

    unicode 和 utf-8 的相互转换

    s='abc'

    print type(s)

    s=u'abc'
    print type(s)
    s=s.encode('utf-8')
    print type(s)

  • 相关阅读:
    250 浅拷贝Object.assign(target, ...sources),深拷贝
    249 递归:概念,利用递归求1~n的阶乘,利用递归求斐波那契数列,利用递归遍历数据
    248 闭包:概念,作用,案例,思考题案例,chrome 中调试闭包
    247 高阶函数 之 函数可以作为参数传递
    246 JavaScript严格模式
    245 改变函数内部 this 指向:call,apply,bind,call、apply、bind 三者的异同
    244 函数内部的this指向:6种
    243 函数:函数的3种定义方式,函数的6种调用方式
    242 Object.defineProperty
    241 获取对象的属性名:Object.keys(对象)
  • 原文地址:https://www.cnblogs.com/zhzhao/p/4525505.html
Copyright © 2011-2022 走看看