Conditional Operators, Recursion, and Comprehensions in Python
This article explains Python's conditional (ternary) operators, the use of if‑else expressions, logical and/or operators, recursion fundamentals including stack frames and recursion limits, and demonstrates list, set, and dictionary comprehensions with clear code examples.
Recursion is defined as a function that calls itself; a new call that starts before an earlier call finishes constitutes recursion.
1. Conditional Operators
ternary operator
<code># expression format
<expression1> ? <expression2> : <expression3></code> <code># example
In [1]: a = 1
In [2]: b = 2
In [3]: max = (a > b) ? a : b</code>if and else
<code># expression format
value_when_true if condition else value_when_false</code> <code># example
In [4]: 'True' if True else 'False'
Out[4]: 'True'
In [5]: 'True' if False else 'False'
Out[5]: 'False'</code>old‑style syntax (not recommended)
<code># expression format
[value_when_false, value_when_true][bool(condition)]</code> <code># example
In [6]: ['False', 'True'][True]
Out[6]: 'True'
In [7]: ['False', 'True'][False]
Out[7]: 'False'</code>and and or
<code># expression format
condition and value_when_true or value_when_false</code> <code># examples
In [11]: 'a' and 'b'
Out[11]: 'b'
In [12]: 'a' and False
Out[12]: False
In [13]: False and 'b'
Out[13]: False
In [14]: 'a' and False and 'b'
Out[14]: False
In [15]: 'a' and True and 'b'
Out[15]: 'b'
In [16]: 'a' or 'b'
Out[16]: 'a'
In [17]: 'a' or False
Out[17]: 'a'
In [18]: False or 'b'
Out[18]: 'b'</code>2. Recursion Operations
Definition of recursion
<code>def fac(n):
if n == 1:
return 1
else:
return n * fac(n-1)
# using a one‑line conditional
def fac(n):
return 1 if n == 1 else n * fac(n-1)</code>Recursion depth
Function calls are managed by a call stack; each call adds a stack frame, and each return removes one. Because the stack size is finite, recursion depth is limited.
In Python2 the default recursion limit is 1000 ; in Python3 it is 3000 . The limit can be changed with sys.setrecursionlimit() .
<code>import sys
sys.getrecursionlimit()
# returns 3000
sys.setrecursionlimit(1000)</code>3. Parsing and Derivation (Comprehensions)
List comprehension
<code># examples
[i * i for i in [1, 2, 3, 4]]
# -> [1, 4, 9, 16]
[i * i for i in [1, 2, 3, 4] if i % 2]
# -> [1, 9]
[i * i for i in range(10) if i % 2 and i % 3]
# -> [1, 25, 49]</code>Set comprehension
<code>{i * i for i in [1, 2, 3, 4, 1]}
# -> {1, 4, 9, 16}</code>Dictionary comprehension
<code>{k: v * v for k, v in [('a', 1), ('b', 2)]}
# -> {'a': 1, 'b': 4}</code>Original source: https://www.escapelife.site/posts/58dee03c.html
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.