【学Python自动化】 3. Python 速览学习笔记 (Windows 环境)
·
一、Python 用作计算器
1 数字运算
基础算术运算
>>> 2 + 2
4
>>> 50 - 5*6
20
>>> (50 - 5*6) / 4
5.0
>>> 8 / 5 # 除法总是返回浮点数
1.6
整数除法和取余
>>> 17 / 3 # 经典除法
5.666666666666667
>>> 17 // 3 # 向下取整除法
5
>>> 17 % 3 # 取余数
2
>>> 5 * 3 + 2 # 验证公式
17
乘方运算
>>> 5 ** 2 # 5的平方
25
>>> 2 ** 7 # 2的7次方
128
变量赋值和使用
>>> width = 20
>>> height = 5 * 9
>>> width * height
900
>>> n # 访问未定义变量会报错
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'n' is not defined
混合运算和特殊变量 _
>>> 4 * 3.75 - 1 # 整数会自动转为浮点数
14.0
>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _ # _ 表示上一个计算结果
113.0625
>>> round(_, 2) # 四舍五入到2位小数
113.06
2 文本(字符串)操作
字符串定义
>>> 'spam eggs' # 单引号
'spam eggs'
>>> "Paris rabbit got your back :)! Yay!" # 双引号
'Paris rabbit got your back :)! Yay!'
>>> '1975' # 数字字符串
'1975'
转义字符和原始字符串
>>> 'doesn\'t' # 转义单引号
"doesn't"
>>> "doesn't" # 或用双引号
"doesn't"
>>> print('C:\some\name') # \n 被解释为换行
C:\some
ame
>>> print(r'C:\some\name') # 原始字符串
C:\some\name
多行字符串
>>> print("""\
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to
""")
字符串拼接和重复
>>> 3 * 'un' + 'ium' # 重复+拼接
'unununium'
>>> 'Py' 'thon' # 相邻字面值自动合并
'Python'
>>> prefix = 'Py'
>>> prefix + 'thon' # 变量拼接要用 +
'Python'
字符串索引和切片
>>> word = 'Python'
>>> word[0] # 第一个字符
'P'
>>> word[5] # 第六个字符
'n'
>>> word[-1] # 最后一个字符
'n'
>>> word[0:2] # 切片:0到2(不含2)
'Py'
>>> word[:2] # 从开始到2
'Py'
>>> word[4:] # 从4到结束
'on'
>>> word[-2:] # 最后两个字符
'on'
字符串不可变性
>>> word[0] = 'J' # 错误:字符串不可修改
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> 'J' + word[1:] # 正确:创建新字符串
'Jython'
字符串长度
>>> s = 'supercalifragilisticexpialidocious'
>>> len(s)
34
3 列表操作
列表创建和访问
>>> squares = [1, 4, 9, 16, 25]
>>> squares
[1, 4, 9, 16, 25]
>>> squares[0] # 索引访问
1
>>> squares[-1] # 最后一个元素
25
>>> squares[-3:] # 切片返回新列表
[9, 16, 25]
列表修改
>>> cubes = [1, 8, 27, 65, 125]
>>> cubes[3] = 64 # 修改元素
>>> cubes
[1, 8, 27, 64, 125]
>>> cubes.append(216) # 添加元素
>>> cubes.append(7 ** 3)
>>> cubes
[1, 8, 27, 64, 125, 216, 343]
列表引用和拷贝
>>> rgb = ["Red", "Green", "Blue"]
>>> rgba = rgb # 引用同一个对象
>>> rgba.append("Alph")
>>> rgb # 两个变量都受影响
["Red", "Green", "Blue", "Alph"]
>>> correct_rgba = rgba[:] # 创建浅拷贝
>>> correct_rgba[-1] = "Alpha"
>>> correct_rgba
["Red", "Green", "Blue", "Alpha"]
>>> rgba # 原列表不变
["Red", "Green", "Blue", "Alph"]
列表切片赋值
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> letters[2:5] = ['C', 'D', 'E'] # 替换切片
>>> letters
['a', 'b', 'C', 'D', 'E', 'f', 'g']
>>> letters[2:5] = [] # 删除切片
>>> letters
['a', 'b', 'f', 'g']
>>> letters[:] = [] # 清空列表
>>> letters
[]
列表长度和嵌套列表
>>> letters = ['a', 'b', 'c', 'd']
>>> len(letters)
4
>>> a = ['a', 'b', 'c']
>>> n = [1, 2, 3]
>>> x = [a, n] # 嵌套列表
>>> x
[['a', 'b', 'c'], [1, 2, 3]]
>>> x[0][1] # 访问嵌套元素
'b'
二、走向编程的第一步
斐波那契数列示例
# 斐波那契数列:前两项之和等于下一项
a, b = 0, 1 # 多重赋值
while a < 10:
print(a)
a, b = b, a + b # 同时更新两个变量
输出:
0
1
1
2
3
5
8
print函数的高级用法
>>> i = 256*256
>>> print('The value of i is', i) # 多个参数
The value of i is 65536
>>> a, b = 0, 1
>>> while a < 1000:
... print(a, end=',') # 取消换行,用逗号结尾
... a, b = b, a+b
...
0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,
重要注意事项
-
运算符优先级:** 比 - 优先级更高,-3**2 结果是 -9
-
引号使用:单引号和双引号功能相同,选择一种保持代码一致性
-
字符串不可变:所有字符串操作都会创建新字符串
-
列表可变:列表可以直接修改,注意引用和拷贝的区别
-
缩进很重要:Python 用缩进来标识代码块,必须保持一致
这些基础操作是Python编程的基石,建议在交互模式下多加练习!
更多推荐



所有评论(0)