Control Flow and Functions Flashcards
5 cards from real PCEP practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 5 Control Flow and Functions flashcards as text
Which of the following is the correct syntax for an "if" statement in Python?
Answer: if condition: statement
Python's `if` statement uses a specific and clear syntax: the `if` keyword, followed by a condition, and then a colon `:`. The code block to be executed if the condition is true must be indented below the `if` line. This structure clearly defines the conditional logic, making Python code readable and consistent.
What does the "return" statement do in a function?
Answer: It ends the function and sends a value back to the caller.
The `return` statement is used inside a function to immediately exit its execution and optionally send a value back to the part of the code that called the function. Once `return` is executed, no further code within that function will run. This allows functions to produce results that can be used elsewhere in the program.
How do you define a function in Python?
Answer: def myFunction():
In Python, functions are defined using the `def` keyword, followed by the function name, a pair of parentheses `()`, and a colon `:`. The code block that constitutes the function's body must then be indented below this definition line. This syntax establishes a new callable function, encapsulating a block of reusable code.
Which keyword is used to exit a loop prematurely in Python?
Answer: break
The `break` statement in Python is used to terminate the current loop (either `for` or `while`) prematurely. When `break` is encountered, the loop immediately stops, and program execution continues with the statement immediately following the loop. This is useful for exiting a loop based on a specific condition being met.
In Python, what does the "else" block of a "for" loop execute for?
Answer: When the loop has no more items to iterate over
In Python, a `for` loop can optionally have an `else` block, which executes only if the loop completes all its iterations without encountering a `break` statement. If the loop is terminated by a `break`, the `else` block is skipped entirely. This provides a way to execute specific code only when a loop finishes naturally.