def Function in Python — Python Technical Interview Questions & Answers

def Function in Python

def Function in Python

  • def is the keyword that creates a named function. Shape: def total(a, b): return a + b. Colon, indented body. Optional docstring on the first line inside. Functions are objects — you can pass them to sorted(key=...) or store them. Without return, the caller gets None.
  • def vs lambda: def has a clear name and can have many lines. lambda is one expression. Freshers should live in def until the key= case appears.
  • def Function in Python — output: Hi Asha. def builds greet; the call runs it.
  def square(n):
      return n * n
         │ square(5)
         ▼
        25

Technical Interview Tip: def vs lambda: named reusable vs tiny expression.

Code Examples for def Function in Python

Example

# def
def greet(name):
    return "Hi " + name

print(greet("Asha"))

def Function in Python — output: Hi Asha. def builds greet; the call runs it.

How to Explain def Function in Python to an Interviewer

Explain def Function as if you are teaching a junior — definition, then one tiny script.

def creates a named function. Indented body + return.

What does the example print, and why?

def Function in Python — output: Hi Asha

What mistake do freshers make with def Function?

Indented body + return.