4.7: Conditional Expressions
- Page ID
- 117546
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\dsum}{\displaystyle\sum\limits} \)
\( \newcommand{\dint}{\displaystyle\int\limits} \)
\( \newcommand{\dlim}{\displaystyle\lim\limits} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\(\newcommand{\longvect}{\overrightarrow}\)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\ket}[1]{\left| #1 \right>}\)
\(\newcommand{\bra}[1]{\left< #1 \right|}\)
\(\newcommand{\braket}[2]{\left< #1 \vphantom{#2} \right| \left. #2 \vphantom{#1} \right>}\)
\(\newcommand{\braopket}[3]{\left< #1 \vphantom{#2}\vphantom{#3} \right| #2 \vphantom{#1}\vphantom{#3} \left| #3 \vphantom{#1}\vphantom{#2} \right>}\)
\(\newcommand{\qmvec}[1]{\mathbf{\vec{#1}}}\)
\(\newcommand{\op}[1]{\hat{\mathbf{#1}}}\)
\(\newcommand{\expect}[1]{\langle #1 \rangle}\)
\(\newcommand{\dfn}[1]{\emph{\textbf{#1}}}\)
By the end of this section you should be able to
- Identify the components of a conditional expression.
- Create a conditional expression.
Conditional expressions
A conditional expression (also known as a "ternary operator") is a simplified, single-line version of an if-else statement.
Conditional expression template:
expression_if_true if condition else expression_if_false
A conditional expression is evaluated by first checking the condition. If condition is true, expression_if_true is evaluated, and the result is the resulting value of the conditional expression. Else, expression_if_false is evaluated, and the result is the resulting value of the conditional expression.
A variable can be assigned with a conditional expression. Ex: Finding the max of two numbers can be calculated with max_num = y if x < y else x
Note: Conditional expressions have the lowest precedence of all Python operations.
What is the conditional expression version of the following if-else statement?
if x%2 == 0:
response = 'even'
else:
response = 'odd'
response = if x%2 == 0 "even" else "odd"response = "odd" if x%2 == 0 else "even"response = "even" if x%2 == 0 else "odd"
- Answer
-
c. The conditional expression will assign
responsewith"even"ifxis even.responseis assigned with"odd"ifxis odd.
Given x = 100 and offset = 10, what is the value of result?
result = x + offset if x < 100 else x - offset
90100110
- Answer
-
a.
100 < 100isFalsesoresultis assigned withx - offset = 100 - 10 = 90.
Which part of the conditional expression is incorrect?
min_num = x if x < y else min_num = y
min_num = xx < ymin_num = y
- Answer
-
c.
min_numcannot be assigned withmin_num = y. The correct expression ismin_num = x if x < y else y"
Which of the following is an improved version of the following if-else statement?
if x < 50:
result = True
else:
result = False
result = True if x < 50 else Falseresult = x < 50
- Answer
-
b. Conditional expressions that evaluate to a Boolean are redundant. The Boolean expression should be used instead.
What are the possible values of total?
total = fee + 10 if hours > 12 else 2
10,2fee + 10,2fee + 10,fee + 2
- Answer
-
b. The expression is evaluated as
(fee + 10) if hours > 12 else 2. So if hours > 12, the result will be fee + 10; otherwise, the result will be 2.
Write a program that reads in an integer, ping, and prints ping_report, a string indicating whether the ping is low to average or too high. ping values under 150 have a ping_report of "low to average". ping values of 150 and higher have a ping_report of "too high". Use a conditional expression to assign ping_report.
Ex: If the input is 30, the output is "Ping is low to average".
Interactive Code
- Input
-
150 - Answer
-
ping = int(input())
ping_report = "low to average" if ping < 150 else "too high"print("Ping is", ping_report)


