![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Python
Python学习
uvyodoytl
这个作者很懒,什么都没留下…
展开
-
Python3 L21
代码:import re#!/usr/bin/python# -*- coding: UTF-8 -*-pattern = re.compile(r'\d+') # 查找数字result1=pattern.findall('aa2673hdb52ggb')print(result1)t="Hello#world!" #以"#"为分隔符result2=t.split('#',1)print(result2)运行结果:['2673', '52']['Hello', 'worl原创 2020-11-08 23:27:20 · 120 阅读 · 0 评论 -
Python3 L20
代码:from collections import deque#!/user/nim/python#-*-coding: UTF-8 -*-#列表当堆栈使用stack=[1,2,3]stack.append(4) #添加元素到栈顶stack.append(5)print (stack)stack.pop() #释放栈顶元素print(stack)#列表当队列使用queue=deque(["Alice","Bob","Cris","David"])queue.append("原创 2020-11-04 17:06:55 · 67 阅读 · 0 评论 -
Python3 L19
代码:#!/usr/bin/python#-*-coding: UTF-8 -*-#set()函数创建集合a = set(("aa","uuii","cbd"))a.add("ooo") #向集合a添加元素print(a)a.update({"w","ww"}) #向集合a添加列表print(a)a.remove("cbd") #删除集合a中指定元素,当使用remove()时,如果删除元素不存在会报错a.pop() #随机删除集合a的一个元素print(a)print(len原创 2020-11-03 20:53:35 · 92 阅读 · 0 评论 -
Python3 L18
代码:#!/usr/bin/python3import sysdef fib(n): a = 0 b = 1 counter = 0 while True: if (counter > n): return yield a #在遇到yield函数时,函数暂停且保存当前信息,返回yield的值,在下一次的执行next()时从当前位置继续执行 a原创 2020-11-02 21:09:01 · 744 阅读 · 0 评论 -
Python3 L17
代码:#!/usr/bin/python3 list=[3,2,1] i = iter(list) # 创建迭代器对象 for x in i: #遍历迭代器对象 print (x+" ")class iterDemo: def __iter__(self): self.a = 1 return self #返回一个迭代器 def __next__(self): b= self.a self.a += 1原创 2020-11-01 22:21:23 · 73 阅读 · 0 评论 -
Python中的缩进
与C、C++、JAVA不同的是Python不用大括号{},而是用缩进表示代码块,不正确的缩进会报错。(1)if True: #由:开始一个逻辑层 print ("T")else: print ("F")(2)if True: print ("T")else: print ("123") print ("F") #else语句内两行的缩进不一致,报错...原创 2020-10-31 23:00:13 · 83 阅读 · 0 评论 -
Python3 L16
代码:#!/user/nim/python#-*-coding: UTF-8 -*-exitFlag = 0import threadingimport timeclass ThreadDemo(threading.Thread): def __init__(self, threadID, name, counter): threading.Thread.__init__(self) self.threadID=threadID self原创 2020-10-30 20:53:54 · 104 阅读 · 0 评论 -
Python3 L15
代码:#!/user/nim/python# -*-coding: UTF-8 -*-import threadimport timedef get_time( threadName, delay): count = 0 while count < 3: time.sleep(delay) count += 1 print ("%s: %s" % ( threadName, time.ctime(time.time()) ))try:原创 2020-10-27 19:48:42 · 93 阅读 · 0 评论 -
Python3 L14
代码#!/user/nim/python#-*-coding: UTF-8 -*-import rephone="800-820-8886"num=re.sub(r'\D',"",phone)print numline="123abc@email.com"m=re.findall(r'\d+',line)print mn=re.findall(r'\d+',line)print n运行结果:8008208886['123']['123']原创 2020-10-25 21:31:41 · 54 阅读 · 0 评论 -
Python3 L13
代码:#!/user/nim/python#-*-coding: UTF-8 -*-#!/user/nim/python#-*-coding: UTF-8 -*-import reline="abc123@email.com"print (re.match('abc',line).span()) #re.match函数,在起始位置匹配print (re.match('email',line)) #re.match函数,不在起始位置匹配print (re.search('ab原创 2020-10-23 23:29:27 · 182 阅读 · 1 评论 -
Python3 L12
代码:(1)#!/user/nim/python#-*-coding: UTF-8 -*-from say import sayhi,saybye #导入自定义的模块,模块需在同一目录下import calendar #导入python自带的模块sayhi()#使用模块里的函数saybye()cal=calendar.month(2020,10) #获取2020年10月的日历print cal(2)def sayhi(): print "hello"def sayb原创 2020-10-22 23:11:23 · 76 阅读 · 0 评论 -
Python3 L11
代码:#!/user/nim/python#-*-coding: UTF-8 -*-import osfile=open("a1.txt","w") #创建文件,只用于写入print "file name:",file.name #文件名print "is closed:",file.closed #文件是否关闭f=open("a2.txt","w+") #创建文件,可读写f.write("hello,world") #向文件写入内容s原创 2020-10-21 20:53:43 · 117 阅读 · 0 评论 -
Python3 L10
(1)代码:#try/exceptwhile True: try : x=int(input("Please Enter a number:")) break except ValueError: print("Error!")运行结果:Please Enter a number:1Please Enter a number:aTraceback (most recent call last): File "<strin原创 2020-10-19 22:20:20 · 47 阅读 · 0 评论 -
Python3 L9
代码:#!/user/nim/python#-*-coding: UTF-8 -*-class Test: #类 i=1 #公有变量 __a=0 #私有变量 def f(self): #def关键字定义方法,方法必须包含参数self,且self为第一个参数 return 'abc' def __init__(self,real,image): #__init__为类的构造方法,在类实例化时自动调用 self.原创 2020-10-18 22:10:12 · 64 阅读 · 0 评论 -
Python3 L8
代码:#!/user/nim/python#-*-coding: UTF-8 -*-dict={'a':0,'b':2,'c':3,'a':1} #字典print (dict['a']) #同一个键被赋值两次,键值取后一个dict['a']=10 #修改字典,改变已有键值对print (len(dict)) #计算字典元素个数print (str(dict)) #遍历字典del dict #删除字典运行结果:13{'a': 10, 'c': 3,原创 2020-10-17 20:43:42 · 228 阅读 · 0 评论 -
Python3 L7
代码:#!/user/nim/python#-*-coding: UTF-8 -*-tup1=(1,2,3) #元组tup2=('a','b','c')print (tup1[0])#tup1[0]=5 非法,元组元素不允许修改tup3=tup1+tup2 #直接对元组进行组合print (tup3)del tup1 #仅删除整个元组,不允许删除元组的元素运行结果:1(1, 2, 3, 'a', 'b', 'c')...原创 2020-10-16 22:41:04 · 78 阅读 · 0 评论 -
Python3 L6
代码:#!/user/nim/python#-*-coding: UTF-8 -*-sum=0#累加列表num=list(range(1,11))for n in num: sum=sum+nprint(sum)#求列表平均值l=len(num)print (sum/l)#列表累乘a=1for n in num: a=a*nprint (a)#列表里统计出现次数lista=[1,1,5,6,7]count =0for n in lista: if原创 2020-10-12 21:12:02 · 75 阅读 · 0 评论 -
Python3 L5
代码:#!/user/nim/python#-*-coding: UTF-8 -*-list_num=[1,2,3]for n in list_num: #for循环遍历列表 print (n)#for n in list_num: #没有缩进,报错#print (n)lista=list(range(1,10)) #range自动生成列表print(lista)for n in range(3): #指定次数循环 print (lista)运行结果:12原创 2020-10-11 23:17:53 · 165 阅读 · 0 评论 -
Python3 L4
代码:#!/user/nim/python#-*-coding: UTF-8 -*-list_num=[1,2,3]list_char=["a","b","c"]print list_num #以列表形式打印print list_charprint list_num[0:3] #指定元素以列表形式打印for n in list_num: #打印列表元素 print (n)list_num.append(4) #append方法:在列表末尾增加元素print ("A原创 2020-10-10 21:46:20 · 57 阅读 · 0 评论 -
Python3 L3
代码#!/user/nim/python#-*-coding: UTF-8 -*-a=3if(a>0): print ("T") #if语句if(a<0): #else if语句 print ("F")else: print("T") while(a>0): #while语句 print ("true") a=a-1运行结果TTtruetruetrue...原创 2020-10-09 22:43:47 · 71 阅读 · 0 评论 -
Python3 L2
代码:#!/user/nim/python#-*-coding: UTF-8 -*-print ('abc') #print字符串print ('abc'+'de') #拼接字符串a='123'b='45'print (a+b) #字符串变量拼接a='99'print(a) #改变字符串输出:abcabcde1234599原创 2020-10-08 20:57:15 · 62 阅读 · 0 评论 -
python3 L1
#!/user/nim/python#-*-coding: UTF-8 -*-print(1) #print数字a=3 #变量命名与赋值(整型)print (a)b=5print(b)c=a*b #变量运算print(c)a=9 #改变变量值print (a)原创 2020-10-07 18:08:24 · 47 阅读 · 0 评论