1. What is function?
Demonstrate defining and calling user defined function with syntax and
example.
A function is a block of reusable code that performs a specific task. It helps in code reusability and
modularity.
Syntax:
def function_name(parameters): '''docstring''' statements return expression Example:
def greet(name): print("Hello,", name) greet("Rohan") Output: Hello, Rohan
2. Python program to calculate factorial of a number using user defined function.
def factorial(n): fact = 1 for i in range(1, n + 1): fact *= i return fact num = int(input("Enter a number:
")) print("Factorial of", num, "is", factorial(num))
3. Explain recursion with an example.
Recursion is the process in which a function calls itself directly or indirectly. It continues until a
base condition is met.
Example:
def countdown(n): if n == 0: print("Blast off!") else: print(n) countdown(n - 1) countdown(5)
4. Explain exception handling in python.
Exception Handling is used to handle runtime errors in a program, preventing the program from
crashing.
It uses keywords try, except, else, finally to manage exceptions.
5. What is exception handling? Write a program to demonstrate exception handling.
Exception handling means managing errors using try-except blocks.
Example:
try: a = int(input("Enter a number: ")) b = int(input("Enter another number: ")) print("Result:", a / b)
except ZeroDivisionError: print("Division by zero is not allowed!") except ValueError: print("Invalid
input! Please enter numbers only.")
6. What is recursion? Write a program to find factorial of a number using recursion.
Recursion is when a function calls itself to solve smaller instances of the same problem.
Example:
def factorial(n): if n == 0 or n == 1: return 1 else: return n * factorial(n - 1) num = int(input("Enter a
number: ")) print("Factorial:", factorial(num))
7. Write syntax to define functions in python. Write a python program to find largest of two
numbers using function.
Syntax:
def function_name(parameters): statements Example:
def largest(a, b): if a > b: return a else: return b x = int(input("Enter first number: ")) y =
int(input("Enter second number: ")) print("Largest number is:", largest(x, y))
8. Write a python program to demonstrate try, except and finally block.
try: num = int(input("Enter a number: ")) result = 10 / num except ZeroDivisionError: print("Cannot
divide by zero!") finally: print("Execution completed.")
9. Explain different types of arguments in functions of python.
Python supports four types of arguments:
1. Positional arguments: Passed in correct order.
2. Keyword arguments: Passed using parameter names.
3. Default arguments: Have default values.
4. Variable-length arguments: *args and **kwargs for multiple arguments.
10. What are default arguments? Explain with an example.
Default arguments are parameters that assume a default value if no value is passed.
Example:
def greet(name="Guest"): print("Hello,", name) greet() greet("Rohan")
11. What are command line arguments? Explain with a program.
Command line arguments are values passed to a Python script when it is executed from the
terminal.
Example:
import sys print("Arguments:", [Link]) If file name is [Link], running it as:
python [Link] hello world Output: ['[Link]', 'hello', 'world']
12. Define keyword arguments. Explain with a program.
Keyword arguments are passed by using parameter names while calling the function.
Example:
def info(name, age): print("Name:", name) print("Age:", age) info(age=20, name="Rohan")