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
原文地址:https://www.cnblogs.com/huaxiaoyao/p/4493232.html