概述
Print and None
None Indicates that Nothing is Returned
The special value None
represents nothing in Python
A function that does not explicitly return a value will return None
Careful: None
is not displayed by the interpreter as the value of an expression
Pure Functions & Non-Pure Functions
- Pure Functions:
- Just return values
- Non-Pure Functions:
- Have side effects
Example: nested expressions with print
>>> print(print(1), print(2))
1
2
None None
Miscellaneous Python Features
Division
2022 / 10 # 202.2 (truediv)
2022 // 10 # 202 (floor div)
-2022 // 10 # -203
2022 % 10 # 2 (mod)
-2022 % 10 # 8
Multiple Return Values
def ret(a, b):
return a, b
x, y = ret(10, 20) # x = 10, y = 20
Doctests
from operator import floordiv, mod
def divide_exact(n, d):
""" Return the quotient and remainder of dividing N by D
>>> q, r = divide_exact(2013, 10)
>>> q
201
>>> r
3
"""
return floordiv(n, d), mod(n, d)
python3 -m doctest ex.py
python3 -m doctest -v ex.py
Trying:
q, r = divide_exact(2013, 10)
Expecting nothing
ok
Trying:
q
Expecting:
201
ok
Trying:
r
Expecting:
3
ok
1 items had no tests:
tmp
1 items passed all tests:
3 tests in tmp.divide_exact
3 tests in 2 items.
3 passed and 0 failed.
Test passed.
Conditional Statements
if ...:
...
elif ...:
...
else:
...
-
Always starts with
if
clause. -
Zero or more
elif
clauses. -
Zero or one
else
clause, always at the end.
Boolean Contexts
False
values in Python: False
, 0
, ‘’
, None
True
values in Python: Anything else incluing True
最后
以上就是怕黑飞机为你收集整理的UC Berkeley CS 61A (2022 Fall) Lecture 3 Control [Notes]Print and NoneMiscellaneous Python FeaturesConditional Statements的全部内容,希望文章能够帮你解决UC Berkeley CS 61A (2022 Fall) Lecture 3 Control [Notes]Print and NoneMiscellaneous Python FeaturesConditional Statements所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复