学习目录阿力阿哩哩:深度学习 | 学习目录zhuanlan.zhihu.com
上一期我们讲到阿力阿哩哩:Python基础zhuanlan.zhihu.com
3.3Python进阶学习
3.3.1 循环
两种循环:for循环与while循环
1. for循环打印List
1. # /chapter3/3_3_Basis_Advance.ipynb
2. str_list = ['Chile', 'b', 'c']
3.
4. print('第1种循环取值方式:直接取值')
5. for sub_str in str_list:
6. print(sub_str)
7.
8. print('--------------------------')
9. print('第2种循环取值方式:索引取值')
10. for i in range(len(str_list)):
11. print(str_list[i])
第1种循环取值方式:直接取值
Chile
b
c
--------------------------
第2种循环取值方式:索引取值
Chile
b
c
2. while循环打印List
1. str_list = ['Chile', 'b', 'c']
2. i = 0
3. while i < len(str_list):
4. print(str_list[i])
5. i += 1
Chile
b
c
3. for循环打印Tuple
1. str_list = ['Chile', 'b', 'c']
2. mix_tuple = ('chile', 111, 2.2, 'a', str_list) # 不可赋值
3. print('第1种循环取值方式:直接取值')
4. for sub_tuple in mix_tuple:
5. print(sub_tuple)
6.
7. print('--------------------------')
8. print('第2种循环取值方式:索引取值')
9. for i in range(len(mix_tuple)):
10. print(mix_tuple[i])
第1种循环取值方式:直接取值
chile
111
2.2
a
['Chile', 'b', 'c']
--------------------------
第2种循环取值方式:索引取值
chile
111
2.2
a
['Chile', 'b', 'c']
4. while循环打印Tuple
1. str_list = ['Chile', 'b', 'c']
2. mix_tuple = ('chile', 111, 2.2, 'a', str_list) # 不可赋值
3. i = 0
4. while i < len(mix_tuple):
5. print(mix_tuple[i])
6. i += 1
chile
111
2.2
a
['Chile', 'b', 'c']
5. for循环打印Dictionary
1. str_list = ['Chile', 'b', 'c']
2. mix_tuple = ('chile', 111, 2.2, 'a', str_list) # 不可赋值
3. num_list = [1, 2, 3, 4]
4. test_dict = {'name': 'Chile', 'age': 18, 'num_list': num_list, 'tuple': mix_tuple}
5. for key in test_dict.keys(): # 键值对打印法
6. print('key:', key)
7. print('value:', test_dict[key])
8. print('-------------')
key: name
value: Chile
-------------
key: age
value: 18
-------------
key: num_list
value: [1, 2, 3, 4]
-------------
key: tuple
value: ('chile', 111, 2.2, 'a', ['Chile', 'b', 'c'])
6. for循环打印Set
1. test_set = {'abc', 1, 1, '1', 'chile'}
2. for value in test_set:
3. print(value, ' ', type(value))
abc
1
1
chile
3.3.2 条件语句
== : 恒等符号
!= : 不等符号
> : 大于号
< : 小于号
>= : 大于等于号
<= : 小于等于号
and: 与
or : 或
not : 非
1. a = 1 # 数字
2. b = '1' # 字符串
3. if a == b:
4. print('a == b')
5. else:
6. print('a != b')
a != b
1. a = 1
2. b = 2
3. if a > b:
4. print('a > b')
5. elif a < b:
6. print('a < b')
7. else:
8. print('a == b')
a < b
1. a = True
2. b = False
3. if a and b:
4. print('True')
5. else:
6. print('False')
7.
8. if a or b:
9. print('True')
10. else:
11. print('False')
12.
13. if a and (not b):
14. print('True')
15. else:
16. print('False')
False
True
True
3.3.3 文件I/O
权限:
w: 写权限
r : 读权限
a : 在原有文本的基础上追加文本的权限
互联网上的文件有非常多的格式,这里只是举个例子让大家有个直观的感受。至于更多格式的读写,大家可以通过互联网去搜索,Python兼容很多文件格式的读写,且代码风格都差不多。
1. with open('text.txt', 'w') as fw: # 只有文件名,默认文件在统计目录
2. string = 'I am chile!'
3. for i in range(5):
4. fw.write(string + '\n')
1. with open('text.txt', 'r') as fr:
2. for line in fr:
3. print(line)
I am chile!
I am chile!
I am chile!
I am chile!
I am chile!
1. with open('text.txt', 'a') as fw:
2. string = 'You are handsome!'
3. for i in range(5):
4. fw.write(string + '\n')
1. with open('text.txt', 'r') as fr:
2. for line in fr:
3. print(line)
I am chile!
I am chile!
I am chile!
I am chile!
I am chile!
You are handsome!
You are handsome!
You are handsome!
You are handsome!
You are handsome!
3.3.4 异常
try:执行正常代码。
except:发生异常,执行此处代码。
else:(这段代码可不加)无异常,则执行此处代码。
1. try:
2. with open('txr.txt', 'r') as fr:
3. text = fr.read()
4. except IOError:
5. print('The file does not exist!')
6.
7. else:
8. print('Succeed')
The file does not exist!
异常处理的目的:大家一开始写Python的时候可能并不需要的异常处理机制,因为我们的代码简洁又高效,不过这并不代表你永远不需要。现代软件是非常庞大的,而代码又是人写的,难免会出错,你不知道一个大型软件在运行过程中会在什么时候出现一个bug,这时候异常处理机制就能让你快速定位自己软件的bug,缩短我们调试的时间,这就是异常处理机制的用途。
3.3.5 导包
导包指令:
import:导入
from ... import ...:从……导入
1. 导入本地包
首先创建test.py,功能是打印hello,接着通过以下代码导入
1. from test import hello
2. hello()
hello!
1. import test
2. test.hello()
hello!
2. 导入系统包
1. import time # 引入时间模块
2. # 格式化成year-month-day hour:min:sec形式
3. print (time.strftime("%Y-%m-%d%H:%M:%S", time.localtime()))
2019-08-21 09:43:07
导包的目的:Python之所以被称为胶水语言,是因为它有很多优秀的第三方包,让我们在编程过程中只关注任务的解决,而不拘泥于代码的繁琐,提升代码的复用率,加快编程速度。因此,导包是Python不可或缺的重要技能。
下一期,我们将介绍
Python高阶学习
敬请期待~
关注我的微信公众号【阿力阿哩哩的炼丹日常】~不定期更新相关专业知识~
喜欢就点个赞吧~