南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)03-Control

Control 9/25/19
Control 9 / 25 / 19

Print and None Demo
Print and None Demo

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 >>def does_not_return_square(x): XX ------- No return >>does_not_return_square(4)+----- None value is not displayed The name sixteen >>isixteen =does_not_return_square(4) is now bound to >>sixteen +4 the value None Traceback (most recent call last): File "",line 1,in TypeError:unsupported operand type(s)for +'NoneType'and 'int
>>> does_not_return_square(4) >>> sixteen + 4 Traceback (most recent call last): File "", line 1, in TypeError: unsupported operand type(s) for +: 'NoneType' and 'int' >>> sixteen = does_not_return_square(4) 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 >>> def does_not_return_square(x): ... x * x ... No return None value is not displayed The name sixteen is now bound to the value None

Pure Functions Non-Pure Functions Pure Functions just return values Return value abs 1支 Argument (2.0 pow 1267650600228229401496703205376 2 Arguments Non-Pure Functions Return None! have side effects -2 print -上- iNone A side effect isn't a value; it's anything Python displays the output "-2" that happens as a consequence of calling a function
Pure Functions & Non-Pure Functions Pure Functions just return values Non-Pure Functions have side effects -2 abs 2 Argument Return value 2, 100 pow 1267650600228229401496703205376 2 Arguments print -2 None Python displays the output “-2” Return None! A side effect isn't a value; it's anything that happens as a consequence of calling a function

Nested Expressions with Print >>> print(print(1),print(2)) 1 2 None,Noneprint Does not get displayed None None None display "NoneNone" None print(print(1),print(2)) func print(...) None None print(1) print(2) func print(...) 1 func print(...) 2 1 print None None display“1" display "2
print(print(1), print(2)) Nested Expressions with Print >>> print(print(1), print(2)) 1 2 None None func print(...) print(1) func print(...) 1 None None print(2) func print(...) 2 print 1 None display “1” 2 print None display “2” None, None print None display “None None” None Does not get displayed

print vs return Demo
print vs return Demo

Control
Control

Control Expressions in programs evaluate to values Statements are executed to perform actions o Ex:assignment and def statements With what we have seen so far,a lot of useful programs have been left out ●For example:returning 'hot',‘warm',or‘cold'depending on an argument temp To do this we introduce the concept of control o Special expressions and statements can control how the program is executed by the interpreter
Control ● Expressions in programs evaluate to values ● Statements are executed to perform actions ○ Ex: assignment and def statements ● With what we have seen so far, a lot of useful programs have been left out ● For example: returning ‘hot’, ‘warm’, or ‘cold’ depending on an argument temp ● To do this we introduce the concept of control ○ Special expressions and statements can control how the program is executed by the interpreter

Conditional statements (if statements) Clause Syntax: if : Always start with if clause .Zero or more elif clauses elif : .Zero or one else clause,always else: at the end Execution Rule for Conditional Statements: Each header is considered in order 1.Evaluate the header's conditional expression if the header is not an else 2.If the expression evaluates to true or the header is an else,execute the suite and skip the remaining headers
Conditional statements (if statements) if : elif : else: Clause Syntax: ● Always start with if clause ● Zero or more elif clauses ● Zero or one else clause, always at the end Execution Rule for Conditional Statements: Each header is considered in order 1. Evaluate the header’s conditional expression if the header is not an else 2. If the expression evaluates to true or the header is an else, execute the suite and skip the remaining headers

if examples Demo
if examples Demo
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)02-Names & Functions.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)01-Introduction(主讲:冯新宇).pptx
- 《计算机科学》相关教学资源(PPT课件讲稿)Modular Verification of Concurrent Assembly Code with Dynamic Thread Creation and Termination.ppt
- 《计算机科学》相关教学资源(PPT课件讲稿)Modular Verification of Assembly Code with Stack-Based Control Abstractions.ppt
- 《计算机科学》相关教学资源(PPT课件讲稿)An Open Framework for Foundational Proof-Carrying Code.ppt
- 《计算机科学》相关教学资源(PPT课件讲稿)Certifying Low-Level Programs with Hardware Interrupts and Preemptive Threads.ppt
- 《计算机科学》相关教学资源(PPT课件讲稿)On the Relationship between Concurrent Separation Logic and Assume-Guarantee Reasoning.ppt
- 《计算机科学》相关教学资源(参考文献)Technical Report TTIC-TR-2008-1(Local Rely-Guarantee Reasoning).pdf
- 《计算机科学》相关教学资源(参考文献)Deny-Guarantee Reasoning.pdf
- 《计算机科学》相关教学资源(参考文献)A Rely-Guarantee-Based Simulation for Verifying Concurrent Program Transformations.pdf
- 《计算机科学》相关教学资源(参考文献)Modular Verification of Linearizability with Non-Fixed Linearization Points.pdf
- 《计算机科学》相关教学资源(参考文献)Characterizing Progress Properties of Concurrent Objects via Contextual Refinements.pdf
- 《计算机科学》相关教学资源(参考文献)Rely-Guarantee-Based Simulation for Compositional Verification of Concurrent Program Transformations.pdf
- 《计算机科学》相关教学资源(参考文献)Compositional Verification of Termination-Preserving Refinement of Concurrent Programs.pdf
- 《计算机科学》相关教学资源(参考文献)A Program Logic for Concurrent Objects under Fair Scheduling.pdf
- 《计算机科学》相关教学资源(参考文献)A Practical Verification Framework for Preemptive OS Kernels.pdf
- 《计算机科学》相关教学资源(参考文献)Progress of Concurrent Objects with Partial Methods.pdf
- 《计算机科学》相关教学资源(参考文献)POMP:Protocol Oblivious SDN Programming with Automatic Multi-Table Pipelining.pdf
- 《计算机科学》相关教学资源(参考文献)Decay of Correlation in Spin Systems.pdf
- 《计算机科学》相关教学资源(参考文献)Counting with Bounded Treewidth.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)04-Environment Diagrams.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)05-Higher-Order Functions.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)06-Recursion.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)07-Recursion Examples.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)08-Containers.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)09-Data-Abstractions.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)10-Trees.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)11-Mutable-Values.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)12-Mutable Functions & Growth.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)13-Iterators.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)13-Iterators & Generators.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)14-Object-Oriented Programming.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)15-Inheritance.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)16-Linked Lists & Mutable Trees.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)17-Interfaces.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)18-Scheme.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)19-More-Scheme.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)20-Interpreters.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)21-Macros.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)22-Streams.pptx