题目比较基础,练习基本语法
1.把2.918 转化为整形
num_a = 2.918
num_b = int(num_a)
print(num_b)
2.把10 进制数 18 转化为2进制数
num_a = 18
num_b = bin(int(num_a))
num_c = hex(int(num_a))
num_d = oct(int(num_a))
print("10进制18转化为2进制后是:", num_b)
print("10进制18转化为16进制后是:", num_c)
print("10进制18转化为8进制后是:", num_d)
3.用java 替换字符串: ”Python is popular” 里面的Python,并把java 变换成JAVA+
str_a = "Python is popular"
str_b = str_a.split() # ['Python', 'is', 'popular']
str_c = str_a.replace("Python", "java".upper())
print(str_c)
4.把列表 [1, 2, 3,4 5,6,7,8]里面的2, 4, 6,8 打印出来
#方法一:利用列表切片
li_a = [1, 2, 3, 4, 5, 6, 7, 8]
li_b = li_a[1::2]
print(li_b)
#方法二:循环
li_c = []
for i in li_a:
if i % 2 == 0:
li_c.append(i)
print(li_c)
5.创建一个字典,字典的key分别是name, sex, province , 修改原始province 的值 为新值”江苏”
#dict_a = {"name": "许杰辉", "sex": "男", "province": "陕西"}
dict_a = {}
dict_a["name"] = "许杰辉"
dict_a["sex"] = "男"
dict_a["province"] = "陕西"
dict_a["province"] = "江苏" #修改
print(dict_a)
6.Test_str=“Python was created in 1989, Python is using in AI, big data, IOT.” 按下列要求对上面文字做出处理。
Test_str = "Python was created in 1989,Python is using in AI,big data,IOT."
Test_str_lower = Test_str.lower()
print(Test_str_lower)
li = Test_str.replace(",", " ").split() #先把逗号换成空格,然后分割
print(li)
str_middle = li[int(len(li)/2)] # float转换为int会取整
print("最中间的单词是:", str_middle)
7.List1=[“python”, 5,6, 8], list2=[“python”,”5”, 6, 8,10], 对list1和list2做出如下处理
list1 = ["python", 5, 6, 8]
list2 = ["python", "5", 6, 8, 10]
list_merge = list1 + list2
list_uniq = set(list_merge)
print("list1和list2的并集为:", list_uniq)
8.把1000-2500之间,既能被7整除,也能被5整除的数取出来,放到一个列表输出
li = []
for num in range(1000, 2501):
if num % 5 == 0 and num % 7 == 0:
li.append(num)
print(li)
9.打印出0-20之间的数字,如果此数字能被3整除,输出英文”three”, 如果能被5整除,输出”five”,如果既能被3整除也
能被5整除,输出”threes+fives”, 要求用到continue
1 2
Threes
4
fives
Threes
7 … 1
4
Thees+fives
print("-----使用Continue-----")
for num in range(21):
if num % 3 == 0 and num % 5 == 0:
print("threes+fives")
continue
elif num % 3 == 0:
print("threes")
continue
elif num % 5 == 0:
print("fives")
continue
print(num)
10.实现一个函数,要求对一个列表里面所有数字求和,如果里面含有非数字的元素。直接跳过。比如[1,2,3] 输出是5, 如果
是[1,2,4,”a”] 输出是7。 并在另外一个包(目录)里面调用这个函数
def sum_list_1(li):
sum = 0
for num in li:
if isinstance(num, int):
sum += num
print(sum)
11.已有字典dic={“name”:”xiaozhang”,”sex”:”male”}, 访问字典dic[“grade”], 通过try… exception 把异常信息打印出来
dic = {"name": "xiaozhang", "sex": "male"}
try:
dic["grade"]
except KeyError as e:
print(e) # 打印索引的key,即"grade"
12.实现一个不定长参数的函数def flexible(aa, *args, **kwargs):,把传入的参数和值打印出来。比如传入参数是
flexible(aa, 2, 3, x = 4, y = 5, *[1, 2, 3], **{‘a’:1,‘b’: 2})输出结果: (2, 3, 1, 2, 3), {‘a’: 1, ‘y’: 5, ‘b’: 2, ‘x’: 4}
def flexible(aa, *args, **kwargs):
print(aa, args, kwargs)
flexible(1, 2, 3, x=4, y=5, *[1, 2, 3], **{'a': 1, 'b': 2})
13.设计一个表示服务器的类。包含服务器的属性有:
• CPU 个数,
• 内存大小,
• 磁盘空间大小
• 操作系统类型(Linux, Windows)
其中操作系统类型设置为私有变量,外部不可以更改。
实现一个方法,输出服务器的属性内容为以下格式:
8 核CPU, 40G 内存, 150G 磁盘空间, Linux
class Server:
def __init__(self, CPU, Memory, Disk, OS):
self.CPU = CPU
self.Memory = Memory
self.Disk = Disk
self.__OS = OS
def server_info(self):
print("{0}核CPU,{1}G内存,{2}G磁盘空间,{3}系统".format(self.CPU, self.Memory, self.Disk, self.__OS))
if __name__ == "__main__":
server = Server(8, 40, 150, "Linux")
print("服务器配置信息如下:")
server.server_info()
# 尝试修改OS属性
server.__OS = "Windows"
print("------------------------------------")
print("修改后服务器配置信息如下:")
server.server_info()
from Task4_1 import Server
class SubServer(Server):
def server_price(self):
sum = "%.2f" %(self.CPU * 1527.679 + self.Memory * 100.21 + self.Disk * 50.789)
print("这款服务器配置如下:", end="")
server.server_info()
print("这种配置的服务器价格为:", sum, "元")
if __name__ == "__main__":
server = SubServer(8, 16, 1024, "Windows")
server.server_price()