1、进制之间转换
# 10进制转为2进制 print(bin(5)) #10进制转为8进制 print(oct(9)) #10进制转为16进制 print(hex(15)) print("------2-----") #2进制转为8进制 print(oct(int('101',2))) #2进制转为10进制 print(int('101',2)) #2进制转为16进制 print(hex(int('101',2))) print("-----8------") #8进制转为2进制 print(bin(int('0o11',8))) #8进制转为10进制 print(int('0o11',8)) #8进制转为16进制 print(hex(int('0o11',8))) print("-----16------") #16进制转为2进制 print(bin(int('0x9',16))) #16进制转为10进制 print(int('0x9',16)) #16进制转为8进制 print(oct(int('0x9',16)))
2、数组、字典、元组转换
# float专int f01=3.1415926 print(int(f01)) list01=[1,2,3,4] #数组专元组 print(tuple(list01)) tuple01=(1,2,3,4) # 元组专数组 print(list(tuple01)) print("数组转为set") print(set(list01)) print("元组转为set") print(set(tuple01)) # 字典 dict01={"name":"zs","age":"18"} print("字典转为set") print(set(dict01)) print("字典转为list") print(list(dict01)) print(list(dict01.values()))
3、取出字典中的值
student = {'stuno':'1001','age':'18','banji':'软件01'} for key in student: print(key,'==',student[key]) print("只循环值") for value in student.values(): print(value) print("同时循环key,value") for key,value in student.items(): print("key=",key,",value=",value)
4、函数
def hell_fun(name,age): return '{}{}你好'.format(name,age) def hello_pp(greeting,name='zhangsan'): return '{},{}:你好'.format(greeting,name) print(hell_fun("zd",23)) print(hello_pp('nihao')) print(hello_pp('nihao','zs')) def student_info(*args,**kwargs): print(args) print(kwargs) courses = ['js','css','php'] info = {'name':'zs','age':'18'} student_info(courses,info) student_info(*courses,**info) def foo(x,*args,a=4,**kwargs): print(x) print(args) print(a) print(kwargs) foo(1,(5,6,7,8),{'name':'zs','age':'18'}) foo(1,*(5,6,7,8),**{'name':'zs','age':'18'}) add = lambda x,y:x+y add01 = lambda *args:sum(args) print(add01(1,56,35,1,1,23)) print(add(1,3))
5、获得当前时间以及文件名
import time import os report_time = time.strftime("%Y%m%d%H%M%S",time.localtime()) print(report_time+"_"+os.path.basename(__file__))
6、从某个序列中取出数据
import random courses = ['js','java','css','html','php','go'] random_course = random.choice(courses) print(random_course)
7、生成随机字符串
import string red_str = ''.join(random.choice(string.ascii_uppercase+string.ascii_lowercase+string.digits)for _ in range(8)) print(red_str)
8、读取文件
try: with open('test.log') as fh: fille_data = fh.read() print(fille_data) except FileNotFoundError: print('This data file is missing.') except PermissionError: print('This is not allowed.') except Exception as err: print('Some other error occurred:',str(err))
常见错误
常用的字符串方法
os模块
9、创建定义类
class Car(object): def __init__(self,make,model,year): self.make = make self.model = model self.year = year def get_des(self): long_name = str(self.year) + '' + self.make + ' '+self.model return long_name.title() my_new_car = Car('mini','a4',2016) print(my_new_car.get_des()) if __name__ == '__main__': my_new_car = Car('mini','a4',2016) print(my_new_car.get_des())
10、继承
class Car(object): def __init__(self,make,model,year): self.make = make self.model = model self.year = year def get_des(self): long_name = str(self.year) + '' + self.make + ' '+self.model return long_name.title() class ElectricCar(Car): def __init__(self,make,model,year,battery): super().__init__(make,model,year) self.battery = battery def des_battery(self): print(self.battery) my_tesla = ElectricCar('mini','model 4',2016,'20万') print(my_tesla.get_des()) my_tesla.des_battery()
11、重写
class Car(object): def __init__(self,make,model,year): self.make = make self.model = model self.year = year def get_des(self): long_name = str(self.year) + '' + self.make + ' '+self.model return long_name.title() class ElectricCar(Car): def __init__(self,make,model,year,battery): super().__init__(make,model,year) self.battery = battery def des_battery(self): print(self.battery) #重写父类get_des() def get_des(self): long_name = str(self.year) + ' ' + self.make + ' '+self.model+' battery:'+self.battery return long_name.title() my_tesla = ElectricCar('mini','model 4',2016,'20万') print(my_tesla.get_des()) my_tesla.des_battery()
12、下划线 ,双下划线的作用
- 变量名如果以__开头,就变成了一个私有变量(private)
- 变量名如果以_开头,这样的变量可以被外部所访问,但是, 按照约定俗成的规定,当你看到这样的变量时,意思就 是,“虽然我可以被访问,但是,请把我视为私有变量,不要随意访问”
- 变量名类似__xxx__的,也就是以双下划线开头,并且以双 下划线结尾的,系统自定义名字,不建议占用
13、导入类
导入单个类
from car import Car my_new_car = Car('benchi','a4',2016) print(my_new_car.get_des())
从一个模块中导入多个类
from car3 import Car,ElectricCar my_new_car = Car('benchi','a4',2016) print(my_new_car.get_des()) my_tesla = ElectricCar('mini','model 4',2016,'20万') print(my_tesla.get_des()) my_tesla.des_battery()
从一个模块中导入所有类
from model_name import *
导入多个模块
from model_name_01 import * from model_name_02 import * from model_name_03 import *
14、函数运行时长计时器
import time import random def time_count(func): def wrap(*args,**kwargs): time_flag = time.time() temp_result = func(*args,**kwargs) print("time cost:",time.time()-time_flag) return temp_result return wrap @time_count def loop_time(x,y): result=0 for i in range(x,y): time.sleep(random.choice((0.1,0.2,0.3))) result=x+y return result print(loop_time(1,10))