我是靠谱客的博主 怕黑飞机,最近开发中收集的这篇文章主要介绍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:
    ...
  1. Always starts with if clause.

  2. Zero or more elif clauses.

  3. 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所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部