第四章 Python外壳:代码结构
Python的独特语法:
- 不使用分号结束语句,而是回车;
- 通过代码缩进来区分代码块;
- if、while、for等,都不用括号,但不能没有冒号(:)。
如何将一行命令分为多行?
>>> myNameIs = "Li\
Zhi\
Xin.\
"
>>> myNameIs
'LiZhiXin.'
4.1 条件语句和循环语句
如何使用if、elif和else进行比较?
>>> a = 1
>>> if a < 2:
print ("yes")
elif a > 2:
print ("no")
else:
print ("chaos") yes
如何连接条件表达式?
python中没有!、&& 和 || 运算符,分别用 not、and 和 or代替。
>>> not 1
False
>>> 5 < 6 and 1 < 2
True
>>> 5 < 6 or 1 > 2
True
python中有哪些假值?
>>> False
False
>>> None
>>> 0
0
>>> 0.0
0.0
>>> ''
''
>>> []
[]
>>> ()
()
>>> {}
{}
>>> set()
set()
如何使用while进行循环?
>>> count = 1
>>> while count <= 5:
print(count)
count += 1 1
2
3
4
5
如何跳出循环?
>>> while True:
stuff = input("string to calpitalize [type q to quit]:")
if stuff == 'q':
break
print(stuff.capitalize()) string to calpitalize [type q to quit]:li zhi xin
Li zhi xin
string to calpitalize [type q to quit]:q
>>>
如何跳到循环开始(用于跳过特定条件的循环)?
>>> while True:
value = input("Interger, please [q to quit]: ")
if value == 'q':
break
number = int(value)
if number % 2 == 0:
continue
print(number, "squared is ", number*number) Interger, please [q to quit]: 1
1 squared is 1
Interger, please [q to quit]: 2
Interger, please [q to quit]: 3
3 squared is 9
Interger, please [q to quit]: 4
Interger, please [q to quit]: q
循环外的else是如何使用的?
>>> numbers = [1,3,5]
>>> position = 0
>>> while position < len(numbers):
number = numbers[position]
if number % 2 == 0:
print('Found even number', number)
break
position += 1
else:
print("No even number found.") No even number found.
*如何使用for进行迭代(很独特)?
python中的for很牛逼,可以在数据结构和具体实现未知的情况下,遍历整个数据结构:
>>> rabbits = ['a', 'b', 'c', 'd']
>>> current = 0
>>> while current < len(rabbits):
print(rabbits[current])
current += 1 a
b
c
d
>>> for rabbit in rabbits:
print(rabbit) a
b
c
d
>>> word = 'cat'
>>> for letter in word:
print(letter) c
a
t
>>> a = {'a':'b', 'c':'d', 'e':'f'}
>>> for key in a:
print(key)
a
c
e
>>> for value in a.values():
print(value) b
d
f
>>> for item in a.items():
print(item) ('a', 'b')
('c', 'd')
('e', 'f')
>>> for key, value in a.items():
print(key, value) a b
c d
e f
如何用zip()函数进行迭代?
对多个序列使用并行迭代,就是一起迭代,等价于将多个序列有序合并了。
>>> c = 0.1, 0.2, 0.3
>>> a = (1, 2, 3)
>>> b = ['a', 'b', 'c', 'd']
>>> c = 0.1, 0.2, 0.3, 0.4, 0.5,
>>> for e, f, g in zip(a, b, c):
print(e, ' ', f, ' ', g) 1 a 0.1
2 b 0.2
3 c 0.3
list()、zip()和 dict()可以灵活运用。
如何生成特定区间的自然数序列?
range()的使用方法类似切片,但是却是使用‘,’分隔,而不是‘:’。
>>> for x in range(0,3):
print(x) 0
1
2
>>> list(range(0,3))
[0, 1, 2]
4.2 推导式(python特有)
从一个或多个迭代器创建数据结构,可以将循环和条件结合。
列表推导式
>>> number_list = [number for number in range(1,6)]
>>> number_list
[1, 2, 3, 4, 5]
>>> a_list = [number for number in range(1,6) if number % 2 ==1]
>>> a_list
[1, 3, 5]
>>> rows = range(1,4)
>>> cols = range(1,3)
>>> cells = [(row, col) for row in rows for col in cols]
>>> for cell in cells:
print(cell) (1, 1)
(1, 2)
(2, 1)
(2, 2)
(3, 1)
(3, 2)
字典推导式
>>> word = 'letters'
>>> letter_counts = {letter:word.count(letter) for letter in word}
>>> letter_counts
{'l': 1, 't': 2, 'r': 1, 's': 1, 'e': 2}
函数
python中参数有哪几种使用方法?
>>> def func(a, b, c):
print('a is ', a, 'b is ', b, 'c is ', c) >>> func(1, 2, 3)
a is 1 b is 2 c is 3
>>> func(c = 1, b = 2, a = 3)
a is 3 b is 2 c is 1
>>> d = [1, 2, 3]
>>> func(*d)
a is 1 b is 2 c is 3
>>> e = {'c':1, 'b':2, 'a':3}
>>> func(**e)
a is 3 b is 2 c is 1
生成器
装饰器
命名空间和作用域
异常
第四章 Python外壳:代码结构的更多相关文章
- [Python学习笔记][第四章Python字符串]
2016/1/28学习内容 第四章 Python字符串与正则表达式之字符串 编码规则 UTF-8 以1个字节表示英语字符(兼容ASCII),以3个字节表示中文及其他语言,UTF-8对全世界所有国家需要 ...
- [Python笔记][第四章Python正则表达式]
2016/1/28学习内容 第四章 Python字符串与正则表达式之正则表达式 正则表达式是字符串处理的有力工具和技术,正则表达式使用预定义的特定模式去匹配一类具有共同特征的字符串,主要用于字符串处理 ...
- Unity 游戏框架搭建 2019 (三十九、四十一) 第四章 简介&方法的结构重复问题&泛型:结构复用利器
第四章 简介 方法的结构重复问题 我们在上一篇正式整理完毕,从这一篇开始,我们要再次进入学习收集示例阶段了. 那么我们学什么呢?当然是学习设计工具,也就是在上篇中提到的关键知识点.这些关键知识点,大部 ...
- 0003.5-20180422-自动化第四章-python基础学习笔记--脚本
0003.5-20180422-自动化第四章-python基础学习笔记--脚本 1-shopping """ v = [ {"name": " ...
- “全栈2019”Java异常第四章:catch代码块作用域详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...
- 《Python CookBook2》 第四章 Python技巧 对象拷贝 && 通过列表推导构建列表
(先学第四章) 对象拷贝 任务: Python通常只是使用指向原对象的引用,并不是真正的拷贝. 解决方案: >>> a = [1,2,3] >>> import c ...
- 第四章:重构代码[学习Android Studio汉化教程]
第四章 Refactoring Code The solutions you develop in Android Studio will not always follow a straight p ...
- Python项目代码结构
目录结构组织方式 简要解释一下: bin/: 存放项目的一些可执行文件,当然你可以起名script/之类的也行. luffy/: 存放项目的所有源代码.(1) 源代码中的所有模块.包都应该放在此目录. ...
- 《Python核心编程》 第四章 Python对象- 课后习题
练习 4-1. Python对象.与所有Python对象有关的三个属性是什么?请简单的描述一下. 答:身份.类型和值: 身份:每一个对象都有一个唯一的身份标识自己,可以用id()得到. 类型:对象的 ...
随机推荐
- java配置自动任务,定期执行代码
任务调用类: package business.tools.service; import java.util.ArrayList; import java.util.Calendar; import ...
- uva 10090 Marbles
Problem F Marbles Input: standard input Output: standard output I have some (say, n) marbles (small ...
- fullPage.js
https://github.com/alvarotrigo/fullPage.js 下载地址 demo:http://pan.baidu.com/s/1o8QWCmm 演示:http://full ...
- infragistcs 又
1:UltraGrid风格设置函数 public static void ColorGrid(ref Infragistics.Win.UltraWinGrid.UltraGrid dgd) { // ...
- SqlSever基础 group by之后,加having 对分组之后的数据在进行处理
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...
- 四HttpServletResponse常见应用——生成验证码
转载自http://www.cnblogs.com/xdp-gacl/p/3791993.html 一.HttpServletResponse常见应用——生成验证码 1.1.生成随机图片用作验证码 生 ...
- [CF738D]Sea Battle(贪心)
题目链接:http://codeforces.com/contest/738/problem/D 题意:1*n的格子里有a条长为b的船.有一个人射了k发子弹都没打中船,现在问最少再打多少次一定能保证射 ...
- 用@RequestMapping映射请求
DispatcherServlet接受一个web请求之后,将请求发送给@Controller注解声明的不同控制器类. 这个调度过程依赖控制器类及其处理程序方法中声明的各种@RequestMapping ...
- CUBRID学习笔记 7 ms常见错误
基本不是权限问题,就是dll问题. 重新下载或应用dll注意版本. 权限的问题,先本机测试. 看看在web管理有无问题. 剩下的基本就简单了 欢迎转载 ,转载时请保留作者信息.本文版权归本人所有, ...
- 历史命令history
历史命令在用户注销之后会保存在用户家目录下的-/.bash_history中 history #查看系统中实时缓存的历史命令,与.bash_history中的内容并不完全相同 history -c # ...