S5第一次月考
# Python五期月考一
# 1 介绍
# 满分100分,90分及格
#
# 考试范围:
# 1.Python语法
# 2.数据类型
# 3.流程控制
# 4.函数
# 5.模块
#
# 考试时间:
# 周五下午2.00点-5.30
# 2 基础题(60分)
# 1.执行python脚本的两种方式是?(1分)
# python a.py直接调用python脚本,第一行要有#!/usr/bin/env python
# ./a.py调用python 解释器来调用python脚本
# 2.python如何实现单行注释和多行注释(1分)
# 单行注释 #
# 多行注释''''''
# 3.在python3中bytes类型与unicode类型二者之间如何相互转换?(1分)
# bytes--->decode--->unicode
# unicode--->encode--->bytes
# 4.创建一个对象有三个特性:身份id,类型,值
# 等号比较的是什么?(1分)
# 值
# is比较的是什么?(1分)
# id
# 5.使用链式赋值的方式将10赋值给变量x、y、z(1分)
# x=y=z=10
# 6.有列表data=['alex',49,[1900,3,18]],分别取出列表中的名字,年龄,出生的年,月,日赋值给不同的变量(1分)
# data=['alex',49,[1900,3,18]]
# name=data[0]
# age=data[1]
# year=data[2][0]
# month=data[2][1]
# day=data[2][2]
# 7.所有数据类型自带布尔值,布尔值为假的有?(1分)
# 0 None {} [] () ''
# 8.常用数据类型有:字符串,列表,元组,字典,集合,请分类
# 按照存值个数分类?(1分)
# 一个值:数字 字符串
# 多个值:列表 字典 集合 元组
# 按照可变\不可变类型分类?(1分)
# 可变:列表 字典 集合
# 不可变:字符串 数字 元组
# 按照取值方式分类(直接取值,按索引,按key取)?(1分)
# 直接取值:数字
# 按索引: 字符串 列表 元组
# 按key:字典
# 按照有序\无序分类?(1分)
# 有序:字符串 列表 元组 数字
# 无序:字典 集合
# 9.阅读代码,请写出执行结果 (1分)
# a = "alex"
# b = a.capitalize()
# print(a)
# print(b)
# 执行结果是alex,Alex
# 10.阅读代码,请写出执行结果
# 代码一:(1分)
# if True or False and False:
# print('yes')
# else:
# print('no')
# and优先级比or更高,先计算 False and False,得到False,True or False得到True,输出的是yes
# 代码二:(1分)
# if (True or False) and False:
# print('yes')
# else:
# print('no')
#括号优先级比and更高,先计算括号里的True or False,得到True,True and False 得到False,输出no
# 11.写代码,有如下变量,请按照要求实现每个功能 (共6分,每小题各0.5分)
# name = " aleX"
# 1)移除 name 变量对应的值两边的空格,并输出处理结果
# print(name.strip())
# 2)判断 name 变量对应的值是否以 "al" 开头,并输出结果
# print(name.startswith('al'))
# 3)判断 name 变量对应的值是否以 "X" 结尾,并输出结果
# print(name.endswith('X'))
# 4)将 name 变量对应的值中的 “l” 替换为 “p”,并输出结果
# print(name.replace('l','p'))
# 5)将 name 变量对应的值根据 “l” 分割,并输出结果。
# print(name.split('l'))
# 6)将 name 变量对应的值变大写,并输出结果
# print(name.upper())
# 7)将 name 变量对应的值变小写,并输出结果
# print(name.lower())
# 8)请输出 name 变量对应的值的第 2 个字符?
# print(name[1])
# 9)请输出 name 变量对应的值的前 3 个字符?
# print(name[:3])
# 10)请输出 name 变量对应的值的后 2 个字符?
# print(name[-2:])
# 11)请输出 name 变量对应的值中 “e” 所在索引位置?
# print(name.index('e'))
# 12)获取子序列,去掉最后一个字符。如: oldboy 则获取 oldbo。
# print(name[:-1])
# 12.输出1-100内所有的奇数(1分)
# for i in range(1,101):
# if i%2==1:
# print(i)
# 13.使用while循环输出1 2 3 4 5 6 8 9 10(1分)
# n=0
# while n<10:
# n+=1
# if n==7:
# continue
# print(n)
# 14.求1-2+3-4+5 ... 99的所有数的和(1分)
# n=0
# s=0
# while n<99:
# n+=1
# if n%2==1:
# s+=n
# elif n%2==0:
# s-=n
# print(s)
# 15.编写for循环,利用索引遍历出每一个字符(1分)
# msg='hello egon 666'
# for i in range(len(msg)):
# print(msg[i])
# 16.编写while循环,利用索引遍历出每一个字符(1分)
# msg='hello egon 666'
# n=0
# while n<len(msg):
# print(msg[n])
# n+=1
# 17.有变量msg='/etc/a.txt|365|get'(1分)
# 将该变量中的文件名,文件大小,操作方法切割出来
# msg='/etc/a.txt|365|get'
# a=msg.split('|')
# b=a[0].split('/')
# print(b[2],a[1],a[2])
# 18.msg='hello alex'中的alex替换成SB(1分)
# msg='hello alex'
# print(msg.replace('alex','SB'))
# 19.编写while循环,让用户输入用户名和密码,如果用户为空或者数字,则重新输入(1分)
# while True:
# user=input('user:')
# pwd=input('passwd:')
# if not user or user.isdigit():
# continue
# break
# 20.有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中。(2分)
# 即: {'k1': 大于66的所有值, 'k2': 小于66的所有值}
# a = [11,22,33,44,55,66,77,88,99]
# b = []
# c = []
# dict = {"k1":b,"k2":c}
# for i in a:
# if i >66:
# b.append(i)
# if i <66:
# c.append(i)
# print(dict)
# 21.有如下两个集合,pythons是报名python课程的学员名字集合,linuxs是报名linux课程的学员名字集合
# pythons={'alex','egon','yuanhao','wupeiqi','cobila','biubiu'}
# linuxs={'wupeiqi','oldboy','cobila'}
# 求出即报名python又报名linux课程的学员名字集合(2分)
# print(pythons&linuxs)
# 求出所有报名的学生名字集合(2分)
# print(pythons|linuxs)
# 求出只报名python课程的学员名字(2分)
# print(pythons-linuxs)
# 求出没有同时这两门课程的学员名字集合(2分)
# print(pythons^linuxs)
# 22.统计s='hello alex alex say hello sb sb'中每个单词的个数(3分)
# 结果如下:
# {'hello': 2, 'alex': 2, 'say': 1, 'sb': 2}
# s='hello alex alex say hello sb sb'
# l=s.split()
# s=set(l)
# dict={}
# for i in s:
# dict[i]=l.count(i)
# print(dict)
# 第二种
# s='hello alex alex say hello sb sb'
# l=s.split()
# dic={}
# for key in l:
# if key in dic:
# dic[key]+=1
# else:
# dic[key]=1
# print(dic) # 23.简述什么是可迭代对象与迭代器对象?(2分)
# 有__iter__的是可迭代对象
# 有__next__的是迭代器对象
# 判断下列数据类型是可迭代对象or迭代器(2分)
# s='hello'
# l=[1,2,3,4]
# t=(1,2,3)
# d={'a':1}
# set={1,2,3}
# f=open('a.txt')
# 分别用依赖索引和不依赖索引两种方式迭代上述对象(2分)
# from collections import Iterable
# from collections import Iterator
# s='hello'
# l=[1,2,3,4]
# t=(1,2,3)
# d={'a':1}
# set={1,2,3}
# f=open('a.txt')
# print(isinstance(s,Iterable))
# print(isinstance(l,Iterable))
# print(isinstance(t,Iterable))
# print(isinstance(d,Iterable))
# print(isinstance(set,Iterable))
# print(isinstance(f,Iterable))
#
# print(isinstance(s,Iterator))
# print(isinstance(l,Iterator))
# print(isinstance(t,Iterator))
# print(isinstance(d,Iterator))
# print(isinstance(set,Iterator))
# print(isinstance(f,Iterator))
# 都是可迭代对象,f是迭代器
# for i in range(len(s)):
# print(s[i])
# for i in s:
# print(i)
# for i in range(len(l)):
# print(l[i])
# for i in l:
# print(i)
# for i in range(len(t)):
# print(t[i])
# for i in t:
# print(i)
# for i in d:
# print(i)
# for i in f:
# print(i)
# 24.文件a.txt内容:每一行内容分别为商品名字,价钱,个数
# apple 10 3
# tesla 100000 1
# mac 3000 2
# lenovo 30000 3
# chicken 10 3
# 要求一:使用列表解析,从文件a.txt中取出每一行,做成下述格式(2分)
# [{‘name’:'apple','price':10,'count':3},{...},{...},...]
# with open('a.txt','w',encoding='utf-8')as f:
# f.write('apple 10 3\ntesla 100000 1\nmac 3000 2\nlenovo 30000 3\nchicken 10 3')
# with open('a.txt',encoding='utf-8')as f:
# l=[{'name':line.split()[0],'price':line.split()[1],'count':line.split()[2]} for line in f]
# print(l)
# 要求二:求出总共消费了多少钱(5分)
# res=sum(float(good['price']) * int(good['count']) for good in l)
# print(res)
# 25.写函数,计算传入字符串中【数字】、【字母】、【空格】 以及 【其他】的个数,并返回结果,如{‘num’:3,’str’:2,’space’:3,’others’:3}(5分)
# def func(n):
# num1=0
# num2=0
# num3=0
# num4=0
# for i in n:
# if i.isdigit():
# num1+=1
# elif i.isspace():
# num2+=1
# elif i.isalpha():
# num3+=1
# else:
# num4+=1
# print({'num':num1,'str':num2,'space':num3,'others':num4})
# func(input('请输入:'))
# 3 综合题(40分)
# 1.利用递归调用,取出[1,[2,3,[4,5,[6,7,[8,[9,10,[11,[12,[13,14,15]]]]]]]]]中所有的值(10分)
# l=[1,[2,3,[4,5,[6,7,[8,[9,10,[11,[12,[13,14,15]]]]]]]]]
# def foo(l):
# for i in l:
# if type(i)==list:#if isinstance(i,list):
# foo(i)
# else:
# print(i)
# foo(l)
# 2.编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件),要求登录成功一次,后续的函数都无需再输入用户名和密码(10分)
# 注意:从文件中读出字符串形式的字典,可以用json或pickle
# user_dic={
# 'z':'123',
# 'x':'456',
# 'y':'789',
# }
# with open('db.txt','w',encoding='utf-8') as f:
# f.write(str(user_dic))
# with open('db.txt','r',encoding='utf-8') as f:
# res=f.read()
# user_dic=eval(res)
# db_path='db.txt'
# login_dic={
# 'user':None,
# 'status':False,
# }
# def auth(func):
# def wrapper(*args,**kwargs):
# if login_dic['user'] and login_dic['status']:
# res = func(*args, **kwargs)
# return res
# name=input('your name: ')
# password=input('your password: ')
# with open(db_path,'r',encoding='utf-8') as f:
# user_dic=eval(f.read())
# if name in user_dic and password == user_dic[name]:
# print('login ok')
# login_dic['user']=name
# login_dic['status']=True
# res=func(*args,**kwargs)
# return res
# else:
# print('login err')
# return wrapper
# @auth
# def index():
# print('welecome to index')
# @auth
# def home(name):
# print('welecome %s to home page' %name)
# index()
# home('z')
#标准答案
# import json
# db_path=r'E:\Pycharmlx\基础,函数,装饰器,模块,包\homework\r.txt'
#
# login_dic={
# 'user':None,
# 'status':False
# }
# def auth(func):
# def wrapper(*args,**kwargs):
# if login_dic['user'] and login_dic['status']:
# res = func(*args,**kwargs)
# return res
# name=input('your name:')
# pwd=input('your pwd:')
# with open(db_path,'r',encoding='utf8') as f:
# user_dic=json.loads(f.read())
# if name in user_dic and pwd == user_dic[name]:
# print('login ok')
# login_dic['user']=name
# login_dic['status']=True
# res=func(*args,**kwargs)
# return res
# else:
# print('login error')
# return wrapper
# @auth
# def index():
# print('welcome to index page')
#
# @auth
# def home():
# print('welcome to home page')
# index()
# home()
# 3.编写函数实现每次执行函数都得到一个唯一的哈希值(可以使用time.clock()作为哈希的依据,来保证每次哈希得到的值都不一样) (10分)
# import time,hashlib
# def func():
# md5=hashlib.md5()
# t=str(time.clock()).encode('utf8')
# md5.update(t)
# return md5.hexdigest()
# 4.一个文件夹中,存在多个文件,包括图片,视频,文本等等,遍历出时间在2017-06-05至2017-06-09这段时间内创建的所有文件
# 具体文件夹,自己创建。(10分)
# import os
# import time
# start=time.mktime(time.strptime('2017-06-05','%Y-%m-%d'))
# stop=time.mktime(time.strptime('2017-06-09','%Y-%m-%d'))
# g=os.walk(r'D:\py3.6保存代码\py_fullstack_s5\作业')
# for far_dir,sub_dir,files in g:
# for file in files:
# b=r'%s\%s'%(far_dir,file)#文件与路径的拼接
# f=os.stat(b).st_ctime
# if f >= start and f <= stop:
# print(b)
#标准
# import time
# import os
# def file_time():
# start=time.mktime(time.strptime('2017-06-20','%Y-%m-%d'))
# stop=time.mktime(time.strptime('2017-06-26','%Y-%m-%d'))
# g=os.walk(r'D:\mypython\demo2')
# for dir,_,files in g:
# for file in files:
# file_path=os.path.join(dir,file)
# file_stamp=os.path.getctime(file_path)
# if start< file_stamp <stop:
# ret = time.strftime('%Y-%m-%d', time.localtime(file_stamp))
# ret2 = os.path.join(dir, file, ret)
# print(ret2)
# file_time()
S5第一次月考的更多相关文章
- 上海python14期第一次周考
上海python14期第一次周考 1 介绍 满分50分 考试范围: Python语法 数据类型 流程控制 考试时间: 周五下午3.00点-晚6:00 2 基础题(38分) 什么是编程语言?什么是语言? ...
- 月考(cogs 1176)
[题目描述] 在上次的月考中Bugall同学违反了考场纪律还吃了处分,更可气的是在第二天的校会时 间学校就此事做了全校通报. 现已知在当天校会时间有总共N个同学听到了有关Bugall的处分决定. B ...
- C#学习笔记(二十):C#总结和月考讲解
m1w1d2_console_variable_constant 输入Console.WriteLine(); 输出Console.ReadLine(); 快捷键 折叠代码:快捷键“Ctrl+ K + ...
- 洛谷 P4256 公主の#19准备月考
题目背景 公主在玩完游戏后,也要月考了.(就算是公主也要月考啊QWQ) 题目描述 公主的文综太差了,全校排名1100+(全校就1100多人),她分析了好久,发现她如果把所有时间放在选择题上,得分会比较 ...
- cogs 1176. [郑州101中学] 月考
1176. [郑州101中学] 月考 ★ 输入文件:mtest.in 输出文件:mtest.out 简单对比时间限制:1 s 内存限制:128 MB [题目描述] 在上次的月考中Bug ...
- COGS——C1176. [郑州101中学] 月考
http://cogs.pro/cogs/problem/problem.php?pid=1176 [题目描述] 在上次的月考中Bugall同学违反了考场纪律还吃了处分,更可气的是在第二天的校会时 间 ...
- cogs 1176. [郑州101中学] 月考 Set 做法
1176. [郑州101中学] 月考 ★★☆ 输入文件:mtest.in 输出文件:mtest.out 简单对比时间限制:1 s 内存限制:128 MB [题目描述] 在上次的月考中B ...
- cogs 1176. [郑州101中学] 月考 Map做法
1176. [郑州101中学] 月考 ★★☆ 输入文件:mtest.in 输出文件:mtest.out 简单对比时间限制:1 s 内存限制:128 MB [题目描述] 在上次的月考中B ...
- cogs 1176. [郑州101中学] 月考 字典树
1176. [郑州101中学] 月考 ★★☆ 输入文件:mtest.in 输出文件:mtest.out 简单对比时间限制:1 s 内存限制:128 MB [题目描述] 在上次的月考中B ...
随机推荐
- logback参考配置
logback配置 <?xml version="1.0" encoding="UTF-8"?> <configuration> < ...
- iis 如何搭建url 重定向,实现无线端和pc端不同的跳转
第一步,下载安装ARR(Application Request Routing), http://www.iis.net/downloads/microsoft/application-request ...
- 什么是AOP和OOP,IOC和DI有什么不同?
什么是AOP和OOP,IOC和DI有什么不同? 解答: 1)面向对象编程(Object Oriented Programming,OOP,面向对象程序设计)是一种计算机编程架构.AOP是OOP的延续, ...
- jsp有哪些内置对象?作用分别是什么?(至少三个)
jsp有哪些内置对象?作用分别是什么?(至少三个) 解答: 1)request表示HttpServletRequest对象.它包含了有关浏览器请求的信息,并且提供了几个用于获取cookie, head ...
- 网络协议之p2p---一个简单的nat 穿透demo
http://www.pudn.com/downloads64/sourcecode/p2p/detail228131.html http://www.pudn.com/downloads15/sou ...
- mybatis 入门学习
所须要jar包:mybatis-3.x.x.jar .假设须要和spring整合,还须要增加相关的包 1:看项目文件夹 红颜色不要关心 2:依照步骤: 1:增加jar包 2:创建数据源(configu ...
- Redis "MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk"问题
今天在程序中,jedis put数据到redis过程中,“MISCONF Redis is configured to save RDB snapshots, but is currently not ...
- iOS -转载-使用Navicat查看数据表的ER关系图
Navicat软件真是一个好东西.今天需要分析一个数据库,然后想看看各个表之间的关系,所以需要查看表与表之间的关系图,专业术语叫做ER关系图. 默认情况下,Navicat显示的界面是这样的: 软件将表 ...
- python中函数参数*args和**kw的区别
1.函数与参数(实参) 在python中创建函数是def,创建函数名是def f(),f函数名字,def f(a,b),这里的a,b是两个参数,函数名是自定义的,参数也是自定义,随意就好.看图如下效果 ...
- uiautomatorviewer.bat使用方法
在android目录下找到uiautomatorviewer.bat,然后双击,页面的第二个按钮连接设备 D:\Program Files\android-sdk-windows\tools\uiau ...