开篇
个人而言,Python是写起来最顺手、看起来最喜欢的编程语言,也是实际工作中使用最多的编程语言。从Python入门,到完全使用Java,再回归Python,编程语言本质上是工具,与Excel、XMind无异。对于测试开发来说,要学习的东西很多,Python是可以作为基础技术深入学习的。第1次通过菜鸟教程学习Python,第2次阅读《流畅的Python》学习Python,第3次跟着官网学习Python。该系列文章参考的是Python3.13版本官方教程,期望学习者具有一定编程基础。
Python解释器
终端使用uv安装Python3.13:
uv python install 3.13
查看安装位置:
which python3.13
进入Python解释器:
$ python3.13
Python 3.13 (default, April 4 2023, 09:25:04)
[GCC 10.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
计算器
平时我们基本不会这样来写Python,但是可以当做计算器来用:
>>> 2 + 2
4
>>> 50 - 5*6
20
>>> (50 - 5*6) / 4
5.0
>>> 8 / 5 # division always returns a floating-point number
1.6
>>> 17 / 3 # classic division returns a float
5.666666666666667
>>>
>>> 17 // 3 # floor division discards the fractional part
5
>>> 17 % 3 # the % operator returns the remainder of the division
2
>>> 5 * 3 + 2 # floored quotient * divisor + remainder
17
>>> 5 ** 2 # 5 squared
25
>>> 2 ** 7 # 2 to the power of 7
128
>>> width = 20
>>> height = 5 * 9
>>> width * height
900
>>> 4 * 3.75 - 1
14.0
>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _ # In interactive mode, the last printed expression is assigned to the variable _
113.0625
>>> round(_, 2)
113.06
Python也很适合用来学习算法,比如最简单的冒泡排序:
def bubble_sort(arr: list) -> list:
n = len(arr)
for i in range(n):
for j in range(n - 1 - i):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
return arr
参考资料:
https://docs.python.org/3.13/tutorial/index.html
https://docs.python.org/3.13/tutorial/appetite.html
https://docs.python.org/3.13/tutorial/interpreter.html
https://docs.python.org/3.13/tutorial/introduction.html