美文网首页大数据 爬虫Python AI SqlPython小哥哥
你怎能错过这几个鲜为人知但非常实用的 Python 技巧?

你怎能错过这几个鲜为人知但非常实用的 Python 技巧?

作者: 轻松学Python111 | 来源:发表于2019-10-09 15:04 被阅读0次

这篇文章主要和大家分享一些 Python 不一样的技巧,虽然鲜为人知,但非常实用喔!下面就跟随我一起来感受 Python 带给你的乐趣吧。

1.print 打印带有颜色的信息

大家知道 Python 中的信息打印函数 Print,一般我们会使用它打印一些东西,作为一个简单调试。

但是你知道么,这个 Print 打印出来的字体颜色是可以设置的。

一个小例子

<pre class="public-DraftStyleDefault-pre" data-offset-key="fnrud-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(26, 26, 26); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">

<pre class="Editable-styled" data-block="true" data-editor="7ml8d" data-offset-key="fnrud-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">

def esc(code=0):
return f'\033[{code}m'

print(esc('31;1;0') + 'Error:'+esc()+'important')

</pre>

</pre>

在控制台或者 Pycharm 运行这段代码之后你会得到结果。

<pre class="public-DraftStyleDefault-pre" data-offset-key="14ao6-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(26, 26, 26); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">

<pre class="Editable-styled" data-block="true" data-editor="7ml8d" data-offset-key="14ao6-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">

Error:important

</pre>

</pre>

其中 Error 是红色加下划线的,important 为默认色

其设置格式为:033[显示方式;前景色;背景色 m

下面可以设置的参数:

<pre class="public-DraftStyleDefault-pre" data-offset-key="du3u-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(26, 26, 26); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">

<pre class="Editable-styled" data-block="true" data-editor="7ml8d" data-offset-key="du3u-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">

说明:
前景色 背景色 颜色


30 40 黑色
31 41 红色
32 42 绿色
33 43 黃色
34 44 蓝色
35 45 紫红色
36 46 青蓝色
37 47 白色

显示方式 意义

0 终端默认设置
1 高亮显示
4 使用下划线
5 闪烁
7 反白显示
8 不可见

例子:
\033[1;31;40m

</pre>

</pre>

image

2.在 Python 中使用定时器

今天看到一个比较人性化的定时模块 schedule,目前 star 数为 6432,还是非常的受欢迎,这个模块也是秉承这 For Humans 的原则,这里推荐给大家。地址: dbader/schedule

1.通过 pip 即可安装。

<pre class="public-DraftStyleDefault-pre" data-offset-key="7q5k1-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(26, 26, 26); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">

<pre class="Editable-styled" data-block="true" data-editor="7ml8d" data-offset-key="7q5k1-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">

pip install schedule

</pre>

</pre>

2.使用案例

<pre class="public-DraftStyleDefault-pre" data-offset-key="38rka-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(26, 26, 26); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">

<pre class="Editable-styled" data-block="true" data-editor="7ml8d" data-offset-key="38rka-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">

import schedule
import time

def job():
print("I'm working...")

schedule.every(10).minutes.do(job)
schedule.every().hour.do(job)
schedule.every().day.at("10:30").do(job)
schedule.every().monday.do(job)
schedule.every().wednesday.at("13:15").do(job)
schedule.every().minute.at(":17").do(job)

while True:
schedule.run_pending()
time.sleep(1)

</pre>

</pre>

从单词的字面意思,你就知道这是做什么的。

举个例子:

schedule.every().monday.do(job)

这句代码作用就是就是单词意思,定时器会每个周一运行函数 job,怎么样是不是很简单。

3.实现一个进度条

<pre class="public-DraftStyleDefault-pre" data-offset-key="ab96a-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(26, 26, 26); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">

<pre class="Editable-styled" data-block="true" data-editor="7ml8d" data-offset-key="ab96a-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">

from time import sleep

def progress(percent=0, width=30):
left = width * percent // 100
right = width - left
print('\r[', '#' * left, ' ' * right, ']',
f' {percent:.0f}%',
sep='', end='', flush=True)

for i in range(101):
progress(i)
sleep(0.1)

</pre>

</pre>

展示效果

image

别卧槽了,赶紧快试试吧。

image

上面的代码中的 print 有几个有用的参数,sep 的作用是已什么为分隔符,默认是空格,这里设置为空串是为了让每个字符之间更紧凑,end 参数作用是已什么结尾,默认是回车换行符,这里为了实现进度条的效果,同样设置为空串。还有最后一个参数 flush,该参数的作用主要是刷新, 默认 flush = False,不刷新,print 到 f 中的内容先存到内存中;而当 flush = True 时它会立即把内容刷新并输出。

4.优雅的打印嵌套类型的数据

大家应该都有印象,在打印 json 字符串或者字典的时候,打印出的一坨东西根本就没有一个层次关系,这里主要说的就是输出格式的问题。

<pre class="public-DraftStyleDefault-pre" data-offset-key="25sq1-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(26, 26, 26); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">

<pre class="Editable-styled" data-block="true" data-editor="7ml8d" data-offset-key="25sq1-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">

import json
my_mapping = {'a': 23, 'b': 42, 'c': 0xc0ffee}
print(json.dumps(my_mapping, indent=4, sort_keys=True))

</pre>

</pre>

大家可以自己试试只用 print 打印 my_mapping,和例子的这种打印方法。

如果我们打印字典组成的列表呢,这个时候使用 json 的 dumps 方法肯定不行的,不过没关系

用标准库的 pprint 方法同样可以实现上面的方法

<pre class="public-DraftStyleDefault-pre" data-offset-key="211vf-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(26, 26, 26); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">

<pre class="Editable-styled" data-block="true" data-editor="7ml8d" data-offset-key="211vf-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">

import pprint
my_mapping = [{'a': 23, 'b': 42, 'c': 0xc0ffee},{'a': 231, 'b': 42, 'c': 0xc0ffee}]
pprint.pprint(my_mapping,width=4)

</pre>

</pre>

image

5.功能简单的类使用 namedtuple 和 dataclass 的方式定义

有时候我们想实现一个类似类的功能,但是没有那么复杂的方法需要操作的时候,这个时候就可以考虑下下面两种方法了。

第一个,namedtuple 又称具名元组,带有名字的元组。它作为 Python 标准库 collections 里的一个模块,可以实现一个类似类的一个功能。

<pre class="public-DraftStyleDefault-pre" data-offset-key="7cdn3-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(26, 26, 26); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">

<pre class="Editable-styled" data-block="true" data-editor="7ml8d" data-offset-key="7cdn3-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">

from collections import namedtuple

以前简单的类可以使用 namedtuple 实现。

Car = namedtuple('Car', 'color mileage')

my_car = Car('red', 3812.4)
print(my_car.color)
print(my_car)

</pre>

</pre>

但是呢,所有属性需要提前定义好才能使用,比如想使用 my_car.name ,你就得把代码改成下面的样子。

<pre class="public-DraftStyleDefault-pre" data-offset-key="dpkrp-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(26, 26, 26); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">

<pre class="Editable-styled" data-block="true" data-editor="7ml8d" data-offset-key="dpkrp-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">

from collections import namedtuple

以前简单的类可以使用 namedtuple 实现。

Car = namedtuple('Car', 'color mileage name')

my_car = Car('red', 3812.4,"Auto")
print(my_car.color)
print(my_car.name)

</pre>

</pre>

使用 namedtuple 的缺点很明显了。

所以现在更优的方案,那就是 Python3.7 加入到标准库的 dataclass。

其实在 3.6 也可以使用不过需要它被作为第三方的库使用了,使用 pip 安装即可。

使用示例如下:

<pre class="public-DraftStyleDefault-pre" data-offset-key="crkp5-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(26, 26, 26); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">

<pre class="Editable-styled" data-block="true" data-editor="7ml8d" data-offset-key="crkp5-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">

from dataclasses import dataclass
@dataclass
class Car:
color: str
mileage: float

my_car = Car('red', 3812.4)
print(my_car.color)
print(my_car)

</pre>

</pre>

image

6.f-string 的 !r,!a,!s

f-string出现在Python3.6,作为当前最佳的拼接字符串的形式,看下 f-string 的结构

<pre class="public-DraftStyleDefault-pre" data-offset-key="5qvj2-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(26, 26, 26); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">

<pre class="Editable-styled" data-block="true" data-editor="7ml8d" data-offset-key="5qvj2-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">

f ' <text> { <expression> <optional !s, !r, or !a> <optional : format specifier> } <text> ... '

</pre>

</pre>

其中'!s' 在表达式上调用str(),'!r' 调用表达式上的repr(),'!a' 调用表达式上的ascii()

(1.默认情况下,f-string将使用str(),但如果包含转换标志,则可以确保它们使用repr () !

<pre class="public-DraftStyleDefault-pre" data-offset-key="6gg8n-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(26, 26, 26); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">

<pre class="Editable-styled" data-block="true" data-editor="7ml8d" data-offset-key="6gg8n-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">

class Comedian:
def init(self, first_name, last_name, age):
self.first_name = first_name
self.last_name = last_name
self.age = age

def __str__(self):
    return f"{self.first_name} {self.last_name} is {self.age}."

def __repr__(self):
    return f"{self.first_name} {self.last_name} is {self.age}. Surprise!"

</pre>

</pre>

调用

<pre class="public-DraftStyleDefault-pre" data-offset-key="4j4mm-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(26, 26, 26); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">

<pre class="Editable-styled" data-block="true" data-editor="7ml8d" data-offset-key="4j4mm-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">

new_comedian = Comedian("Eric", "Idle", "74")
f"{new_comedian}"
'Eric Idle is 74.'

f"{new_comedian}"
'Eric Idle is 74.'
f"{new_comedian!r}"
'Eric Idle is 74. Surprise!'

</pre>

</pre>

(2.!a的例子

<pre class="public-DraftStyleDefault-pre" data-offset-key="2d89i-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(26, 26, 26); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">

<pre class="Editable-styled" data-block="true" data-editor="7ml8d" data-offset-key="2d89i-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">

a = 'some string'
f'{a!r}'
"'some string'"

</pre>

</pre>

等价于

<pre class="public-DraftStyleDefault-pre" data-offset-key="2knk2-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(26, 26, 26); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">

<pre class="Editable-styled" data-block="true" data-editor="7ml8d" data-offset-key="2knk2-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">

f'{repr(a)}'
"'some string'"

</pre>

</pre>

(3.!d的例子

类似2

pycon2019有人提出的一个展望!d的功能实现:

image

在python3.8中已经实现上述功能,不过不再使用!d了改为了f"{a=}"的形式,看过这个视频的发现没有!d应该很懵逼

7.f-string 里"="的应用

在 Python3.8 里有这样一个功能

<pre class="public-DraftStyleDefault-pre" data-offset-key="3nl9s-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(26, 26, 26); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">

<pre class="Editable-styled" data-block="true" data-editor="7ml8d" data-offset-key="3nl9s-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">

a = 5
print(f"{a=}")

</pre>

</pre>

打印之后的结果为

<pre class="public-DraftStyleDefault-pre" data-offset-key="5e7q-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(26, 26, 26); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">

<pre class="Editable-styled" data-block="true" data-editor="7ml8d" data-offset-key="5e7q-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">

a=5

</pre>

</pre>

是不是很方便,不用你再使用 f"a={a}" 了。

8.海象运算符:=的是使用

<pre class="public-DraftStyleDefault-pre" data-offset-key="9u87g-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(26, 26, 26); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">

<pre class="Editable-styled" data-block="true" data-editor="7ml8d" data-offset-key="9u87g-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">

a =6
if b:=a+1>6:
print(b)

</pre>

</pre>

赋值的时候同时可以进行运算,和 Go 语言的赋值类似了。

代码的运行顺序,首先计算 a+1 得到值为 7,然后把 7 赋值给 b,到这里代码相当于下面这样了

<pre class="public-DraftStyleDefault-pre" data-offset-key="dbpgf-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(26, 26, 26); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">

<pre class="Editable-styled" data-block="true" data-editor="7ml8d" data-offset-key="dbpgf-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">

b =7
if b>6:
print(b)

</pre>

</pre>

怎么样是不是简单了不少,不过这个功能 3.8 开始才能用哦。

相关文章

网友评论

    本文标题:你怎能错过这几个鲜为人知但非常实用的 Python 技巧?

    本文链接:https://www.haomeiwen.com/subject/kptaectx.html