0% found this document useful (0 votes)
16 views3 pages

Midterm 2 Review

The document discusses various types of loops in programming including while, for, and nested loops. It also covers functions, parameters, arguments, return values, and polymorphism.
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)
16 views3 pages

Midterm 2 Review

The document discusses various types of loops in programming including while, for, and nested loops. It also covers functions, parameters, arguments, return values, and polymorphism.
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/ 3

A loop is a program construct that repeatedly executes the loop's statements (known as the loop body)

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(5) 01234 Every integer from 0 to 4.

range(0, 5) 01234 Every integer from 0 to 4.

range(3, 7) 3456 Every integer from 3 to 6.

range(10, 13) 10 11 12 Every integer from 10 to 12.

range(0, 5, 1) 01234 Every 1 integer from 0 to 4.

range(0, 5, 2) 024 Every 2nd integer from 0 to 4.

range(5, 0, -1) 54321 Every 1 integer from 5 down to 1

range(5, 0, -2) 531 Every 2nd integer from 5 down to 1

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.

The range() function can take up to three integer arguments.


A nested loop is a loop that appears as part of the body of another loop. The nested loops are commonly
referred to as the outer loop and inner loop.

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 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, 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 is a named series of statements.

 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 may return one value using a return statement

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.

Polymorphism refers to how an operation depends on the involved object types.

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.

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

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.

 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 bytecode is a low-level operation, such as adding, subtracting, or loading
from memory.

1) Forgetting to return a value from a function is a common error.  true


2) Copying-and-pasting code can lead to common errors if all necessary changes are not made to
the pasted code.  true
3) Returning the incorrect variable from a function is a common error. true

You might also like