python学习小结1

1 使用idel新建立程序后,保存运行,CTRL+F5即可运行 

2  if (time==12): 
      print 'hello' 
else: 
     pring 'dsdsd' 

//注意冒号! 

3 包、模块的命名规则:全部以小写字母形式命名 

4 类名首字母大写,其他字母小写;私有属性和私有方法用两个下划线做前缀,SELF=JAVA中的this 

5 函数命名规则:函数名必须下划线或者字母开头,可以包含任意的数字,字母和下划线组合,函数名 
区分大小写,函数名不能保留字 

6 注释用 #号,中文注释  #_*_ coding:UTF-8 _*_ 

7 PYTHON不强制使用分号 

8 PYTHON类型 
   1) 整型 
      分别为布尔型(0:FALSE,1:TRUE),长整型和标准整数类型;长整型相当于JAVA.BIGINTEGER,在后 
面加L表示 
    

   2)双精度类型,E记数,相当于c的double 
   3)复数,比如0+2j,其中num.real,num.imag分别返回其实部和虚部,num.conjugate(),返回其共扼复数对象 
  
   
9 PYTHON不支持i++,i--的运算 

10 全局变量,使用global 关键字 

11 字符串知识 
   1)‘hello'  --//输出'hello'  原样输出hello 
   2)  "hello"   //输出"hello" 
   3) "what 's your name"    输出"what's your name 
   4)三引号:其中的字符串可以任意使用单引号和双引号 
   >>> '''what's your name,'this is a pig' ''' 
  输出:"what's your name,'this is a pig' " 

   5)也可以转义,比如 
      >>>'what \'s your name' 
    6) 字符串连接用+号 
     7)print str('hello')  //输出hello,将其转为字符串的形式 

   8)字符串的输入函数   input(),把输入数据改为默认的python表达 
式,而raw_input()是将读入的数据转换为字符串 
     比如raw_input('我的年龄') 

12 整除 
    3/6=0,这时因为两个整数除的时候,python 2.5会对结果进行截取,只保留 
整数部分。 
   3.0/6.0=0.5  ,如果参与除法个数中有一个是浮点数字,则是真正除(5。0/6) 
  from __future__ import division 
真除法:5/6=0.83333      
而//叫地板除  用于两个数的整除,比如5//6=0 
  **乘方运算 
   3**3=27 


13 falas,none,0,空字符串,空元素,空列表及空字典都是假(0),而其他值都是真值‘ 

14 if语句 
   if  xxxx: 
print '' 
elif xxxx: 
print 'xxxx' 
elif xxx: 
print 'xxxx' 
  注意是elif 

15 while语句 
      while i<=5: 
      ....... 
      else: 
        ........... 

16 for循环 
   for target in object: 
     .... 
     if  xxxx: 
     break 
if   xxxxx: 
   continue 
else: 
    ....... 

17 迭代器 
    mab=['a','b','c','d'] 
    my=iter(mab) 
  
    print my.next() 
     print my.next() 
    
   
  并行迭代: 
   names=[a','b','c','d'] 
   ages=[2,4,5,7] 
   for name,age in zip(names,ages) : 
       print name,'的年龄是',age 

   zifu=raw_input('输入您要查询鲜花的名称:') 
shujus=['长春花','珍珠花','向日葵','风铃草','金盏菊','含羞草','夹竹桃','大丽花','金雀花','野蔷薇','桔梗花'] 
for index,shuju in enumerate(shujus): 
if zifu in shuju:
print shuju 

   //其中enumerate为迭代器 

18 while True: 
    word=raw_input('input name') 
    if not word: 
    break 
     .......... 
19 PASS语句 
   什么也不做 

20 DEL语句 
    删除名称本身 
    name='abc' 
     del name 
   print name 
   
  动态执行python代码 
     exec "print 'hello'" 
    

21 PYTHON程序结构 
    由包,模块和函数组成 
22 
  # 函数的定义 
def login (username = "maxianglin" , password = "maxianglin"): 
    if(username == 'admin') and (password == 'admin'): 
        print "登录成功!" 
    else: 
        print "登录失败" 
login('admin','admin') 
login('admin') 
login(password='admin') 
login() 

23 注意PYTHON 2下,WINDOWS下的话,里面有中文的话,将其保存为GBK编码再运行,PYTHON3则没这个问题。 

24 函数中的可变长度参数值 
     比如def login (* userpwds): 
          username=userpwds[0]; 
          ................... 
      调用login('a','b') 
25 方法需要返回值的话,则只需要return 即可 
26  方法需要返回多个值,可以使用元组的方法 
     比如def  abc(x,y,z) 
           x=x+5 
           y=y+4 
           z=z+6 
         opera=[x,y,z] 
         numbers=tuple(opera) 
         return numbers 

27 模块的创建 

       abc.py 
       def test(): 
       ....... 

调用方: 
      import  abc 
       ab.test(); 

//或者from abc import test 
      from abc import *  //导入所有模块      

  python的import可以放在任何位置 


28 # -*- coding: UTF-8 -*- 
import sys 

print updatePwd.updatePassword().decode('UTF-8').encode(type) 
        用这里调用结果用decode转换为UTF-8了 


29 模块中的属性 
    1) __name__属性 :用来判断当前模块是不是程序的入口,如果是入口,则返回__main__ 

    2) __DOC__:每个对象都会有__doc__,用来描述注释作用,比如 
      class myclass: 
           'adsdfsdfsdf' 
           ............... 
       print myclass.__doc__  //输出adsdfsdfsdf 

30) 内设的模块函数 
    1、apply()函数 
      def login (username , password): 
    msg = '' 
    if(username == 'admin') and (password == 'admin'): 
        msg = '登录成功' 
    else: 
        msg = '登录失败' 
    return msg 
print apply(login,('admin','admin')) 
    把函数的参数存放在一个元组或序列中 

   2 filter()函数 
     用函数来过滤序列,把序列的每一个项传递到过滤函数中去,如果函数返回TRUE则过滤,并一次性返回 
其结果,如果过滤函数返回的结果为FALSE,则从列表中删除该项目。 
        def validate (usernames): 
    if (len(usernames) > 4) and (len(usernames) < 12): 
        return usernames 
print filter(validate , ('admin','maxianglin','mxl','adm','wanglili')) 

     将('admin','maxianglin','mxl','adm','wanglili')传进去进行过滤 

   3 reduce函数 
       def operat (x , y): 
    return x*y 

print reduce(operat , (1,2,3,4,5,6)) 
print reduce(operat , (7,8,9) , 5) 
    其中,对(1,2,3,4,5,6)进行每一个的连乘; 

    4 map函数 
      对多个序列中的每个元素执行相同的操作,并返回一个与输入序列长度相同的列表。 
    def add1(a):   
    return a + 1  
def add2(a, b):   
    return a + b   
def add3(a, b, c):   
    return a + b + c 
    
a1 = [1,2,3,4,5]   
a2 = [1,2,3,4,5]   
a3 = [1,2,3,4,5]   
  
b = map(add1, a1)   
print b   
b = map(add2, a1, a2)   
print b   
b = map(add3, a1, a2, a3) 
print b 

  分别输出:[2, 3, 4, 5, 6] 
[2, 4, 6, 8, 10] 
[3, 6, 9, 12, 15] 


31 LIST 
  1)   userList = ['0001' , '0002' , '0003' , '0004' , '0005'] 
print '目前有学生'+str(len(userList))+'个' 
print '刚来一个学生' 
userList.append('0006') 
print '现有学生'+str(len(userList))+'个,他们是:' 
for item in userList: 
    print item 
   2) insert方法 
       userList = ['0001' , '0002' , '0006' , '0004' , '0005'] 
print '初始化的userList列表为:'+str(userList) 
userList[2] = '0003' 
print '更新后的userList列表为:'+str(userList) 
     //注意序列从0开始算 

  3) 删除元素remove 
         userList = ['0001' , '0002' , '0003' , '0004' , '0005'] 
print '初始化的userList列表为:'+str(userList) 
#userList.remove('0003') 
   4)del 
     del userlist[1] 
   5) 分片赋值 
       userList = list('Python') 
userList[1:] = list('rite') 
print userList 
    这里用冒号分片,从第3个元素开始到结束,改为rity,输出pyrite 

    
   6)使用负索引访问列表元素 
         userList = ['0001' , '0002' , '0003' , '0004' , '0005' , '0006'] 
print userList[-2] 
   最尾部元素为-1,-2即输出005 
  7) 列表分片 
      userList = ['0001' , '0002' , '0003' , '0004' , '0005' , '0006'] 
subUser1= userList[2:5] //输出索引2,3,4号位的元素,不包括索引为5的元素 

subUser2= userList[-3:-1] 
subUser3= userList[0:-2] //输入不0开始,不包括-2元素位(即0005)的所有元素 
print subUser1 
print subUser2 
print subUser3 

   ['0003', '0004', '0005'] 
['0004', '0005'] 
['0001', '0002', '0003', '0004'] 
    

   
8) 二元表 
    有点象数组: 
      userList1=['0001' , '0002' , '0003' , '0004'] 
userList2=['0005' , '0006' , '0007'] 
userList=[userList1 , userList2] 
   则userList[0][1]=0002 
  列表可以连接: 
   userList1=['0001' , '0002' , '0003' , '0004'] 
userList2=['0005' , '0006' , '0007'] 
userList1.extend(userList2) 
print userList1 
   将userLIst2加到userList1后去 
     userList6= ['0015' , '0016']*2  //将其中的元素添加一杯  ['0015','0016','0015','0016'] 


9 ) 列表的查找,排序和反转 
   查找:userList = ['0001' , '0002' , '0003' , '0004' , '0005' , '0006'] 
print '元素0002对应的索引值为:',userList.index('0002') 
   排序:userList = ['0001' , '0004' , '0006' , '0002' , '0005' , '0003'] 
userList.sort(reverse=True) 
print userList 
   //默认是升序,如果设置参数reverse=true,则为降序 
   反转:userList = ['0001' , '0004' , '0006' , '0002' , '0005' , '0003'] 
userList.reverse() 

32 POP操作:userlist.pop() //弹出 
   append:入栈操作  
     
33 不可变序列:元组 
   1) 创建元组,创建时可以不指定元素的个数,相当于不定长的数组,但一旦创建就不能修改元组的长度。 
  如果只有一个数的元组,则必须指定为比如(42,),注意后面加上一个逗号 
    元组:  user=('1','2','3','4','5')  ,元组的长度不可变,同样索引从0开始 

   2) 添加元组 
     userTuple = ('0001'  , '0002' , '0003' , '0004' , '0005' , '0006') 
new_userTupele=(userTuple , '0007' , '0008') 
  3 )元组中的元素不能修改 
  4) 同样支持分片 
      userTuple = ('0001'  , '0002' , '0003' , '0004' , '0005' , '0006') 
print '元组中的第3个元素为:',userTuple[2] 
print '元组中倒数第3个元素为:',userTuple[-3] 
print '元组中第3个元素到倒数第2个元素组成的元组为:',userTuple[2:-1] 

  5) 元组可以进行解包操作 
       userTuple = ('0001' , '0002' , '0003') 
stu1 , stu2 , stu3 = userTuple 
print stu1 
print stu2 
print stu3 
  6)元组的遍历 
      for range的用法 
    userTuple = ('0001' , '0002' , '0003' , '0004' , '0005' , '0006') 
for item in range(len(userTuple)): 
    print userTuple[item] 
  还可以用MAP 
    userTuple1 = ('0001' , '0002' , '0003') 
userTuple2 = ('0004' , '0005' , '0006') 
userTuple=(userTuple1 , userTuple2) 
for item in map(None , userTuple): 
    for i in item: 
        print i 
      MAP中的NONE时,则MAP返回其后面的元组 

34 字典 
     userDic = [(2,'maxianglin'),(1,'wanglili'),(3,'malinlin')] 
dic_userDic = dict(userDic) 
print dic_userDic 
  输出为:{1:'wanglili',2:'maxlinlin',3:'malinlin'} 
   或者usera={'0000':'sssss','ssss':'tom'} 
  操作: 
   1) 字典添加元素 
     字典是无序的,没append方法,向其调用增加一个元素,用setdeffault()方法 
   userDic={'0001':'maxianglin','0002':'wanglili','0003':'malinlin'} 
userDic.setdefault('0004','zhuhongtao') 
print userDic 
userDic.setdefault('0001','zhangfang') //重复元素,添加失败,依然是原来的 
print userDic 
    也可以直接改值  userDic['0004']='ddddd' 
   删除值: 
     del(userdic['0002']); 
  也可以  userdic.pop 
       
    2) 字典的遍历 
       for in遍历 
   userDic={'0001':'maxianglin','0002':'wanglili','0003':'malinlin'} 
#for key in userDic: 
    #print 'userDic[%s]='% key,userDic[key] 
print userDic.items() 
      还有用items方法遍历字典,比如 
   userDic={'0001':'maxianglin','0002':'wanglili','0003':'malinlin'} 
for (key,value) in userDic.iteritems(): 
    print 'userDic[%s]='% key,value 
     还有iterkeys()和itervalues, 
      for key in userDic.iterkeys(): 
    print key 
for value in userDic.itervalues(): 
    print value 
     
for (key ,value) in zip(userDic.iterkeys(),userDic.itervalues()): 
    print 'userDic[%s]='% key,value 

   3) 字典的基础方法 
     clear():清除字典里的所有项  userDic.clear() 
     copy()方法:userDic.copy() 
     fromkeys()方法:  
       source_userDic= {'0001':'maxianglin','0002':'wanglili','0003':'malinlin'} 
print {}.fromkeys(['0001','0002']) 
print source_userDic.fromkeys(['0001','0002']) 
print source_userDic.fromkeys(['0001','0002'],'maxianglin') 
  其中fromkeys中第一个参数是新的字典的列表,第2个参数是值 
     {'0001': None, '0002': None} 
{'0001': None, '0002': None} 
{'0001': 'maxianglin', '0002': 'maxianglin'} 

 4) 字典的get方法 
     userDic= {'0001':'maxianglin','0002':'wanglili','0003':'malinlin'} 
   print userDic.get('0002') 
    //当get访问不存在的值的时候,返回的是NONE,不会有异常 
 5) has_key()方法 
    找字典中是否有该键,如果有返回true. 
       userDic.has_key('abc') 
  6)popitem()方法 
    弹出栈,userDic.popitem() 
7) update方法,用一个字典更新另外一个字典 
    userDic= {'0001':'maxianglin','0002':'wanglili','0003':'malinlin'} 
newDic={'0002':'zhangfang'} 
userDic.update(newDic) 
print userDic 
   则更新0002的值 
  

35  序列 
   numbers=[0,1,2,3,4,5,6,7,8,9] 
      numbers[3:6] ,表示从第3个元素开始,但不包括第6个元素的值,输出3,4,5 
      numbers[7:]  则取到最后,从第7个开始,即7,8,9 
   更大的步长 
   又如numbers[0:10:1] ,指定了步长为1,默认为1,输出[0,1,2,3,4,5,6,7,8,9] 

   如果设置步长为2,则跳过元素 
   [0,2,4,6,8] 
   还可以number[::3],只有步长,输出[0,3,6,9] 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值