python 基础习题
1、8<<2等于?
8 ---> 1000
32 ---> 100000
-----------结果---
32
2、通过内置函数计算5除以2的余数
print(dir()) #不带参数时,返回当前范围内的变量、方法和定义的类型列表,#找到__builtins__
print(dir(__builtins__)) #找内置函数 print(divmod(5,2)[1])
----------------------结果-----------
1
3、s=[1,"h",2,"e",[1,2,3],"l",(4,5),"l",{1:"111"},"o"],将s中的5个字符提取出来并拼接成字符串。
方法一: s1 = []
for n in s:
if type(n) == str:
s1.append(n)
print("".join(s1)) 方法二: print("".join([i for i in s if type(i) == str]))
4、判断"yuan"是否在[123,(1,"yuan"),{"yuan":"handsome"},"yuanhao"],如何判断以及对应结果?
s1 = [123,(1,"yuan"),{"yuan":"handsome"},"yuanhao"]
def foo(name):
if "yuan" in name:
print(name)
for i in name:
if type(i) == list or type(i) == tuple:
foo(i)
if type(i) == dict:
foo(i.keys())
foo(i.values())
foo(s1)
-----------结果为---------
(1, 'yuan')
dict_keys(['yuan'])
6、 a=[1,2,[3,"hello"],{"egon":"aigan"}]
b=a[:]
a[0]=5
a[2][0]=666
print(a)
print(b)
#计算结果以及为什么?
[5, 2, [666, 'hello'], {'egon': 'aigan'}]
[1, 2, [666, 'hello'], {'egon': 'aigan'}]
b相当于a的浅拷贝,当拷贝a中[3,"hello"]相当于只拷贝了一个内存地址,当劣列表里的元素改变时,b指向的内存地址并未发生改变,所以列表元素跟着一起改变
7 使用文件读取,找出文件中最长的行的长度(用一行代码解决)?
print(max([len(line) for line in open('file')]))
10 .通过函数化编程实现5的阶乘
方式一: def func(n):
if n == 1:
return 1
else:
return n * func(n-1) obj = func(3)
print(obj) 方式二: from functools import reduce def func(number):
obj = reduce(lambda x,y:x*y,range(1,number + 1))
return obj print(func(4))
11 打印如下图案:
*
***
*****
*******
*****
***
* def func(number):
for i in range(1,number,2):
print(("*" * i).center(number))
for i in range(number,0,-2):
print(("*" * i).center(number)) func(7)
12.
def outer():
count = 10
def inner():
nonlocal count #nonlocal 作用于外部作用域
count = 20
print(count)
inner()
print(count)
outer() 1.分析运行结果?
20
10
2.如何让两个打印都是20 def outer():
count = 10
def inner():
nonlocal count #nonlocal 作用于外部作用域
count = 20
print(count)
inner()
print(count)
outer()
13 输入一个年份,判断是否是闰年?
def func(year):
if (year%4 == 0 and year%100 != 0) or year%400 == 0:
return True
else:
return False print(func(2005)) judge = lambda year: True if (year%4 == 0 and year%100 != 0) or (year%400 == 0) else False
print(judge(2004))
14 任意输入三个数,判断大小?
def func(a,b,c):
if a >b:
if a >c:
print("%s最大"% a)
else:
print("%s最大" % c)
else:
if b >c:
print("%s最大" % b)
else:
print("%s最大" % c) func(1,2,3)
15 求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222,几个数相加以及a的值由键盘控制。
def func(number,count):
ret = 0
if int(count) > 1:
for i in range(1,int(count) + 1 ):
ret += int((str(number) * i))
return ret
else:
return number obj = func(2,3)
print(obj)
16.
f=open("a")
while 1:
choice=input("是否显示:[Y/N]:")
if choice.upper()=="Y":
for i in f:
print(i)
else:
break
请问程序有无bug,怎么解决?
--------------------------------结果---------------
f=open("a")
while 1:
choice=input("是否显示:[Y/N]:")
if choice.upper()=="Y":
for i in f:
print(i)
f.seek(0)
else:
break
f.close("a")
17
def foo():
print('hello foo')
return()
def bar():
print('hello bar')
(1)为这些基础函数加一个装饰器,执行对应函数内容后,将当前时间写入一个文件做一个日志记录。
(2)改成参数装饰器,即可以根据调用时传的参数决定是否记录时间,比如@logger(True)
import time def init(func):
def wrapper(*args,**kwargs):
a= str(time.time()) + "执行%s\n" % func
with open("record.txt","a+") as f:
f.write(a)
func(*args,**kwargs)
return wrapper @init
def foo():
print('hello foo')
return() @init
def bar():
print('hello bar') foo()
bar() import time def auth(flag):
def init(func):
def wrapper(*args,**kwargs):
if flag == True:
a= str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) + "执行%s\n" % func
with open("record.txt","a+") as f:
f.write(a)
func(*args,**kwargs)
return wrapper
return init @auth(True)
def foo():
print('hello foo')
return() @auth(True)
def bar():
print('hello bar') foo()
bar()
18.
list3 = [
{"name": "alex", "hobby": "抽烟"},
{"name": "alex", "hobby": "喝酒"},
{"name": "alex", "hobby": "烫头"},
{"name": "alex", "hobby": "Massage"},
{"name": "wusir", "hobby": "喊麦"},
{"name": "wusir", "hobby": "街舞"},] a = [
{"name":"alex","hobby":[1,2,3,4]},
{"name":"wusir","hobby":[1,2,3,4]}
]

19 三次登陆锁定:要求一个用户名密码输入密码错误次数超过三次锁定?
accounts = {}
def lock_user(name):
with open("lock_user", mode="r+", encoding="utf8") as f_lock:
for line in f_lock:
if line.strip().split()[0] == name:
print("Lock user")
exit()
def lockd_user(**kwargs):
with open("lock_user",mode="a+",encoding="utf8") as f_lockd_user:
for key in kwargs:
if kwargs[key] >2:
f_lockd_user.write(key + "\n")
def check_user(name,passwd):
with open("user",mode="r",encoding="utf8") as f_check:
for line in f_check:
if name == line.strip().split()[0]:
if passwd == line.strip().split()[1]:
print("Success")
exit()
else:
add_error(name)
return name
return name
def add_error(name):
if accounts:
if name in accounts:
accounts[name] += 1
else:
accounts[name] = 1
else:
accounts[name] = 1
def main():
count = 0
while True:
name = input("input name: ")
passwd = input("input passwd: ")
lock_user(name) #判断用户是否锁定
name = check_user(name,passwd) #判断用户
count += 1
if count > 2:
lockd_user(**accounts)
print("exit than three")
break
if __name__ == '__main__':
main()
python 基础习题的更多相关文章
- 从入门到自闭之Python 基础习题训练
""" name = input(">>>")通过代码来验证name变量是什么数据类型? """ na ...
- python 基础 习题
1.执行 Python 脚本的两种方式2.简述位.字节的关系 1Byte = 8bits 3.简述 ascii.unicode.utf-8.gbk 的关系 都是字符集,unicode兼容其他3种字符集 ...
- 001_02-python基础习题答案
python 基础习题 执行 Python 脚本的两种方式 如:脚本/python/test.py 第一种方式:python /python/test.py 第二中方式:在test.py中声明:/us ...
- python基础1习题练习
python基础1习题练习: #encoding:utf-8 #1.实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败! name=input('na ...
- Python 基础练习
今天接触了python,了解了一下 python 的基础语法,于是想着手训练一下,在本习题集中,参考代码为提供的参考答案,前面的代码为自己思考的代码,最后每道题给出练习的时间. Python 基础练习 ...
- python基础篇实战
1. 判断下面的结果 # 1. 判断下面的结果 # 1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 pri ...
- PJzhang:python基础进阶的10个疗程-two
猫宁!!! 第2节:python基本图形绘制 保留字是python基础语法的支撑 默写python代码是件挺恐怖的事情!!! 2008年android操作系统诞生 计算时代 编程语言也是一个江湖 C语 ...
- python基础全部知识点整理,超级全(20万字+)
目录 Python编程语言简介 https://www.cnblogs.com/hany-postq473111315/p/12256134.html Python环境搭建及中文编码 https:// ...
- 入门python有什么好的书籍推荐?纯干货推荐,你值得一看 python基础,爬虫,数据分析
Python入门书籍不用看太多,看一本就够.重要的是你要学习Python的哪个方向,或者说你对什么方向感兴趣,因为Python这门语言的应用领域比较广泛,比如说可以用来做数据分析.机器学习,也可以用来 ...
随机推荐
- POJ1269:Intersecting Lines——题解
http://poj.org/problem?id=1269 题目大意:给四个点,求前两个点所构成的直线和后两个点所构成的直线的位置关系(平行,重合,相交),如果是相交,输出交点坐标. ——————— ...
- BZOJ1901:Zju2112 Dynamic Rankings——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=1901 Description 给定一个含有n个数的序列a[1],a[2],a[3]……a[n],程序 ...
- HDU2089:不要62——题解
http://acm.hdu.edu.cn/showproblem.php?pid=2089 Problem Description 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer). 杭州交通管 ...
- Codevs1169:传纸条——题解
题目描述 Description 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个m行n列的矩阵,而小渊和小轩被安排在矩阵对角线的两端,因此,他们就 ...
- BZOJ1042 [HAOI2008]硬币购物 【完全背包 + 容斥】
1042: [HAOI2008]硬币购物 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2924 Solved: 1802 [Submit][St ...
- YBT 2.4 AC自动机
其实这个专题NOIP几乎不考 AC自动机,就是能让题自动AC的东西,是不是十分神奇 对的,就是这么神奇 AC自动机是解决多模式串与文本串匹配的问题 是KMP+Trie树的结合,也是一个毒瘤算法 Key ...
- linux 常见服务端口
Linux服务器在启动时需要启动很多系统服务,它们向本地和网络用户提供了Linux的系统功能接口,直接面向应用程序和用户.提供这些服务的程序是由运行在后台的守护进程(daemons) 来执行的.守护进 ...
- Efficient Deblurring for Shaken and Partially Saturated Images
Try the online demo: http://willow-fd.rocq.inria.fr/unshake/ Overview One common feature of “shaken” ...
- HDU4081:Qin Shi Huang's National Road System (任意两点间的最小瓶颈路)
Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3 ...
- could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.5 or one of(maven报错)
could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.5 or one of ...