官网学习之控制流

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 if if语句是最常见的控制流: >>> x = int(input("Please enter an integer: ")) Please enter an integer: 42 >>> if x < 0: ... x = 0 ... print('Negative changed to zero') ... elif x == 0: ... print('Zero') ... elif x == 1: ... print('Single') ... else: ... print('More') ... More for for语句用来遍历: ## Create a sample collection users = {'Hans': 'active', 'Éléonore': 'inactive', '景太郎': 'active'} ## Strategy: Iterate over a copy for user, status in users.copy().items(): if status == 'inactive': del users[user] ## Strategy: Create a new collection active_users = {} for user, status in users.items(): if status == 'active': active_users[user] = status range range函数生成数字序列: ...

跟着官网学习Python系列

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 开篇 个人而言,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也很适合用来学习算法,比如最简单的冒泡排序: ...