标签:
一.类
1 #coding=utf-8 2 class Animal(object): 3 def __init__(self, name): 4 self.name = name 5 zebra = Animal("Jeffrey") 6 print zebra.name 7 8 #override 9 class Employee(object): 10 def __init__(self, emplotee_name): 11 self.employee_name = emplotee_name 12 def calculate_wage(self, hours): 13 self.hours = hours 14 return hours * 20.0 15 16 class PartTimeEmployee(Employee): 17 def calculate_wage(self, hours): 18 self.hours = hours 19 return hours * 12.0 20 def full_time_wage(self, hours): 21 return super(PartTimeEmployee, self).calculate_wage(hours) 22 milton = PartTimeEmployee("Li") 23 print milton.full_time_wage(10) #200 24 print milton.calculate_wage(10) #120 25 26 class Car(object): 27 condition = "new" 28 my_car = Car() 29 print my_car.condition #new 30 31 class A(object): 32 def __init__(self, n): 33 self.__n = n 34 aa = A(10) 35 aa #__n是私有变量,对象无法访问
二.文件
1 #coding=utf-8 2 """ 3 w wb (+)写 4 r rb (+)读 5 a ab (+)追加 6 """ 7 my_List = [i**2 for i in range(1, 11)] 8 f = open("output.txt", ‘w‘) 9 for item in my_List: 10 f.write(str(item) + ‘\n‘) 11 f.close() 12 13 print f.readline() #一行 14 print f.read() #全部 15 16 with open("text.txt", ‘w‘) as textfile: 17 textfile.write("success!") 18 19 if textfile.closed == False: 20 textfile.close() 21 print textfile.closed
1 #coding=utf-8 2 import os 3 import shutil 4 import glob 5 """ 6 文件夹操作 7 """ 8 print os.getcwd() 9 os.mkdir("D:\\cwd") 10 print os.path.exists("D:\\cwd") 11 os.chdir("D:\\cwd") 12 print os.getcwd() 13 f = file("a.txt", "w") 14 f.close() 15 shutil.copy("E:\\b.txt", "D:\\cwd\\b.txt") 16 shutil.copy("E:\\b.txt", "D:\\cwd\\c.txt") 17 shutil.copy("E:\\b.txt", "D:\\cwd\\d.txt") 18 os.remove("a.txt") 19 os.rename("b.txt", "e.txt") 20 21 """ 22 文件夹遍历 23 """ 24 for root, dirs, files in os.walk(os.getcwd()): 25 print files 26 break 27 28 for file in os.listdir(os.getcwd()): 29 if os.path.isfile(os.path.join(os.getcwd(), file)): 30 print file 31 32 print glob.glob("*.txt") 33 os.chdir("D:\\") 34 shutil.rmtree("D:\\cwd") 35 #os.removedirs("D:\\cwd") error,删除多层空文件夹 36 #os.rmdir("D:\\cwd") error,只适用于空文件夹
三.struct,pack,unpack
1 #coding=utf-8 2 import struct 3 a = 1 4 b = 2 5 c = ‘sss‘ 6 d = 1.1 7 e = [a, b, c, d] 8 ss = struct.pack(‘ii3sf‘, a, b, c, d) 9 a, b, c, d = struct.unpack(‘ii3sf‘, ss) 10 print a, b, c, d #1 2 sss 1.10000002384 11 12 ss = struct.pack(‘ii3sf‘, *e) 13 a, b, c, d = struct.unpack(‘ii3sf‘, ss) 14 print a, b, c, d #1 2 sss 1.10000002384 15 16 x = [1, 2, 3] 17 y = [4, 5, 6] 18 xyz = zip(x, y) 19 print zip(x, y) #[(1, 4), (2, 5), (3, 6)] 20 print zip(x) #[(1,), (2,), (3,)] 21 print zip(*xyz) #[(1, 2, 3), (4, 5, 6)] 22 """ 23 [(1, 2, 3), (4, 5, 6), (7, 8, 9)] 24 25 一般认为这是一个unzip的过程,它的运行机制是这样的: 26 27 在运行zip(*xyz)之前,xyz的值是:[(1, 4), (2, 5), (3, 6)] 28 29 那么,zip(*xyz) 等价于 zip((1, 4), (2, 5), (3, 6)) 30 31 所以,运行结果是:[(1, 2, 3), (4, 5, 6)] 32 """
标签:
原文地址:http://www.cnblogs.com/wanderingzj/p/5248151.html