Different to if statements (main difference is the use of a colon, ’:’). This syntax always requires an else statement. Similar to conditional (ternary) operators.
Syntax: <value_if_true> if <condition> else <value_if_false>
n = -5res = "Positive" if n > 0 else "Negative" if m < 0 else "Zero"# The above expression can be written asres = "Positive" if n > 0 else ("Negative" if n < 0 else "Zero")
Lambda functions
Anonymous functions (function without a name). It would be named if it is assigned.
Useful for reducing complexity and repetition.
Syntax: lambda <arguments>: <expression>
# check if number is positive, negative or zeroclassify = lambda num: "Positive" if num > 0 else "Negative" if num < 0 else "Zero"classify(5) # Positiveclassify(-5) # Negativeclassify(0) # Zero# multiple parameterscalc = lambda x, y: (x + y, x * y)calc(3, 4) # (7, 12)# callbacksn = [1, 2, 3, 4, 5, 6]list(filter(lambda x: x % 2 == 0, n)) # [2, 4, 6]list(map(lambda x: x * 2, n)) # [2, 4, 6, 8, 10, 12]