pdf week 2 Flashcards
What do conditionals allow in programming?
Conditionals allow a program to make decisions based on certain conditions, like choosing a path based on user input
Give examples of when conditionals are useful.
When a user inputs wrong data (e.g., expecting an integer but receiving text).
When reaching the end of a file.
After a sufficient number of actions are performed (e.g., printing numbers from 1 to 10).
>
greater than
<
less than
> =
greater than or equal to
<=
less than or equal to
==
equals to
!=
not equal to
What is control flow?
Control flow refers to the order in which individual statements, instructions, or function calls are executed in a program.
How does elif improve the efficiency of conditional statements?
elif allows the program to skip subsequent checks once a true condition is found, reducing unnecessary evaluations.
Why use an else statement?
An else statement provides a default action when none of the preceding conditions are met, simplifying logic.
What does the or operator do?
The or operator allows the program to evaluate multiple conditions, executing the code block if at least one condition is true.
What does the and operator do?
The and operator requires all conditions to be true for the code block to execute.
What is short-circuit evaluation?
In short-circuit evaluation, if the first condition of an and or or statement determines the outcome, the second condition is not evaluated.
What are nested conditionals?
Nested conditionals are conditionals placed inside other conditionals, allowing for more complex decision-making.
How can conditionals be used with strings?
You can compare strings, check for the presence of substrings, and manipulate strings based on conditions (e.g., modifying case, stripping whitespace).
What is short-circuit evaluation in programming?
Short-circuit evaluation means that in logical expressions, not all parts of the expression need to be evaluated if the result can be determined early.
How does short-circuit evaluation work with the and operator?
If the first expression connected by and evaluates to False, the entire compound expression is False without evaluating the second expression.
How does short-circuit evaluation work with the or operator?
If the first expression connected by or evaluates to True, the entire compound expression is True without evaluating the second expression.
How do you check for equality in Python?
Use == to check if two strings are equal, e.g., “test” == myString.
How do you check if two strings are not equal?
Use !=, e.g., “test” != myString.
What does the equality comparison return?
It returns a boolean value: True or False.
How does the equality comparison work?
It checks one character at a time from left to right.
How do you check if a substring exists in a string?
Use in, e.g., “test” in myString.