python
孤城@少年
早呀
展开
-
#二.2 主题:python函数
‘’’def 函数名(参数,参数,…):…return(没有return默认返回none)help(内置函数)=>查看帮助文档‘’’#内置函数print(abs(-20))#查看帮助文档help(abs)#数据类型转换print(int('123')) #字符串转换成intprint(int(123.456)) #float转换成intprint(float('...原创 2019-09-18 10:56:24 · 258 阅读 · 0 评论 -
turtle
#turtle库‘’’一.turtle的绘图窗体turtle.setup(width,height,startx,starty) #设置窗体的大小和位置,后两个参数可选turtle空间坐标体系#绝对坐标turtle.goto(x,y) #想去哪里就去哪里#海龟坐标turtle.fd(d) #向正前方turtle.bk(d) #向反方向turlte.circle(r,angle)...原创 2019-10-08 11:28:46 · 126 阅读 · 0 评论 -
re模块
‘’’re模块‘’’import re#search()方法用于在字符串中搜索正则表达式模式第一次出现的位置print(re.search(r'FishC','I love FishC.com!')) #r代表原始字符串#<re.Match object; span=(7, 12), match='FishC'>#find方法也可以print("I love Fish...原创 2019-10-07 22:36:35 · 74 阅读 · 0 评论 -
字典
#字典的内置方法#fromkeys()dict1={}print(dict1.fromkeys((1,2,3))) #{1: None, 2: None, 3: None}print(dict1.fromkeys((1,2,3),'number')) #{1: 'number', 2: 'number', 3: 'number'}#遍历dict1=dict1.fromkeys(ra...原创 2019-09-26 18:25:32 · 74 阅读 · 0 评论 -
字符串操作方法
str1=‘Songsong’print(str1.casefold()) # songsong 将字符串全部改为小写str1=‘ABABABC’print(str1.count(‘ABA’)) #1 子字符串有几个print(str1.find(‘BC’)) #5 找不到返回-1,找到了就返回所在的下标print(str1.isalnum()) #True 如果字符串至少有...原创 2019-09-26 16:03:58 · 78 阅读 · 0 评论 -
元组
#元组'''创建'''tuple1=(1,2,3,4)print(tuple1) #(1, 2, 3, 4)tuple1=1,2,3,4print(tuple1) #(1, 2, 3, 4) 元祖最重要的不是小括号,而是逗号!tuple2=(1)print(type(tuple2)) #<class 'int'>tuple2=(1,)print(type(tup...原创 2019-09-26 15:25:15 · 69 阅读 · 0 评论 -
列表
#列表mix=[1,'xiaojiayu',3.14,[1,2,3]]print(mix) #[1, 'xiaojiayu', 3.14, [1, 2, 3]]'''向列表里添加元素'''#添加一个mix.append('song')print(mix) #[1, 'xiaojiayu', 3.14, [1, 2, 3], 'song']#添加多个,以列表的形式添加mix.ex...原创 2019-09-26 10:05:46 · 86 阅读 · 0 评论 -
水仙花数(python)
n, m = (int, input().split())print(type(n))flag=0list=[]s=0for i in range(n,m+1): a=i%10 b=i//10%10 c=i//100 if i==a**3+b**3+c**3: list.append(i) s+=1 flag...原创 2019-09-25 11:18:19 · 650 阅读 · 0 评论 -
python_对象,封装,继承,多态(小甲鱼)
‘’’对象=属性+方法类名约定首写字母大写‘’’#继承‘’’子类重写父类的方法就会把父类的方法覆盖掉,解决办法就是利用super函数(super().init()…)多重继承‘’’class Mylist(list): #继承list里的东西 passlist2=Mylist()list2.append(2)list2.append(1)print(list2) ...原创 2019-09-24 10:52:43 · 284 阅读 · 0 评论 -
python_异常处理机制
#异常处理‘’’try: 检测范围except Exception[as reason]: 出现异常(Exceotion)后的处理方法finally: 无论如何都会被执行的代码'''try: #int ('abc') f=open('D:\\我睡.txt','w') print(f.write('I love you!')) sum =...原创 2019-09-23 14:34:21 · 102 阅读 · 0 评论 -
python中的pickle
将列表等东西转换成二进制保存起来,用的时候再拿出来。可以减少程序代码量import picklemy_list=[123,4124,[121,134124]]pickle_file=open('D:\\my_list.pkl','wb')pickle.dump(my_list,pickle_file)pickle_file.close()pickle_file=open('D:\\m...原创 2019-09-23 10:48:41 · 152 阅读 · 0 评论 -
python 文件的读入和写入
f=open('D:\py.txt')song=[]shi=[]count=1def xie_ru(song,shi,count): song_file = open('D:\\song_' + str(count) + '.txt', 'w') shi_file = open('D:\\shi_' + str(count) + '.txt', 'w') song_...原创 2019-09-23 10:06:29 · 129 阅读 · 0 评论 -
二.3主题: python面向对象
‘’’面向过程编程为:函数的调用集合面向对象编程为:对象之间传递信息的集合对象包含属性和方法类可以理解为图纸或者模板实例是根据类的图纸或者模板创建出来的一个一个对象类定义为class,关键字self类的初始化函数_init_面对对象的三大特点:继承,封装,多态‘’’#面向过程stu1={‘name’:‘xiaohong’,‘score’:98}stu2={‘name’:‘x...原创 2019-09-18 12:39:00 · 131 阅读 · 0 评论 -
python数据类型
#整数类型‘’’十进制二进制 0b开头八进制 0o开头十六进制 0x开头pow()‘’’#浮点数‘’’round(x,n) #对x进行四舍五入,n为小数截取位数‘’’#复数类型‘’’获得实部:z.real()获得虚部:z.imag()‘’’#数值运算函数‘’’1.abs(x)2.divmod(x//y,x%y) 同时输出商和余数3.pow(x,y[,z])...原创 2019-10-08 15:52:08 · 73 阅读 · 0 评论