0% found this document useful (0 votes)
26 views2 pages

Midterm 2 Cheat Sheet

The document discusses various Python programming concepts including functions, loops, scope, namespaces, and docstrings. It provides examples of using functions, loops, and ranges. It also explains reasons for defining functions and discusses incremental development and function stubs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views2 pages

Midterm 2 Cheat Sheet

The document discusses various Python programming concepts including functions, loops, scope, namespaces, and docstrings. It provides examples of using functions, loops, and ranges. It also explains reasons for defining functions and discusses incremental development and function stubs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

reversed() function to reverse the order of the elements in a loop (Ex for name in reversed(names):)

range(0,5,1) 0,1,2,3,4 // range(0,5,2) 0,2,4 // range(5,0,-2)  5,3,1

A break statement in a loop causes the loop to exit immediately.

A continue statement in a loop causes an immediate jump to the while or for loop header statement.

A loop may optionally include an else clause that executes only if the loop terminates normally

enumerate() function retrieves both the index and corresponding element

A function is a named series of statements.  ex def calc_area(parameter) and


calc_area(2.5(argument))

A function with no return statement, or a return statement with no following expression, returns the
value None. None is a special keyword that indicates no value.

A function with no return statement is called a void function, and such a function returns the
value None.

The function's behavior of being able to add together different types is a concept called polymorphism.

Python uses dynamic typing to determine the type of objects as a program executes. In contrast to
dynamic typing, many other languages like C, C++, and Java use static typing, which requires the
programmer to define the type of every variable and every function parameter in a program's source
code.

Reasons for defining functions

 Modular development is the process of dividing a program into separate modules that can be
developed and tested separately and then integrated into a single program.
 Improving program readability
 Avoid writing redundant code

Programs are typically written using incremental development, meaning a small amount of code is
written and tested, then a small amount more (an incremental amount) is written and tested, and so on.

To assist with the incremental development process, programmers commonly introduce function stubs,
which are function definitions whose statements haven't been written yet.

 use the pass keyword


 return -1
 raise NotImplementedError causes the program to halt

part of the value of a function object is compiled bytecode that represents the statements to be
executed by the function.

A variable or function object is only visible to part of a program, known as the object's scope

A namespace maps names to objects. The Python interpreter uses namespaces to track all of the objects
in a program. Maps the visible names in a scope to objects.
If the name cannot be found in any namespace, the interpreter generates a NameError. The process of
searching for a name in the available namespaces is called scope resolution

three nested scopes: built-in-scope, global scope, local scope

A docstring is a string literal placed in the first line of a function body. A docstring starts and ends with
three consecutive quotation marks.

The help() function can aid a programmer by providing them with all the documentation associated with
an object.

You might also like