Functional programming - Python

in #coding4 years ago

  • What is the output of this code?
    nums = {1, 2, 3, 4, 5, 6}
    nums = {0, 1, 2, 3} & nums
    nums = filter(lambda x: x > 1, nums)
    print(len(list(nums)))

Result - 2

  • What is the output of this code?
    def power(x, y):
    if y == 0:
    return 1
    else:
    return x * power(x, y-1)

print(power(2, 3))

Result - 8

  • Fill in the blanks to calculate the expression x*(x+1) using an anonymous function and call it for the number 6.

a = (_____ x: x__ (x+1))_____

print(a)

Answer - lambda, *, 6

a = (lambda x: x* (x+1)) (6)

Result - 42

  • Fill in the blanks to leave only even numbers in the list.

nums = [1, 2, 8, 3, 7]
res = list( _____(_______x: x%__==0, nums))
print(res)

Answer: filter
lambda
2

Answering or trying to answer practice questions as this helps remember lessons while studying Python.

image source

Coin Marketplace

STEEM 0.30
TRX 0.11
JST 0.033
BTC 64243.42
ETH 3152.93
USDT 1.00
SBD 4.28