程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python exam

編輯:Python

6、 Output 100 The prime number within

for i in range(2, 101):
for j in range(2, i//2+1):
if i % j == 0:
break
else:
print(i)

7、 Defined function , Judge whether the user name is legal ( Contains at least one capital letter , a number
word )

import string
numbers = string.digits
upper = string.ascii_uppercase
name = "csdvvscADA2521s"
def check(name):
num = 0
upp = 0
for i in name:
if i in numbers:
num = 1
if i in upper:
upp = 1
if num == upp == 1:
return True
else:
return False
if check(name):
print(" legal ")
else:
print(" illegal ")

8、 Using recursion , Output all in the specified directory jpg file

import os
dirname = input(" Please enter the path :")
def selectjpg(dirpatth):
items = os.listdir(dirpatth)
for item in items:
item=os.path.join(dirpatth, item)
if os.path.isdir(item):
selectjpg(item)
elif os.path.isfile(item):
if item.endswith(".jpg"):
print(os.path.basename(item))
selectjpg(dirname)

9、 exception handling : Handle "123"+4 It's abnormal

a = "123"
b = 4
try:
c = a + b
except TypeError:
print(" Type error ")

10、 The book management system , Book information package to save
enclosed : Book number 、 Title 、 Price 、 Inventory . Keyboard entry 3 Information about this book , Store and complete the output in three ways . If the title exceeds 4 A word , Display only 4 A word , rest
Add ‘.…’
Mode one : Use tuples to store information about a Book , The tuple list stores all books
Mode two : Use a dictionary to store all book information , The key is the book number , The value is “ Title _ Price inventory ”
Mode three : Define Book classes ( Use property Encapsulation properties ), Use the object list to store all books

class book:
def __init__(self, id, name, price, number):
self.__id = id
self.__name = name
self.__price = price
self.__number = number
@property
def id(self):
return self.__id
@id.setter
def id(self, id1):
self.__id = id1
@property
def name(self):
return self.__name
@name.setter
def name(self, name1):
self.__name = name1
@property
def price(self):
return self.__price
@price.setter
def price(self, price1):
self.__price = price1
@property
def number(self):
return self.__number
@number.setter
def price(self, number1):
self.__number = number1
tu = tuple()
dic = {
}
ls = []
for i in range(3):
id = input(" Please enter the book number :")
name = input(" Please enter the title of the book :")
price = int(input(" Please enter the price :"))
number = int(input(" Please enter stock :"))
if i == 0:
tu = (id, name, price, number)
elif i == 1:
dic[id] = [name, price, number]
elif i == 2:
ls.append(book(id, name, price, number))
def sl(bookname):
if len(bookname) > 4:
name = bookname[0:4] + "***"
print(' name ', name)
else:
print(' name ', bookname)
print(' Book number ', tu[0])
sl(tu[1])
print(' Price ', tu[2])
print(' stock ', tu[3])
for id, value in dic.items():
print(' Book number ', id)
sl(value[0])
print(' Price ', value[1])
print(' stock ', value[2])
for i in ls:
print(' Book number ', i.id)
sl(i.name)
print(' Price ', i.price)
print(' stock ', i.number)

11、 Use polymorphic simulation : Paws & Claws Pet Vet : See a doctor for a pet : The dog is sick , Just get an injection and take medicine ; The bird is ill , Just bandage and recuperate


class Animal_doctor:
def ill(self):
print(" Seeing a pet doctor ")
def treat(self,anmial):
anmial.ill()
class Dog(Animal_doctor):
def ill(self):
print(" The dog is sick , Injection and medicine ")
class Bird(Animal_doctor):
def ill(self):
print(" Sick birds , Dressing and recuperation ")
doctor = Animal_doctor()
dog = Dog()
bird = Bird()
doctor.treat(dog)
doctor.treat(bird)

12、 File read and write :
1) Randomly generated 10 individual 4 A verification code consisting of digits , Stored in code.txt In file ( One verification code and one line )
2) from code.txt Read out all contents in the output

import string
import random
a = string.digits + string.ascii_letters
def yanz():
code = ""
for i in range(4):
code += random.choice(a)
return code
with open('code.txt','w') as f:
for i in range(10):
f.write(yanz()+'\r')
with open('code.txt','r') as f:
print(f.read())

13、 Use the crawler to download pictures

import requests
url = "https://pic.netbian.com/uploads/allimg/211109/221532-163646733295cd.jpg"
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0"}
respones = requests.get(url,headers = headers)
print(" Downloading ")
with open("E://"+'picture1.jpg','wb') as f:
f.write(respones.content)
print(" Download complete ")

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved