
python基础
写写吧
这个作者很懒,什么都没留下…
展开
-
Python开发模式:实现单例模式的两种方法
调用方法,不产生新对象 使用同一块系统空间方法一:创建一个类的私有变量,用来保存第一次开辟内存空间的地址,再之后每次使用__init__时,将私有变量保存地址————>init(self)----->创建的对象class Singleton: # 私有化 单例的地址就储存在 __instance __instance = None name = 'Jack' # 重写 __new__ # __new__开辟空间,__init__初始化对象 ——不产生原创 2022-05-17 16:23:52 · 519 阅读 · 0 评论 -
Python文件复制
import os# 源目录src_path = r'C:\Users\Wanglq\Desktop\python\test\p1'# 目标目录target_path = r'C:\Users\Wanglq\Desktop\python\test\p5'def copy(src_path, target_path): # 源目录的文件名,保存到列表中 file_list = os.listdir(src_path) # 判断目录是否在 if os.path.exis原创 2022-04-07 12:29:34 · 1892 阅读 · 0 评论 -
Python——open()函数 文件处理
“”"模式参数添加 ‘w’ 写入模式 告知Python 要写入文件 会覆盖原内容重新写入‘r’ 读取模式‘r+’ 读写模式‘a’ 附加模式 不会覆盖原内容调用 write() 函数写入https://blog.youkuaiyun.com/u011811614/article/details/50832544Character Meaning‘r’ openfor reading (default)‘w’ openfor writing, truncating the file first‘a原创 2022-04-02 23:29:25 · 1020 阅读 · 0 评论 -
Python——数据引用sys.getrefcount()
sys.getrefcount(a),在非函数中查找引用次数del 变量 表示删除一个引用import sysprint(sys.getrefcount(1658))a = 1658print(sys.getrefcount(a))b = aprint(sys.getrefcount(a))del bprint(sys.getrefcount(a))c = aprint(sys.getrefcount(a))运行结果:34545函数引用:需要分清楚可变与不可变类型原创 2022-03-29 10:58:47 · 2479 阅读 · 0 评论 -
Python——LittleTips(01) append生成列表
列表原创 2022-03-28 17:40:39 · 1051 阅读 · 0 评论 -
Python数据类型的可变与不可变(有关函数全局变量)
可变与不可变:通过地址去判断可变不可变可变: 内容改变,地址不变类型:list,dict,set{集合}不可变:当变量的值发生改变时,地址会改变,所以是不可变类型: int,float,str,bool,tuplea = 15print(id(a))a = 20print(id(a))list1 = [1, 5, 6, 7, 8]print(id(list1))list1.append(5)print(id(list1))结果:2202795731696220279573原创 2022-03-28 13:26:38 · 1617 阅读 · 0 评论 -
Python全局变量和局部变量
全局变量与局部变量def login(username, password): global isLogin if username == 'admin' and password == '123': print("登陆成功") isLogin = True else: print("密码错误") return Falsedef borrow_books(bookname):原创 2022-03-27 21:39:15 · 229 阅读 · 0 评论 -
Python模块导入
一、同级直接用import mod1或from mod1 import ;二、上下级– src|– mod1.py|– lib|| – mod2.py|– test1.py如果想在程序test1.py中导入模块mod2.py ,可以在lib件夹中建立空文件__init__.py文件– src|– mod1.py|– lib|| – init.py|| – mod2.py|– test1.pyfrom lib.mod2 import或import lib.mo原创 2022-03-23 18:02:44 · 1332 阅读 · 0 评论 -
matplotlib 坐标轴采用科学计数法
主要函数:matplotlib.pyplot.ticklabel_format(**kwargs)参数解释:style:两个选项,‘sci’(or ‘scientific’) 或者’plain’,前者是科学计数法,后者是关闭科学计数法scilimits:输入为(m, n)一对整数。如果style设置为’sci’,那么将对该范围之外的数值采用科学计数法,该范围之内的保持不变,选择(0, 0)对所有的数值都采用科学计数法。useOffset:三个选项[True | False | offset],如果为转载 2022-03-15 18:59:33 · 4355 阅读 · 0 评论 -
plt.rcParams(可解决matplotlib无法显示中文和负号的问题)
plt.rcPrarms的使用方法以及能解决的问题import matplotlib.pyplot as pltimport matplotlib字体matplotlib.rcParams[‘font.family’] = ‘SimHei’ #设置字体matplotlib.rcParams[‘font.size’] = 10 #设置字体大小matplotlib.rcParams[‘axes.unicode_minus’]=False #坐标轴的负号正常显示样式plt.rcPa原创 2022-03-15 15:59:09 · 12249 阅读 · 2 评论 -
python基础——函数——传递任意数量的实参
Python 允许函数从调用语句中收集任意数量的实参注:通用形参名*args,也收集任意数量位置实参#*topping Python创建一个名字为topping的空元组,并封装(需要放到最后)。def make_pizza(*toppings):“”“打印顾客所有配料”""print(toppings)make_pizza(‘pepperoni’)make_pizza(‘mushrooms’, ‘green peppers’, ‘extra cheese’)...原创 2022-02-11 00:43:36 · 734 阅读 · 0 评论 -
python基础——几种遍历列表的方法
messages = [‘Hello’, ‘everybody’, ‘hi’]def show_message(message):i = 0while i < len(messages):print(messages[i])i += 1# i=0# for i in range(len(messages)):# print(messages[i])# i =+1# for message in messages:# print(message)show原创 2022-02-08 17:21:05 · 628 阅读 · 0 评论