Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Python 10 questions

Functions and Lambda Expressions

Define Python functions, pass args, kwargs, return values, closures, nested scopes, and anonymous lambda expressions.

More Python
Interview questions 1–10 of 10
1

What is a function in Python and what is function scope?

Function is reusable code block. Scope determines variable accessibility. Python has 4 scopes: Local, Enclosin...

Read answer →
2

What are *args and **kwargs?

def add(*args): return sum(args) print(add(1, 2, 3)) # 6 def info(**kwargs): for k, v in kwargs.ite...

Read answer →
3

What is a lambda function?

square = lambda x: x ** 2 print(square(5)) # 25 # With map, filter, sorted nums = [1, 2, 3, 4, 5] squared =...

Read answer →
4

What is the mutable default argument trap?

# WRONG - shared default! def append_to_list(item, my_list=[]): my_list.append(item) return my_list p...

Read answer →
5

How do you return multiple values from a function?

def get_user(): name = 'John' age = 30 email = 'john@gmail.com' return name, age, email # Ret...

Read answer →
6

What is a docstring and why is it important?

def add(a, b): '''Add two numbers. Args: a: First number b: Second number...

Read answer →
7

What is a decorator?

def my_decorator(func): def wrapper(): print('Before') func() print('After') r...

Read answer →
8

What is a closure?

def make_multiplier(n): def multiplier(x): return x * n # Captures n return multiplier multi...

Read answer →
9

What are function annotations?

def add(a: int, b: int) -> int: '''Add two integers.''' return a + b print(add.__annotations__) # {'a...

Read answer →
10

What are map, filter, and reduce?

from functools import reduce # map - apply function to each element nums = [1, 2, 3, 4, 5] squared = list(map...

Read answer →