# examples of conditional expressions # # Matt Bishop, ECS 36A, Winter 2019 # # first, an "if" value = 0 if value > 0: sgn = 1 else: sgn = 0 print("in regular if, sgn should be 0 --", sgn) # now as a conditional expression sgn = 1 if value > 0 else 0 print("in conditional expression, sgn should be 0 --", sgn) # now an if .. else if .. else value = -200 if value > 0: sgn = 1 elif value == 0: sgn = 0 else: sgn = -1 print("in regular if/elif/else, sgn should be -1 --", sgn) # now as a conditional expression sgn = 1 if value > 0 else 0 if value == 0 else -1 print("in conditional expression, sgn should be -1 --", sgn) # and now the recursive factorial function def rcefact(n): return 1 if n == 1 or n == 0 else n * rcefact(n-1) print("7! =", rcefact(7)) print("10! =", rcefact(10))