1/79
second exam
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No study sessions yet.
What is the purpose of a repetition structure?
To repeat a set of instructions multiple times until a certain condition is met.
How do you construct a repetition structure in a program flowchart?
Use a decision diamond to represent the condition, with arrows for “True” (loop back) and “False” (exit the loop).
How do you construct a WHILE loop?
Use a loop that continues to execute while a condition remains True.
What are the 3 steps to include in a WHILE loop?
Initialize the loop control variable.
Test the condition.
Update the loop control variable.
What is an iteration variable?
A variable that keeps track of the number of iterations or controls how many times a loop runs.
How do you construct a definite WHILE loop?
Use a loop with a known number of iterations, typically controlled by a counter variable.
How do you construct an indefinite WHILE loop?
Use a loop that continues until a user or specific event stops it; condition depends on user input or a sentinel value.
What is meant by an infinite loop?
A loop that never ends because the condition never becomes False.
How do you construct a FOR loop?
Use the syntax for variable in iterable: followed by indented statements.
What is a target variable?
The variable that takes on each value in the iterable during each iteration.
What is meant by an iterable?
A sequence or collection of items that can be looped over, such as a list, string, or range.
Are lists iterables?
Yes.
Does the range() function produce an iterable?
Yes, it generates a sequence of numbers that can be looped through.
How do you construct a FOR loop that iterates through a list of values?
for item in [list]:
statement
What symbol represents a list?
Square brackets [ ].
Can a list be defined as a variable?
Yes, for example: numbers = [1, 2, 3].
How do you construct a FOR loop that iterates through a range of values?
for i in range(start, stop, step):
statement
What are the three arguments of the range() function?
start, stop, and step.
What happens when we use only 1, 2, or 3 arguments in range()?
1 argument → range(0, stop)
2 arguments → range(start, stop)
3 arguments → range(start, stop, step)
Which loops should you know how to code?
WHILE (definite and indefinite), FOR..in LIST, and FOR..in RANGE().
For any loop, how do you make it count up or count down?
Adjust the update statement or the step value in range() to increment or decrement.
What does the term iteration mean in loops?
One complete execution of the loop body.
How can you determine how many times a loop will iterate?
Evaluate how the loop control variable changes relative to its start, end, and step values or the condition.
What is an augmented assignment operator?
A shorthand way to update a variable’s value using an operation (e.g., x += 1 instead of x = x + 1).
What are the synonyms for augmented assignment operators?
Compound operators or shortcut operators.
What are the augmented assignment operators for mathematical operators?
Addition: +=
Subtraction: -=
Multiplication: *=
Division: /=
Integer Division: //=
Modulus: %=
Exponentiation: **=
What is the purpose of a counter variable?
To count the number of times a loop executes or an event occurs.
How do you initialize and update a counter variable?
Initialize before the loop (e.g., count = 0) and update inside the loop (e.g., count += 1).
What is a running total?
A variable that accumulates the sum of values as the loop iterates.
How do you initialize and update a running total?
Initialize before the loop (e.g., total = 0) and update inside (e.g., total += value).
What is input validation?
Ensuring the user enters valid data before proceeding.
How do you validate input using a while loop?
while value not valid:
ask for input again
When should you convert a user’s numeric input to int or float?
After validating the input to ensure it’s a valid number.
What does the break statement do?
Exits the loop immediately.
What does the continue statement do?
Skips the rest of the current iteration and moves to the next one.
What does the pass statement do?
Acts as a placeholder that does nothing; used to maintain syntax structure.
How do you construct a nested loop?
Place one loop inside another.
When should you write code in each of the 3 locations within a nested loop (A, B, C)?
A: Before the inner loop (setup actions)
B: Inside the inner loop (main repeated action)
C: After the inner loop (cleanup actions)
Can you nest different types of loops?
Yes, any loop type (WHILE, FOR in LIST, FOR in RANGE) can be nested inside another.
How do you write a nested loop to display tabular data?
Outer loop controls rows; inner loop controls columns.
What do we process in the outer loop?
The major grouping or row-level data.
What do we process in the inner loop?
The smaller repeated units or column-level data.
What is a function?
A piece of prewritten code that performs an operation.
What are examples of built-in Python functions?
print(), input(), int(), float(), type()
What is the purpose of writing your own functions?
To make code reusable, modular, organized, and easier to maintain.
What are the benefits of using functions?
Reduces complexity
Increases code reuse
Speeds up development
Easier to test and maintain
Improves quality
Decreases cost
What is top-down design?
Breaking a program into smaller subtasks, each coded as separate functions.
What is a modularized program?
A program whose tasks are divided into functions to organize code and promote reuse.
What are the two main types of functions?
Void functions and value-returning functions.
What is the difference between them?
Void functions perform an action but do not return a value.
Value-returning functions perform an action and return a value to the caller.
What are the two parameter types for functions?
With parameters (requires input values)
Without parameters (no inputs required)
What is a void function?
A function that does not return a value; typically just outputs information using print().
What is the syntax for defining a void function without parameters?
def function_name():
statements
What is a good naming convention for functions?
Use a verb phrase that describes what the function does.
Example of a void function without parameters:
def display_greeting():
print('\nWelcome User!')
Where must the function call to main() be placed in a Python program?
At the bottom, after all function definitions.
Why must main() be at the bottom?
Because a function must be defined before it can be called.
What is the difference between the “calling function” and the “called function”?
Calling function: The one that makes the function call (e.g., main()).
Called function: The one being executed (e.g., display_greeting()).
What is a parameter?
A variable listed in a function definition that receives input from outside the function.
What is an argument?
A value passed into a function when it is called.
How are parameters and arguments related?
The argument’s value is passed into the parameter during the function call.
What is the syntax for defining a function with a parameter?
def display_greeting(name):
print(f'Welcome, {name}!')
What is the syntax for calling a function with a parameter?
display_greeting('Bob')
What happens if you define a function that uses a parameter but call it without passing an argument?
You will get a TypeError, because the function is missing a required argument.
Can a function have multiple parameters?
Yes, for example:
def calc_total_pay(hours_worked, pay_rate):
total_pay = hours_worked * pay_rate
print(f'Total pay: ${total_pay:.2f}')
What are local variables?
Variables defined inside a function; only accessible within that function.
What is scope in Python?
The region of code where a variable is accessible.
Where is a local variable’s scope?
Only within the function it’s defined in.
Why must we pass values between functions using parameters?
Because functions do not automatically share local variables.
What is a dynamic greeting example using parameters?
def display_greeting(name):
print(f'Welcome, {name}!')
From the perspective of the programmer writing the function, what are they defining?
Parameters — the input values required by the function.
From the perspective of the programmer calling the function, what are they providing?
Arguments — the values passed to the function.
Can arguments be literals, variables, or expressions?
Yes — any of the three can be passed as arguments.
What is an optional parameter?
A parameter that has a default value and doesn’t require an argument to be passed.
What rule must optional parameters follow?
They must appear after all required parameters in the function header.
Example of a function with an optional parameter:
def greet(name='User'):
print(f'Welcome, {name}!')
What is one common error when using functions?
Forgetting to call main() at the bottom of the program.
Another common error?
Calling a function before it has been defined.
Why might a variable not be recognized in another function?
Because it was defined locally and not passed as a parameter.