Learn Python the Hard Way,ex37-1
本练习为复习python的符号和关键字
关键字有:

#and or False True
print(1==0 and 2==0, 1==0 or 2==0)
print(False)
print(True)
输出:
False False
False
True
lists = ['1', '2', 'a', 'afds', 3, 463]
"""
del:Deletion of a target list recursively deletes each target, from left to right.
for……in print
if……elif……else
"""
# del lists[] values/name
for i in range(0,len(lists)):
print(lists, end='\t')
del lists[0]
print(lists)
del lists
if 'lists' not in locals().keys():
print("lists is not variable")
elif 'lists' in locals().keys():
print("lists is variable")
else:
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
"""
pass: "pass" is a null operation
def class
"""
def temp_function(): # a function that does nothing (yet)
pass
class temp_class: # a class with no methods (yet)
pass
"""
with EXPRESSION as TARGET:
SUITE
is semantically equivalent to: manager = (EXPRESSION)
enter = type(manager).__enter__
exit = type(manager).__exit__
value = enter(manager)
hit_except = False try:
TARGET = value
SUITE
except:
hit_except = True
if not exit(manager, *sys.exc_info()):
raise
finally:
if not hit_except:
exit(manager, None, None, None)
"""
with open("temp.txt") as fp:
lines = fp.readlines()
i = 0
while i < len(lines):
if i == 3:
i += 1
continue
print("%d:" % i, lines[i])
i += 1
#is semantically equivalent to:
try:
fp = open("temp.txt")
lines = fp.readlines()
i = 0
while i < len(lines):
if i == 3:
i += 1
continue
print("%d:" % i, lines[i])
i += 1
except:
print("ERROR!!")
else:
print("There nothing to print!")
finally:
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!
tuples = ('1', '2', 'a', 'afds', 3, 463)
sets = {'1', '2', 'a', 'afds', 3, 463}
"""
is
"""
if tuples is not sets:
print("tuple is noe sets!")
输出结果:
tuple is noe sets!
"""
yield: *generator* function
yield_stmt ::= yield_expression
"""
def foo(n):
a, b = 0, 1
while 0 < n:
print(b)
a, b = b, a+b
n -=1
def foo1(n):
a, b = 0, 1
L = []
while 0 < n:
L.append(b)
print(L)
a, b = b, a+b
n -=1
return L
def foo2(n):
a, b = 0, 1
while 0 < n:
yield b
a, b = b, a+b
n -=1 foo(5)
foo1(5)
for i in foo2(5):
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
"""
lambda
lambda_expr ::= "lambda" [parameter_list] ":" expression
def <lambda>(parameters):
return expression
"""
fp = lambda para : open(para)
print(fp("temp.txt").readlines())
"""
async for TARGET in ITER:
SUITE
else:
SUITE2
iter = (ITER)
iter = type(iter).__aiter__(iter)
running = True while running:
try:
TARGET = await type(iter).__anext__(iter)
except StopAsyncIteration:
running = False
else:
SUITE
else:
SUITE2
"""
import asyncio
import time
async def test2(i):
r = await other_test(i) #uspend the execution of *coroutine* on an *awaitable* object.
print("*****",i,r) async def other_test(i):
r = i
print(i,end="*\t")
await asyncio.sleep(0)
print(time.time()-start)
return r url = ["https://segmentfault.com/p/1210000013564725",
"https://www.jianshu.com/p/83badc8028bd",
"https://www.baidu.com/"] loop = asyncio.get_event_loop() # Get the event loop for the current context.
task = [asyncio.ensure_future(test2(i)) for i in url] #Wrap a coroutine or an awaitable in a future.
start = time.time()
loop.run_until_complete(asyncio.wait(task)) #Wait for the Futures and coroutines given by fs to complete.
endtime = time.time()-start
print(endtime)
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
"""
local global nonlocal
"""
sets = {'1', '2', 'a', 'afds', 3, 463}
def temp_global():
global sets
print("temp_global", sets)
sets.add('temp') #+
print(1, sets)
sets.update('temp') #+ 重复的就不添加了,可以添加多个
print(2, sets)
sets.remove('1') #- 没有会提示错误
print(3, sets)
sets.discard('temp') #- 没有不提示错误
sets.pop() #- 随机删除
print(4, sets)
def temp_local():
print("temp_local", sets)
sets.add('1') #+
print(1, sets)
sets.update('temp') #+ 重复的就不添加了,可以添加多个
print(2, sets)
sets.remove('1') #- 没有会提示错误
print(3, sets)
sets.discard('temp') #- 没有不提示错误
sets.pop() #- 随机删除
print(4, sets)
def temp_nonlocal():
name = ['1', '2']
def temp_nonlocal_fuc():
nonlocal name
name.append('3')
return name
return temp_nonlocal_fuc temp_global()
print("*", sets)
temp_local()
print("**", sets)
tn = temp_nonlocal()
print(tn()) #不可单独调用
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的更多相关文章
- Learn Python the Hard Way,ex37-2
本练习为复习python的符号和关键字 数据类型有:True False None Strings numbers floats lists dict tuple set ""&q ...
- [IT学习]Learn Python the Hard Way (Using Python 3)笨办法学Python3版本
黑客余弦先生在知道创宇的知道创宇研发技能表v3.1中提到了入门Python的一本好书<Learn Python the Hard Way(英文版链接)>.其中的代码全部是2.7版本. 如果 ...
- 笨办法学 Python (Learn Python The Hard Way)
最近在看:笨办法学 Python (Learn Python The Hard Way) Contents: 译者前言 前言:笨办法更简单 习题 0: 准备工作 习题 1: 第一个程序 习题 2: 注 ...
- 《Learn python the hard way》Exercise 48: Advanced User Input
这几天有点时间,想学点Python基础,今天看到了<learn python the hard way>的 Ex48,这篇文章主要记录一些工具的安装,以及scan 函数的实现. 首先与Ex ...
- 学 Python (Learn Python The Hard Way)
学 Python (Learn Python The Hard Way) Contents: 译者前言 前言:笨办法更简单 习题 0: 准备工作 习题 1: 第一个程序 习题 2: 注释和井号 习题 ...
- 算是休息了这么长时间吧!准备学习下python文本处理了,哪位大大有好书推荐的说下!
算是休息了这么长时间吧!准备学习下python文本处理了,哪位大大有好书推荐的说下!
- python安装完毕后,提示找不到ssl模块的解决步骤
转载自 醇酒醉影 python安装完毕后,提示找不到ssl模块: [root@localhost ~]# python2.7.5 Python 2.7.5 (default, Jun 3 2013, ...
- linux下,Python 多版本共存,及Pip,Easy_install 安装扩展包
Python2与Python3共存 安装Python3后,建立ln,使用Python(Python2),Python3 来区分两个版本 使用sudo apt-get install python3-s ...
- python学习03——设计,与input有关
笨办法学python第36节,我写的代码如下: from sys import exit def rule(): print "Congratulations! You made the r ...
随机推荐
- Python 实现多线程的几种方式
threading.Thread 模块 继承实现: import threading import time class TestThread(threading.Thread): def __ini ...
- 设计模式(十八)——观察者模式(JDK Observable源码分析)
1 天气预报项目需求,具体要求如下: 1) 气象站可以将每天测量到的温度,湿度,气压等等以公告的形式发布出去(比如发布到自己的网站或第三方). 2) 需要设计开放型 API,便于其他第三方也能接入气象 ...
- MiniSMB 网络性能测试 免费版本安装指南
1) 烧录Image至USB 在Linux环境下可以运行以下命令(假设usb设备符号为/dev/sdb): root# tar -Jxf minismb-free-edition.img.tar.xz ...
- spark 一、编程指南
总览 第一.每个spark 应用都有一个驱动程序去运行着主函数和再每个节点上的并行操作. spark提供了一个RDD(弹性分布式数据集)的数据集合,可以通过不同的节点并行操作运算,可以通过hdfs文件 ...
- 多线程之ThreadLocal类
深入研究java.lang.ThreadLocal类 0.前言 ThreadLocal(线程变量副本)Synchronized实现内存共享,ThreadLocal为每个线程维护一个本地变量.采用空间换 ...
- 爬虫入门二 beautifulsoup
title: 爬虫入门二 beautifulsoup date: 2020-03-12 14:43:00 categories: python tags: crawler 使用beautifulsou ...
- Leetcode(22)-括号生成
给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结果为: [ "((()))", "(()())& ...
- haut-1280 诡异的迷宫
1280: 诡异的迷宫 时间限制: 2 秒 内存限制: 128 MB提交: 174 解决: 27提交 状态 题目描述 Simple最近刷题(打游戏)刷多了,一觉醒来发现自己到了一个迷宫里,怎么也出 ...
- C++ part8
1.volatile关键字 在C++中,对volatile修饰的对象的访问,有编译器优化上的副作用: 不允许被编译器优化,提供特殊地址的稳定访问(只从内存中读取). 有序性,编译器进行优化时,不能把对 ...
- u-boot 移植 --->5、友善之臂Tiny210底板王网卡驱动移植
网卡芯片的工作原理 DM9000AE具有以下主要性能: ①48管脚的LQFP封装,管脚少体积小: ②支持8/16位数据总线: ③适用于10Base-T和100Base-T,10/100M自适应,适应不 ...