摘要:
# Python:else的4种用法 ━━━━━━━━━━━━━━━━━━━━━━━━━ 简介:与其他编程语言略有不同,python中,else除了常规的和 if 配合使用外,还有3种组合使用的场景。 ## else使用场景: 1、if - else 2、for - else 3、while - e 阅读全文
posted @ 2023-06-27 23:40
nxhujiee
阅读(534)
评论(0)
推荐(0)
摘要:
# 举例说明exec()函数的用法 ━━━━━━━━━━━━━━━━━━━━━━━━━ `exec()` 函数可以用于执行一段字符串作为代码,这在某些场景下非常有用。以下是一些 `exec()` 函数的用法示例: ## 1. 动态执行 Python 代码: ```python code_str = 阅读全文
posted @ 2023-06-27 22:35
nxhujiee
阅读(139)
评论(0)
推荐(0)
摘要:
 https://www.bilibili.com/video/BV1Yh4y1p7Ej/?spm_id 阅读全文
posted @ 2023-06-27 17:54
nxhujiee
阅读(315)
评论(0)
推荐(0)
摘要:
一行异常处理 我们使用异常处理来处理 Python 中的运行时错误。你知道我们可以在一行中编写这个 Try except 语句吗?通过使用 exec() 语句,我们可以做到这一点。 #原始方式 ``` try: print(x) except: print("Error") ``` #单行方式 `` 阅读全文
posted @ 2023-06-27 11:36
nxhujiee
阅读(79)
评论(0)
推荐(0)
摘要:
计算执行特定代码所花费的时间 ━━━━━━━━━━━━━━━━━━━━━━ ``` import time start time time.time() a=1 b=2 c=a+b print(c) #3 end time= time.time() total time= end time- sta 阅读全文
posted @ 2023-06-27 11:31
nxhujiee
阅读(13)
评论(0)
推荐(0)
摘要:
# python:一行代码读写文件 ### 1、读取文件 ``` lst = [line.strip() for line in open('data.txt')] print(lst) ``` 这里我们使用列表来处理。 首先,我们打开一个文本文件,并使用for循环,逐行读取。 最后,使用strip 阅读全文
posted @ 2023-06-27 11:27
nxhujiee
阅读(77)
评论(0)
推荐(0)

浙公网安备 33010602011771号