Midterm 2 Review
Midterm 2 Review
while the loop's expression is true; when false, execution proceeds past the loop. Each time through a
loop's statements is called an iteration.
A while loop is a construct that repeatedly executes an indented block of code (known as the loop body)
as long as the loop's expression is True.
Each execution of the loop body is called an iteration, and looping is also called iterating.
A for loop statement loops over each element in a container one at a time, assigning a variable with the
next element that can then be used in the loop body. The container in the for loop statement is typically
a list, tuple, or string. Each iteration of the loop assigns the name given in the for loop statement with
the next element in the container.
A for loop may also iterate backwards over a sequence, starting at the last element and ending with the
first element, by using the reversed() function to reverse the order of the elements.
Ex for name in reversed(names):
Generated
Range Explanation
sequence
range() generates a sequence of integers between a starting integer that is included in the range, an
ending integer that is not included in the range, and an integer step value. The sequence is generated by
starting at the start integer and incrementing by the step value until the ending integer is reached or
surpassed.
ord() converts a 1-character string into an integer, and chr() converts an integer into a character.
Thus, chr(ord('a') + 1) results in 'b'
1) Incremental programming may help reduce the number of errors in a program. true
2) FIXME comments provide a way for a programmer to remember what needs to be added. true
3) Once a program is complete, one would expect to see several FIXME comments. false
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, not
using a break statement. Thus, the complete forms of while and for loops are:
The enumerate() function retrieves both the index and corresponding element value at the same time,
providing a cleaner and more readable solution.
A function definition consists of the new function's name and a block of statements. Ex: def
calc_pizza_area():. An indented block of statements follows the definition.
A function call is an invocation of the function's name, causing the function's statements to
execute.
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 parameter is a function input specified in a function definition. Ex: A pizza area function might
have diameter as an input.
An argument is a value provided to a function's parameter during a function call. Ex: A pizza
area function might be called as calc_pizza_area(12.0) or as calc_pizza_area(16.0).
A function's statements may include function calls, known as hierarchical function calls or nested
function calls
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. Ex: The
consecutive statements num = 5 and num = '7' first assign with an integer type, and then a string type.
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. Ex: string name = "John" would declare a string variable in C and C++.
Dynamic typing typically allows for more flexibility in terms of the code that a programmer can write,
but at the expense of potentially introducing more bugs, since there is no compilation process by which
types can be checked.
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
1) A key reason for creating functions is to help the program run faster. false
2) If a function's internal statements are revised, all function calls will have to be modified too. false
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.
part of the value of a function object is compiled bytecode that represents the statements to be
executed by the function. A bytecode is a low-level operation, such as adding, subtracting, or loading
from memory.