Core Python / Control Statements#
What is the type and value of x?
int 10
Use type(x) and print(x) to verify.
orreturns the actual value of the first “true” expression. Forandif the clause evaluates toTrue, the last expression evaluated is returned.Why is the order of
ifandelifclauses important?The Python interpreter evaluates the clauses in their listed order. Only the code block associated with the first clause that evaluates to true executes.
Explain short-circuit evaluation for
andandorclauses.Once the Python interpreter can determine the outcome of a Boolean expression, the interpreter no longer needs to continue evaluation.
For
and, if the first expression evaluates toFalse, the second expression will not be evaluated as the result of the operator will always beFalse.For
or, if the first expression evaluates toTrue, the second expression will not be evaluated as the result of the operator will always beTrue.How are code blocks identified in Python?
Indention. This indention must be consistent throughout the source code file. Other programming languages often use curly braces.
What is the difference between
if/elifstatement(s) and a series ofifstatements?With
if/elifstatements, only the code block associated with the first clause that evaluates toTrueexecutes. With a series ofifstatements, each code block associated with aTrueclause will evaluate.Why is testing for equality with float numbers problematic? How do we work around such situations?
Floating-point numbers do not have an exact representation. As such certain calculations may not produce the exact answer expected. Use the
math.isclose()instead of the equality operator.