Sitemap
Krishna Chaitanya

Krishna Chaitanya

Python Functions

3 min readJun 27, 2025

--

1. Python function — definition with example

2. Usage of functions

3. List of Python functions:

- Predefined functions with examples

- User-defined functions:

- General function (no params, no return)

- Function with parameters:

- Default arguments

- Keyword arguments

- Positional arguments

- Arbitrary arguments (*args, **kwargs)

- Function with return type

- Anonymous (lambda) function

- Recursive function

Function:

A function in Python is a reusable block of code that performs a specific task. Functions help organize code, make it modular, and reduce repetition.

Press enter or click to view image in full size

Syntax:

def function_name(parameters):

statement(s)

Example:

def greet():

print(“Hello, World!”)

greet() # Output: Hello, World!

2. What is the Use of Functions?

- Avoid code repetition

- Increase modularity and readability

- Easy debugging and maintenance

- Encourages reuse of code

3. List of Python Functions

3.1 Predefined Function Definition with Examples

These are built-in functions provided by Python.

Examples:

print(“Hello”) # Output: Hello

len(“Python”) # Output: 6

type(10.5) # Output: <class ‘float’>

sorted([3, 1, 2]) # Output: [1, 2, 3]

sum([1, 2, 3]) # Output: 6

3.2 User Defined Functions

3.2.1 General Function — Without Parameters and No Return Type

Syntax:

def function_name():

statements

Example:

def say_hello():

print(“Hello!”)

say_hello()

3.2.2 Python Function with Parameters

a. Default Arguments

def greet(name=”User”):

print(“Hello,”, name)

greet() # Output: Hello, User

greet(“Krishna”)

# Output: Hello, Krishna

b. Keyword Arguments (Named Arguments)

def student(name, age):

print(“Name:”, name)

print(“Age:”, age)

Get Krishna Chaitanya’s stories in your inbox

Join Medium for free to get updates from this writer.

student(age=25, name=”Moni”)

c. Positional Arguments

def add(a, b):

print(a + b)

add(3, 5)

# Output: 8

d. Arbitrary Arguments (Variable-Length Arguments)

Using *args:

def total_sum(*numbers):

print(sum(numbers))

total_sum(1, 2, 3, 4) # Output: 10

Using **kwargs:

def display_info(**info):

for key, value in info.items():

print(f”{key}: {value}”)

display_info(name=”Chaitanya”, age=30)

3.2.3 Python Function with Return Type

def multiply(x, y):

return x * y

result = multiply(4, 5)

print(result)

# Output: 20

3.3 Anonymous Function / Lambda Function

A lambda function is a small, unnamed function defined using the lambda keyword.

Syntax:

lambda arguments: expression

Example:

double = lambda x: x * 2

print(double(5))

# Output: 10

3.4 Recursive Function

A recursive function is one that calls itself within its definition.

Example:

def factorial(n):

if n == 0:

return 1

else:

return n * factorial(n — 1)

print(factorial(5))

# Output: 120

Interview Questions on Python Functions

  1. What is the difference between a function and a method in Python?
  2. What are the different types of arguments in Python functions?
  3. What are *args and **kwargs? When would you use them?
  4. How does Python handle default argument values?
  5. What is the difference between a return statement and print?
  6. Explain lambda functions. How are they different from regular functions?
  7. Can a function return multiple values?
  8. What is recursion? Give a real-time use case.
  9. How do Python’s built-in functions help in day-to-day coding?
  10. What are the advantages of using functions in large codebases?

Tasks for Practice

  1. Create a Calculator — Use functions for operations like add, subtract, multiply, divide.
  2. Student Management System — Use keyword and default arguments to input student data.
  3. Invoice Generator — Use return functions and lambda to apply discounts and calculate totals.
  4. Dynamic Greetings System — Use arbitrary arguments to greet multiple users.
  5. Recursive Task — Write a function to compute Fibonacci numbers recursively.
  6. Data Processor — Use *args and **kwargs to handle dynamic data fields.
  7. Lambda Sorting — Use lambda to sort a list of dictionaries by a specific key.
  8. Quiz Application — Functions to ask questions, validate answers, and keep score.
  9. String Analyzer — Function that counts vowels, consonants, spaces, and symbols.
  10. Bank Transaction System — Simulate deposits and withdrawals using functions and return values.

--

--