本练习为复习python的符号和关键字

关键字有:

  1. #and or False True
  2. print(1==0 and 2==0, 1==0 or 2==0)
  3. print(False)
  4. print(True)
    输出:

False False
False
True

  1. lists = ['1', '2', 'a', 'afds', 3, 463]
  2. """
  3. del:Deletion of a target list recursively deletes each target, from left to right.
  4. for……in print
  5. if……elif……else
  6. """
  7. # del lists[] values/name
  8. for i in range(0,len(lists)):
  9. print(lists, end='\t')
  10. del lists[0]
  11. print(lists)
  12. del lists
  13. if 'lists' not in locals().keys():
  14. print("lists is not variable")
  15. elif 'lists' in locals().keys():
  16. print("lists is variable")
  17. else:
  18. pass
    输出结果:

['1', '2', 'a', 'afds', 3, 463] ['2', 'a', 'afds', 3, 463]
['2', 'a', 'afds', 3, 463] ['a', 'afds', 3, 463]
['a', 'afds', 3, 463] ['afds', 3, 463]
['afds', 3, 463] [3, 463]
[3, 463] [463]
[463] []
lists is not variable

  1. """
  2. pass: "pass" is a null operation
  3. def class
  4. """
  5. def temp_function(): # a function that does nothing (yet)
  6. pass
  7. class temp_class: # a class with no methods (yet)
  8. pass
  1. """
  2. with EXPRESSION as TARGET:
  3. SUITE
  4. is semantically equivalent to:
  5.  
  6. manager = (EXPRESSION)
  7. enter = type(manager).__enter__
  8. exit = type(manager).__exit__
  9. value = enter(manager)
  10. hit_except = False
  11.  
  12. try:
  13. TARGET = value
  14. SUITE
  15. except:
  16. hit_except = True
  17. if not exit(manager, *sys.exc_info()):
  18. raise
  19. finally:
  20. if not hit_except:
  21. exit(manager, None, None, None)
  22. """
  23. with open("temp.txt") as fp:
  24. lines = fp.readlines()
  25. i = 0
  26. while i < len(lines):
  27. if i == 3:
  28. i += 1
  29. continue
  30. print("%d:" % i, lines[i])
  31. i += 1
  32. #is semantically equivalent to:
  33. try:
  34. fp = open("temp.txt")
  35. lines = fp.readlines()
  36. i = 0
  37. while i < len(lines):
  38. if i == 3:
  39. i += 1
  40. continue
  41. print("%d:" % i, lines[i])
  42. i += 1
  43. except:
  44. print("ERROR!!")
  45. else:
  46. print("There nothing to print!")
  47. finally:
  48. print("This is finally print!")

    输出结果:

0: a

1: applw

2: sjklfjs

4: 发的是加费的

5: drwjksjfr
0: a

1: applw

2: sjklfjs

4: 发的是加费的

5: drwjksjfr
There nothing to print!
This is finally print!

  1.  

tuples = ('1', '2', 'a', 'afds', 3, 463)
   sets = {'1', '2', 'a', 'afds', 3, 463}

  1. """
  2. is
  3. """
  4. if tuples is not sets:
  5. print("tuple is noe sets!")
    输出结果:
    tuple is noe sets!
  1. """
  2. yield: *generator* function
  3. yield_stmt ::= yield_expression
  4. """
  5. def foo(n):
  6. a, b = 0, 1
  7. while 0 < n:
  8. print(b)
  9. a, b = b, a+b
  10. n -=1
  11. def foo1(n):
  12. a, b = 0, 1
  13. L = []
  14. while 0 < n:
  15. L.append(b)
  16. print(L)
  17. a, b = b, a+b
  18. n -=1
  19. return L
  20. def foo2(n):
  21. a, b = 0, 1
  22. while 0 < n:
  23. yield b
  24. a, b = b, a+b
  25. n -=1
  26.  
  27. foo(5)
  28. foo1(5)
  29. for i in foo2(5):
  30. print(i)
    输出结果:

1
1
2
3
5
[1]
[1, 1]
[1, 1, 2]
[1, 1, 2, 3]
[1, 1, 2, 3, 5]
1
1
2
3
5

  1. """
  2. lambda
  3. lambda_expr ::= "lambda" [parameter_list] ":" expression
  4. def <lambda>(parameters):
  5. return expression
  6. """
  7. fp = lambda para : open(para)
  8. print(fp("temp.txt").readlines())
  1. """
  2. async for TARGET in ITER:
  3. SUITE
  4. else:
  5. SUITE2
  6. iter = (ITER)
  7. iter = type(iter).__aiter__(iter)
  8. running = True
  9.  
  10. while running:
  11. try:
  12. TARGET = await type(iter).__anext__(iter)
  13. except StopAsyncIteration:
  14. running = False
  15. else:
  16. SUITE
  17. else:
  18. SUITE2
  19. """

import asyncio
import time

  1. async def test2(i):
  2. r = await other_test(i) #uspend the execution of *coroutine* on an *awaitable* object.
  3. print("*****",i,r)
  4.  
  5. async def other_test(i):
  6. r = i
  7. print(i,end="*\t")
  8. await asyncio.sleep(0)
  9. print(time.time()-start)
  10. return r
  11.  
  12. url = ["https://segmentfault.com/p/1210000013564725",
  13. "https://www.jianshu.com/p/83badc8028bd",
  14. "https://www.baidu.com/"]
  15.  
  16. loop = asyncio.get_event_loop() # Get the event loop for the current context.
  17. task = [asyncio.ensure_future(test2(i)) for i in url] #Wrap a coroutine or an awaitable in a future.
  18. start = time.time()
  19. loop.run_until_complete(asyncio.wait(task)) #Wait for the Futures and coroutines given by fs to complete.
  20. endtime = time.time()-start
  21. print(endtime)
  22. loop.close()
    输出结果:

https://segmentfault.com/p/1210000013564725* https://www.jianshu.com/p/83badc8028bd* https://www.baidu.com/* 0.0
***** https://segmentfault.com/p/1210000013564725 https://segmentfault.com/p/1210000013564725
0.0019965171813964844
***** https://www.jianshu.com/p/83badc8028bd https://www.jianshu.com/p/83badc8028bd
0.0029840469360351562
***** https://www.baidu.com/ https://www.baidu.com/
0.0039823055267333984

  1. """
  2. local global nonlocal
  3. """
  4. sets = {'1', '2', 'a', 'afds', 3, 463}
  5. def temp_global():
  6. global sets
  7. print("temp_global", sets)
  8. sets.add('temp') #+
  9. print(1, sets)
  10. sets.update('temp') #+ 重复的就不添加了,可以添加多个
  11. print(2, sets)
  12. sets.remove('1') #- 没有会提示错误
  13. print(3, sets)
  14. sets.discard('temp') #- 没有不提示错误
  15. sets.pop() #- 随机删除
  16. print(4, sets)
  17. def temp_local():
  18. print("temp_local", sets)
  19. sets.add('1') #+
  20. print(1, sets)
  21. sets.update('temp') #+ 重复的就不添加了,可以添加多个
  22. print(2, sets)
  23. sets.remove('1') #- 没有会提示错误
  24. print(3, sets)
  25. sets.discard('temp') #- 没有不提示错误
  26. sets.pop() #- 随机删除
  27. print(4, sets)
  28. def temp_nonlocal():
  29. name = ['1', '2']
  30. def temp_nonlocal_fuc():
  31. nonlocal name
  32. name.append('3')
  33. return name
  34. return temp_nonlocal_fuc
  35.  
  36. temp_global()
  37. print("*", sets)
  38. temp_local()
  39. print("**", sets)
  40. tn = temp_nonlocal()
  41. print(tn()) #不可单独调用
  42. print(tn())
    输出结果:

temp_global {'2', 3, 'a', 'afds', 463, '1'}
1 {'2', 3, 'a', 'temp', 'afds', 463, '1'}
2 {3, 463, 'e', 'm', '2', 'a', 'afds', 'p', '1', 't', 'temp'}
3 {3, 463, 'e', 'm', '2', 'a', 'afds', 'p', 't', 'temp'}
4 {463, 'e', 'm', '2', 'a', 'afds', 'p', 't'}
* {463, 'e', 'm', '2', 'a', 'afds', 'p', 't'}
temp_local {463, 'e', 'm', '2', 'a', 'afds', 'p', 't'}
1 {463, 'e', 'm', '2', 'a', 'afds', 'p', 't', '1'}
2 {463, 'e', 'm', '2', 'a', 'afds', 'p', 't', '1'}
3 {463, 'e', 'm', '2', 'a', 'afds', 'p', 't'}
4 {'e', 'm', '2', 'a', 'afds', 'p', 't'}
** {'e', 'm', '2', 'a', 'afds', 'p', 't'}
['1', '2', '3']
['1', '2', '3', '3']

Learn Python the Hard Way,ex37-1的更多相关文章

  1. Learn Python the Hard Way,ex37-2

    本练习为复习python的符号和关键字 数据类型有:True False None Strings numbers floats lists dict tuple set ""&q ...

  2. [IT学习]Learn Python the Hard Way (Using Python 3)笨办法学Python3版本

    黑客余弦先生在知道创宇的知道创宇研发技能表v3.1中提到了入门Python的一本好书<Learn Python the Hard Way(英文版链接)>.其中的代码全部是2.7版本. 如果 ...

  3. 笨办法学 Python (Learn Python The Hard Way)

    最近在看:笨办法学 Python (Learn Python The Hard Way) Contents: 译者前言 前言:笨办法更简单 习题 0: 准备工作 习题 1: 第一个程序 习题 2: 注 ...

  4. 《Learn python the hard way》Exercise 48: Advanced User Input

    这几天有点时间,想学点Python基础,今天看到了<learn python the hard way>的 Ex48,这篇文章主要记录一些工具的安装,以及scan 函数的实现. 首先与Ex ...

  5. 学 Python (Learn Python The Hard Way)

    学 Python (Learn Python The Hard Way) Contents: 译者前言 前言:笨办法更简单 习题 0: 准备工作 习题 1: 第一个程序 习题 2: 注释和井号 习题 ...

  6. 算是休息了这么长时间吧!准备学习下python文本处理了,哪位大大有好书推荐的说下!

    算是休息了这么长时间吧!准备学习下python文本处理了,哪位大大有好书推荐的说下!

  7. python安装完毕后,提示找不到ssl模块的解决步骤

    转载自 醇酒醉影 python安装完毕后,提示找不到ssl模块: [root@localhost ~]# python2.7.5 Python 2.7.5 (default, Jun 3 2013, ...

  8. linux下,Python 多版本共存,及Pip,Easy_install 安装扩展包

    Python2与Python3共存 安装Python3后,建立ln,使用Python(Python2),Python3 来区分两个版本 使用sudo apt-get install python3-s ...

  9. python学习03——设计,与input有关

    笨办法学python第36节,我写的代码如下: from sys import exit def rule(): print "Congratulations! You made the r ...

随机推荐

  1. Codeforces Round #656 (Div. 3) A. Three Pairwise Maximums (数学)

    题意:给你三个正整数\(x\),\(y\),\(z\),问能够找到三个正整数\(a\),\(b\),\(c\),使得\(x=max(a,b)\),\(y=max(a,c)\),\(z=max(b,c) ...

  2. 递归实现jsonTree

    using System;using System.Collections.Generic;using System.Text;using WeChatApi.Model;using System.L ...

  3. 6.Header交换机之模拟验证用户身份

    标题 : 6.Header交换机之模拟验证用户身份 目录 : RabbitMQ 序号 : 6 var channel = connection.CreateModel(); ​ //设置服务质量 ch ...

  4. mysql(五)--性能优化总结

    1 优化思路 作为架构师或者开发人员,说到数据库性能优化,你的思路是什么样的? 或者具体一点,如果在面试的时候遇到这个问题:你会从哪些维度来优化数据库, 你会怎么回答? 我们在第一节课开始的时候讲了, ...

  5. C#通过NI-VISA操作Tektronix TBS 2000B系列示波器

    一.概述 本文描述采用C#语言访问控制Tektronix TBS 2000B 系列示波器.接口协议采用NI-VISA. 最近一个项目需要和一款示波器进行通信,需要对示波器进行一些简单控制并获取到波形数 ...

  6. 字节跳动-前端面试题 Multi Promise Order

    字节跳动-前端面试题 Multi Promise Order Promise Order Async/Await async function async1 () { console.log('asy ...

  7. 如何禁用 Chrome Taps Group feature &#128169;

    如何禁用 Chrome Taps Group feature bug https://support.google.com/chrome/go/feedback_confirmation How to ...

  8. 联合登录 & OAuth 2.0 & OpenID

    联合登录 & OAuth 2.0 & OpenID 第三方联合登录一般可以降低网站的获客成本,所以一般的网站都会做一些联合登录,常用的就是QQ.微信.微博; https://www.z ...

  9. Virtual Reality In Action

    Virtual Reality In Action VR WebXR immersive 沉浸式 https://github.com/immersive-web/webxr https://imme ...

  10. git in depth

    git in depth git delete remote branch # Deleting remote branches in Git $ git push origin --delete f ...