zoukankan      html  css  js  c++  java
  • python Basic usage

    __author__ = 'student'
    l=[]
    l=list('yaoxiaohua')
    print l
    print l[0:2]
    l=list('abc')
    print l*3
    l.append(4)
    print l
    l.extend('de')
    print l
    print l.count('a')
    l.sort()
    print l
    l.reverse()
    print l
    l[0:2]=[1,2,3]
    print l
    print list(map (ord,'spam'))
    l = ['abc', 'ABD', 'aBe']
    l.sort(key=str.lower) # sort in place not return new object, be ware of this
    print l
    print sorted(l, key=str.upper, reverse=True) #return new object not change l
    print l
    
    '''
    while True:
        reply=raw_input("please input what you want to say:")
        if reply=='exit':
            break
        else:
            print reply.upper()
    '''
    print 100**2
    a,b='A','B'
    print a
    print a,b
    
    L = [1, 2]
    M = L  # L and M reference the same object
    L = L + [3, 4]  # Concatenation makes a new object
    print L, M  # Changes L but not M
    L = [1, 2]
    M = L
    L += [3, 4]  # But += really means extend
    print L, M  # M sees the in-place change too!
    
    L = L.append(4)  # But append returns None, not L
    print(L)  # So we lose our list! None
    
    data = (123, 'abc', 3.14)
    for i, value in enumerate(data):
        print i, value
    
    import re
    m=re.match(r'd+','123:abc')
    if m is not None : print m.group()
    
    import random
    for i in range(1,10):
        print random.choice(xrange(10))
    
    l=[1,2]
    sum= lambda a,b:a+b
    print 'sum is :' , sum(*l) # parse the list to function parameters
    d={'a':1,'b':2}
    print 'sum is ', sum(**d) # ** parse the dictionary to function parameters
    
    with open(r'd:	est.txt','w') as file: # with clause the system will release resource automatically
        for x in range(0,26,1):
            file.write(chr(ord('a')+x))
            file.write('
    ')
    with open(r'd:	est.txt','r') as file:
        for line in file:
            print line.rstrip() # rstrip remove the end line in the string
  • 相关阅读:
    C++ 单例模式
    单链表快速排序
    美团后台面经
    排序算法及其优化总结
    (转)再谈互斥量与环境变量
    互斥锁和自旋锁
    算法题总结----数组(二分查找)
    Linux里的2>&1的理解
    Ubuntu下开启mysql远程访问
    说说eclipse调优,缩短启动时间
  • 原文地址:https://www.cnblogs.com/huaxiaoyao/p/4493232.html
Copyright © 2011-2022 走看看