python期末考试复习
实验
基本
1、 水仙花数
value = input("请输入一个三位数:")
a = int(value[0])
b = int(value[1])
c = int(value[2])
value = int(value)
if value == a**3+ b**3+ c**3:
print("是水仙花数字")
else:
print("不是水仙花数")
2、print多个变量输出
num = 389
print("389的二进制整数为:{:b}".format(num))
print("389的八进制整数为:%o" %num)
print("389的十进制整数为:%i" %num)
print("389的十六进制整数为:%x" %num)
print("0.002178的科学表示法形式并保留两位小数:%.2e"%0.002178)
print("1/3输出为百分号表示法:{:.2%}".format(1/3))
3、取整数的百十个位
a = int(input("请输入一个三位整数:"))
get_3 = a/100
get_2 = a/10 % 10
get_1 = a % 10
print("此三位整数的百位为:%d \n"
"此三位整数的十位为:%d \n"
"此三位整数的个位为:%d " % (get_3, get_2, get_1))
4、谁在说谎,推理
lst = ['甲', '乙', '丙', '丁']
for who in lst:
if (who != "甲") + (who == "丙") + (who == "丁")+(who != "丁") == 3:
print(who)
5、阶乘
def num(n):
if n == 0:
return 1
else:
return n * num(n - 1)
print(num(6))
import math
value = math.factorial(6)
print(value)
列表
1、用户身份登陆
users = ["root", "admin"]
passwords = ["123", "456"]
username = input("please enter your username:")
if username in users:
print("请输入你的密码:")
password = input("please enter your password:")
i = users.index(username)
if password == passwords[i]:
print("登陆成功")
else:
print("登陆失败,密码错误")
else:
print("用户名不存在")
2、3个0~2为索引,生成句子
import random
lst_who = ["小猪", "小狗", "小猫"]
lst_where = ["草地上", "电影院", "家里"]
lst_what = ["看电影", "听故事", "吃晚饭"]
a = random.randint(0, 2)
b = random.randint(0, 2)
c = random.randint(0, 2)
print(a)
new_lst = lst_who[a]+lst_where[b]+lst_what[c]
print(new_lst)
3、 彩票排序
import random
lst = [random.randint(10, 99) for i in range(20)]
print(lst)
asc_lst = lst[0:10]
asc_lst.sort()
print(asc_lst)
dsc_lst = lst[10:20]
dsc_lst.sort(reverse=True)
print(dsc_lst)
字典
1、 字母出现第一次的位置写入字典
sen = "hello hzy"
dict = {}
for i in range(len(sen)):
a = sen[i]
b = sen.index(sen[i])
xx = {a: b}
dict.update(xx)
print(dict)
函数
1、 函数:多值传参:mult返回输入参数的乘积
def multi(list):
sum1 = 1
for i in range(len(list)):
sum1 = sum1 * list[i]
return sum1
s = [1, 2, 3]
print(multi(s))
方法
1、狗类
class Dog:
def __init__(self,name,age):
self.__name = name
self.__age = age
def eat(self):
print("正在吃东西")
class TeddyDod(Dog):
def watch(self):
print("看着你")
class BabyTeddyDog(TeddyDod):
def go_around(self):
print("四处转悠")
money = BabyTeddyDog("money", 3)
money.eat()
money.watch()
money.go_around()
2、学生类
class Student:
count = 0
def __init__(self, sno, sname, age):
self.__sname = sname
self.__age = None
self.__sno = sno
@property
def sno(self):
return self.__sno
def get__age(self):
return self.__age
def set__age(self, age):
self.__age = age
def del__age(self):
del self.__age
age = property(get__age, set__age, '学生年龄')
def show(self):
print(self.__sno,self.__sname,self.__age)
def classAddCount(self):
Student.count += 1
print("共计实例化对象: %d" % Student.count)
def staticShowCount(self):
print(Student.count)
字符串
1、 字符串操作:单词翻转,hello反转,split
str1 = "www.jhun.edu.cn"
print("%.1s" %str1)
print("%.3s" %str1)
print(str1[-3::])
print(str1[::-1])
print(str1.index("u"))
print(str1.count("u"))
print(str1.replace(".", "-"))
print(str1.upper())
print(str1.split("."))
2、字符串的英文、数字个数
# 判断是否为数字和字母
# i.isalnum
# i.isalpha
print("192208106236")
s = input("input:")
big_num = 0 # 记录大写字母个数
small_num = 0 # 记录小写字母个数
for i in s:
if i.isupper():
big_num += 1
elif i.islower():
small_num += 1
print('大写字母个数为:%d, 小写字母个数:%d' % (big_num, small_num))
文件
1、D盘 sample : test、建立info子目录、拷贝 sample. test到info文件中
import shutil
import os
os.makedirs("D:\\sample\\info")
list = os.listdir()
a = "D:\\sample\\test.txt"
b = "D:\\sample\\info"
shutil.copy(a, b)
2、txt中最长的一行有多少字符
max = -1
with open('data.txt') as info:
while True:
line = info.readline().strip()
if len(line) > max:
max = len(line)
if not line:
break
print(max)
info.close()
做图
1、数据分析、折线图、条形图
2、柱状图、折线图
3、plt、legend
plt.legend() #显示图例