我是靠谱客的博主 怕黑飞机,这篇文章主要介绍UC Berkeley CS 61A (2022 Fall) Lecture 3 Control [Notes]Print and NoneMiscellaneous Python FeaturesConditional Statements,现在分享给大家,希望可以做个参考。
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内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复