码迷,mamicode.com
首页 > 编程语言 > 详细

Python 知识点练习

时间:2021-06-18 19:22:14      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:put   inpu   res   返回   nbsp   update   return   cmd   sleep   

  1 #函数应用:计算两个正数的最大公约数和最小公倍数
  2 def function1(n1,n2):
  3     if n1 > n2:
  4         n1 , n2 = n2 , n1
  5     for i in range(n1,0,-1):
  6         if n1 % i == 0 and n2 % i == 0:
  7             return i
  8 def function2(n1,n2):
  9     return n1*n2//function1(n1,n2)
 10 
 11 #函数应用:判断是否为素数
 12 def sushu(number):
 13     a = 2
 14     for i in range(2 , number - 1):
 15         if number % i == 0 :
 16             a = 1
 17     if a == 1:
 18         return False
 19     else:
 20         return True
 21 #运用
 22 s1 = hello  * 4
 23 s2 = hello,world
 24 print(len(s1),s2.title(),s1.upper())
 25 print(s1.index(lo))
 26 print({0} * {1} = {2}.format(s1,s2,s1 + s2))
 27 
 28 #列表
 29 list1 = [3,4,5,6,7,88,90]
 30 print(list1)
 31 a = list1[3:5]
 32 b = list1[2]
 33 print(a,b)
 34 for x in range(len(list1)):
 35     print(list1[x])
 36 for index, elem in enumerate(list1):
 37     print(index,elem)
 38 list1.append(b)
 39 list1.insert(4,20)
 40 print(list1)
 41 list2 = [sdf,alte,cllect,fast,baby]
 42 list3 = sorted(list2)
 43 list4 = list2.sort(reverse = True)
 44 print(list2,\n,list3,\n,list4)
 45 
 46 #生成器
 47 import sys
 48 list1= [x for x in range(1,10)]
 49 print(list1)
 50 print(sys.getsizeof(list1))
 51 
 52 #元组
 53 import sys
 54 list1 = (cmd,5,贵州遵义)
 55 print(list1,sys.getsizeof(list1))
 56 
 57 #集合不存在重复性
 58 set1 = {1, 4, 23, 55, 43,4}
 59 print(set1)
 60 print(lenth:, len(set1))
 61 set1.add(88)
 62 set1.update([5,7])
 63 set1.discard(4)
 64 set1.remove(1)
 65 print(set1)
 66 set2 = set(range(0,5))
 67 print(set2)
 68 print(set1 & set2)
 69 print(set1 | set2)
 70 print(set1 - set2)
 71 print(set1 <= set2)
 72 print(set1 ^ set2)
 73 
 74 #字典
 75 scores = dict(a = 1, b = 3, c = 6)
 76 scores1 = {gz:20,sc:66,zy:3}
 77 print(scores)
 78 print(scores1)
 79 cs = scores1[gz]
 80 print(cs)
 81 for key in scores:
 82     print(key,scores[key])
 83 
 84 #
 85 class studengt(object):
 86     def __init__(self, name,age):
 87         self.name = name
 88         self.age = age
 89     def study(self, discipline):
 90         print(%s正在学习%s。%(self.name, discipline))
 91     def whatch(self):
 92         if self.age < 18:
 93             print(%s正在看 熊出没 %self.name)
 94         else:
 95             print(%s正在看 迪迦.%self.name)
 96 def studengt1():
 97     stu1 = studengt(wgy, 23)
 98     stu1.whatch()
 99     stu1.study(zgggS)
100 
101 print(studengt1())
102 
103 from time import sleep
104 class day():
105     def __init__(self, hour = 0, minute = 0, second = 0):
106         self.hour = hour
107         self.minute = minute
108         self.second = second
109     def cycle(self):
110         self.second += 1
111         if self.second == 60:
112             self.second = 0
113             self.minute += 1
114             if self.minute == 60:
115                 self.minute = 1
116                 self.hour += 0
117                 if self.hour == 24:
118                     self.hour = 0
119     def disply (self):
120         print(%02d : %02d : %02d %(self.hour, self.minute, self.second))
121 
122 def main():
123     time = day(12,33,44)
124     while True:
125         day.disply( time)
126         sleep(1)
127         day.cycle(time)
128 if __name__ == __main__:
129     main()
130 
131 #正则表达式(qq,用户名)
132 import re
133 #调用正则表达式模块re
134 def main():
135     user_name = input(请输入用户名:)
136     qq_number = input(请输入qq号:)
137     m1 = re.match(r^[0-9a-zA-Z]{6,20}$, user_name) #判断格式是否符合
138     m2 = re.match(r^[1-9]\d{4,11}, qq_number)
139     if not m1:
140         print(用户名不存在)
141     if not m2:
142         print(qq不存在)
143     if m1 and m2:
144         print(格式正确)
145 
146 print(main())
147 
148 #多进程实现文件下载
149 from multiprocessing import Process 
150 #linux用os模块fork()函数创建进程
151 #Windows用multiprocessing模块中的Process类创建进程
152 from os import getpid 
153 #返回进程号
154 from random import randint
155 from time import time, sleep
156 def downlode(filename):
157     print(启动下载程序,进程号%s % getpid)
158     print(开始下载%s... %filename)
159     downlode_time = randint(5,10)
160     sleep(downlode_time)
161     print(%s下载完成!耗费%d秒 %(filename, downlode_time))
162 
163 def main():
164     start = time() #开始计时
165     m1 = Process(target = downlode, args = (python从零开始.ptf, ))
166     #Process类创建了进程对象,通过target参数我们传入一个函数来表示进程启动后要执行的代码,后面的args是一个元组,它代表了传递给函数的参数。
167     m1.start()
168     m2 = Process(target= downlode, args = (python的多进程.ptf, ))
169     m2.start()
170     #start方法用来启动进程
171     m1.join()
172     #join方法表示等待进程执行结束
173     m2.join()
174     end = time() #结束计时
175     print(总共耗费了%.2f秒 %(end - start))
176 
177 if __name__ == __main__:
178     main()
179 
180 #多进程实现文件下载
181 from threading import Thread
182 from random import randint
183 from time import time, sleep
184 def download(filename):
185     print(%s文件开始下载.... %filename)
186     time_filename = randint(5, 12)
187     sleep(time_filename)
188     print(%s下载结束,下载花费%d秒 %(filename, time_filename))
189 def main():
190     start = time()
191     m1 = Thread(target = download, args = (python从零开始.ptf, ))
192     m1.start()
193     m2 = Thread(target = download, args=(pytn的多线程应用.pdf, ))
194     m2.start()
195     m1.join()
196     m2.join()
197     end = time()
198     print(总耗费%d秒. %(start - end))
199 if __name__ == __main__:
200     main()
201 
202 #API的调用
203 from time import time
204 from threading import Thread
205 import requests
206 class daygas():
207     def __init__(self, url):
208         self.url = url
209 
210     def run(self):
211         resp = requests.get(self.url)
212         with open(daygas.json,w) as f:
213             f.write(resp)
214 def main():
215     resp = requests.get(http://api.tianapi.com/txapi/tianqi/index)
216     for x in resp:
217         url = x
218         daygas(url)
219 
220 if __name__ == __main__:
221     main()
222 
223 #操作图像
224 from PIL import Image
225 image = Image.open(C:/Users/ASUS/Pictures/Saved Pictures/1.jpg)
226 print(image.format, image.size, image.mode)
227 image.show()
228 rect = 60, 30, 330, 340
229 image.crop(rect).show()

 

Python 知识点练习

标签:put   inpu   res   返回   nbsp   update   return   cmd   sleep   

原文地址:https://www.cnblogs.com/Uriel-w/p/14897677.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!