1、8<<2等于?

32

  1. “<<”位运算
  2.  
  3.     264   132  64  32  16  8  4  2  1
  4.  
  5. 原始位置    0    0   0   0   0   1  0  0  0
  6.  
  7. 想左位移2 0    0   0   1   0   0  0  0  0

  

2、通过内置函数计算5除以2的余数

  1. a=divmod(5,2)
  2. print(a)
  3. --->(2,1) #2为商,1有余数
  4.  
  5. print(a[1])
  6. --->1

3、s=[1,"h",2,"e",[1,2,3],"l",(4,5),"l",{1:"111"},"o"],将s中的5个字符提取出来并拼接成字符串。

  1. s = [1, "h", 2, "e", [1, 2, 3], "l", (4, 5), "l", {1: "111"}, "o"]
  2. str_s=""
  3. for i in s:
  4. if type(i)==str:
  5. str_s+="".join(i)
  6. print(str_s)
  7. --->hello
  8.  
  9. str_s="".join([i for i in s if type(i)==str])
  10. print(str_s)
  11. --->hello

4、判断"yuan"是否在[123,(1,"yuan"),{"yuan":"handsome"},"yuanhao"],如何判断以及对应结果?

  1. s=[123, (1, "yuan"), {"yuan": "handsome"}, "yuanhao"]
  2. if "yuan" in s:
  3. print('"yuan"在列表s中')
  4. else:
  5. print('"yuan"不在列表s中')
  6.  
  7. --->"yuan"不在列表s

5、l=[1,2,3]

   l2=l.insert(3,"hello")


   print(l2)


   执行结果并解释为什么? 

  1. l=[1,2,3]
  2. l2=l.insert(3,"hello")
  3. print(l)
  4. --->[1, 2, 3, 'hello']
  5.  
  6. print(l2)
  7. --->None #因为l.insert(3,'hello')的执行结果是没有返回值的,所以打印l2什么也得不到

6、 a=[1,2,[3,"hello"],{"egon":"aigan"}]
  b=a[:]

  a[0]=5
  a[2][0]=666

  print(a)
  print(b)
  计算结果以及为什么?

  1. [5, 2, [666, 'hello'], {'egon': 'aigan'}]
  2. [1, 2, [666, 'hello'], {'egon': 'aigan'}]
  3. b相当于a的浅拷贝,当拷贝a中[3,"hello"]相当于只拷贝了一个内存地址,当劣列表里的元素改变时,
  4. b指向的内存地址并未发生改变,所以列表元素跟着一起改变

7 使用文件读取,找出文件中最长的行的长度(用一行代码解决)? 

  1. print(max([line for line in open("a.txt","r",encoding="utf8")]))

8.

def add(s, x):
  return s + x

def generator():
  for i in range(4):
    yield i

base = generator()
for n in [1, 11]:
  base = (add(i, n) for i in base)

print list(base)

  1. --->[22, 23, 24, 25]
  2.  
  3. base=[0,1,2,3]
  4. base1=(add(i, n) for i in [0,1,2,3])
  5. --->[11,12,13,14]
  6.  
  7. base2=(add(i, n) for i in [11,12,13,14])
  8. --->[22,23,24,25]

9

hello.py(gbk方式保存):
#coding:GBK
print(“老男孩”)

如果用py2,py3下在cmd下运行回报错吗?为什么并提出解决方案? (编码)

  1. 均不会报错,因为cmd默认编码方式为GBK,所以在python2python3中都不会报错

  1. 但在python2解释器中会报错,需要gbk模式解码。python3解释器中不会报错

10 通过函数化编程实现5的阶乘

  1. def func(n):
  2. if n == 1:
  3. return 1
  4. else:
  5. return n * func(n-1)
  6.  
  7. obj = func(3)
  8. print(obj)

11 打印如下图案:


  1. ***
  2. *****
  3. *******
  4. *****
  5. ***
  6. *
  7.  
  8. def func(number):
  9. for i in range(1,number,2):
  10. print(("*" * i).center(number))
  11. for i in range(number,0,-2):
  12. print(("*" * i).center(number))
  13.  
  14. func(7)

  

12 求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222,几个数相加以及a的值由键盘控制。

  1. def func(a,n):
  2. sum=0
  3. a=str(a)
  4. for i in range(1,n+1):
  5. x=int(a*i)
  6. sum+=x
  7. return sum
  8.  
  9. print(func(2,2))

13

def foo():
  print('hello foo')
  return()

def bar():
  print('hello bar')

(1)为这些基础函数加一个装饰器,执行对应函数内容后,将当前时间写入一个文件做一个日志记录。

  1. def timer(func):
  2. def wrapper():
  3. import time
  4. res = func()
  5. f = open('log', 'a+') #以追加的方式打开文件,没有则会创建
  6. s = time.asctime() #获取当前时间:Tue Apr 18 21:46:18 2017
  7. f.write(s + '\n') #将当前时间写入log文件,并换行
  8. f.close() #关闭log文件
  9. return res
  10. return wrapper
  11.  
  12. @timer
  13. def foo():
  14. print('hello foo')
  15. return ()
  16. @timer
  17. def bar():
  18. print('hello bar')
  19.  
  20. foo()
  21.  
  22. bar()

  

(2)改成参数装饰器,即可以根据调用时传的参数决定是否记录时间,比如@logger(True)

  1. def logger(choice):
  2. def timmer(func):
  3. def wrapper():
  4. import time
  5. if choice == True:
  6. res = func()
  7. f = open('log', 'a+') #以追加的方式打开文件,没有则会创建
  8. s = time.asctime() #获取当前时间:Tue Apr 18 21:46:18 2017
  9. f.write(s + '\n') #将当前时间写入log文件,并换行
  10. f.close() #关闭log文件
  11. return res
  12. else:
  13. pass
  14. return wrapper
  15. return timmer
  16.  
  17. @logger(True)
  18. def foo():
  19. print('hello foo')
  20. return ()
  21. @logger(True)
  22. def bar():
  23. print('hello bar')
  24.  
  25. foo()
  26.  
  27. bar()

18 三次登陆锁定:要求一个用户名密码输入密码错误次数超过三次锁定?

  1. #读取注册用户的信息,用户名,密码,输错次数,写入字典中
  2. user={}
  3. with open("DB1",encoding="utf8") as f:
  4. for line in f:
  5. username_list=line.strip().split("|") #username_list--->['egon', '123', '2']
  6. user[username_list[0]]={"name":username_list[0],
  7. "pwd":username_list[1],
  8. "times":username_list[2]}
  9. # print(user) #-->{'egon': {'name': 'egon', 'pwd': '123', 'times': '2'}, 'xuyaping': {'name': 'xuyaping', 'pwd': '123', 'times': '0'}, 'xyy': {'name': 'xyy', 'pwd': '123', 'times': '1'}}
  10.  
  11. #读取黑名单用户,将黑名单用户加入列表中
  12. with open("black_lockname",encoding="utf8") as f1:
  13. black_list=[]
  14. for line in f1:
  15. black_list.append(line.strip())
  16. # print(black_list)
  17.  
  18. while True:
  19. username = input("please input your username:").strip()
  20. passwd = input("please input your passwd:").strip()
  21. #用户在黑名单中
  22. if username in black_list:
  23. print("该用户为黑名单用户,请滚")
  24. break
  25.  
  26. # 用户为注册用户
  27. elif username in user:
  28. user[username]["times"]=int(user[username]["times"])
  29. if user[username]["times"]<3 and passwd==user[username]["pwd"]:
  30. print("登录成功")
  31. user[username]["times"]=0
  32. #将修改后的信息重新写入DB1中
  33. with open("DB1","w",encoding="utf8") as f3:
  34. for i in user:
  35. f3.write(i + "|" + user[i]["pwd"] + "|" + str(user[i]["times"]) + "\n")
  36. break
  37.  
  38. else:
  39. user[username]["times"]+=1
  40. print("登录错误")
  41. # 将修改后的信息重新写入DB1中
  42. with open("DB1", "w", encoding="utf8") as f3:
  43. for i in user:
  44. f3.write(i + "|" + user[i]["pwd"] + "|" + str(user[i]["times"]) + "\n")
  45. if user[username]["times"]==3:
  46. black_list.append(username)
  47. print("账户被锁定")
  48. # 将修改后的信息重新写入black_lockname中
  49. with open("black_lockname","w",encoding="utf8") as f4:
  50. for j in black_list:
  51. f4.write(j+ "\n")
  52. break
  53.  
  54. #用户不是注册用户
  55. else:
  56. print("该用户没有注册")
  57. break

参考博客:http://www.cnblogs.com/xuyaping/p/6679305.html 

python(36)- 测试题的更多相关文章

  1. [Leetcode][Python]36: Valid Sudoku

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 36: Valid Sudokuhttps://oj.leetcode.com ...

  2. Python基础测试题

    1,执行Python脚本的两种方式 答:一种是 交互式,命令行shell启动Python,输入相应代码得出结果,无保存,另一种是 脚本式,例如:python 脚本文件.py,脚本文件一直存在,可编辑, ...

  3. Python 36 死锁现象和递归锁、信号量、Event事件、线程queue

    一:死锁现象和递归锁 所谓死锁: 是指两个或两个以上的进程或线程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去.此时称系统处于死锁状态或系统产生了死锁,这些永远 ...

  4. Python 36 GIL全局解释器锁 、vs自定义互斥锁

    一:GIL全局解释器锁介绍 在CPython中,全局解释器锁(或GIL)是一个互斥锁, 它阻止多个本机线程同时执行Python字节码.译文:之所以需要这个锁, 主要是因为CPython的内存管理不是线 ...

  5. python 36 进程池、线程池

    目录 1. 死锁与递归锁 2. 信号量Semaphor 3. GIL全局解释器锁:(Cpython) 4. IO.计算密集型对比 4.1 计算密集型: 4.2 IO密集型 5. GIL与Lock锁的区 ...

  6. python.36的特性新定义初学者必看课程

    一.Python3.6新特性 1.新的格局化字符串办法 <p "="">新的格局化字符串办法,即在一般字符串前增加 f 或 F 前缀,其效果相似于str.fo ...

  7. python+selenium 自动化测试实战

    一.前言: 之前的文章说过, 要写一篇自动化实战的文章, 这段时间比较忙再加回家过11一直没有更新博客,今天整理一下实战项目的代码共大家学习.(注:项目是针对我们公司内部系统的测试,只能内部网络访问, ...

  8. 零基础学Python--------入门篇 第1章 初始Python

    入门篇 第1章  初始Python 1.1  Pyhton 概述 1.1.1 了解 Python Python,本义是指“蟒蛇”.1989年,荷兰人Guido van Rossum发明了一种面向对象的 ...

  9. Python面试常见的问题

    So if you are looking forward to a Python Interview, here are some most probable questions to be ask ...

  10. 用 Python 和 OpenCV 检测图片上的条形码(转载)

    原文地址:http://python.jobbole.com/80448/ 假设我们要检测下图中的条形码: # load the image and convert it to grayscale 1 ...

随机推荐

  1. Leetcode 423.从英文中重建数字

    从英文中重建数字 给定一个非空字符串,其中包含字母顺序打乱的英文单词表示的数字0-9.按升序输出原始的数字. 注意: 输入只包含小写英文字母. 输入保证合法并可以转换为原始的数字,这意味着像 &quo ...

  2. Django模板(filter过滤器{{ }}与tag标签{% %}应用)

     模板里面过滤器与标签的应用 templates模板里面的应用参考(主要应用在这里面) <!DOCTYPE html> <html lang="en"> & ...

  3. Balanced Lineup(ST)

    描述 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. On ...

  4. [译]pycache是什么?

    原回答: https://stackoverflow.com/questions/16869024/what-is-pycache 当你用python运行一个程序时,解释器首先将它编译成字节码(这是一 ...

  5. Linux 终端操作之「I/O Redirection」

    I/O 重定向是在终端运行程序时很常用的技巧,但是我对它所知甚少.今天我在 DigitalOcean 上发现了一篇很好的 tutorial.这篇随笔记录一下我的心得体会和发现的一个问题. I/O re ...

  6. 刷题总结——开车旅行(NOIP2012 set+倍增)

    题目: 题目描述 小 A 和小 B 决定利用假期外出旅行,他们将想去的城市从 1 到 N 编号,且编号较小的城市在编号较大的城市的西边,已知各个城市的海拔高度互不相同,记城市 i 的海拔高度为Hi,城 ...

  7. windows下 maven+selenium+testng项目搭建(七)

    Selenium2.47.1 + Maven3.3.9 + TestNG6.8.8 windows准备好以下环境 1. Jdk,环境变量配置 2. maven环境3. eclipse 开发工具 ,ec ...

  8. cf671B Robin Hood

    We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and his wits to s ...

  9. C语言第四题

    今天就一道题 阅读printf代码的具体实现,要求在阅读过程中要做下列的事 1.至少列出十个c标准库的方法,并且说明他们方法的含义,以及参数的含义 2.至少列出2个c标准库的引入(或者是依赖),并且说 ...

  10. 什么是GOP(转)

    所谓GOP,意思是画面组,MPEG格中的帧序列,分为I.P.B三种,如排成IBBPBBPBBPBBPBBP...样式,这种连续的帧图片组合即为GOP(画面群,GROUP OF PICTURE),是MP ...