0% found this document useful (0 votes)
0 views

Python Vol 1 Technical Publication

The document provides definitions and examples of key Python concepts such as functions, modules, packages, and exceptions. It outlines the advantages of user-defined functions, types of function arguments, and the use of namespaces. Additionally, it explains errors and exceptions, lists predefined exceptions, and describes recursive functions with examples.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Python Vol 1 Technical Publication

The document provides definitions and examples of key Python concepts such as functions, modules, packages, and exceptions. It outlines the advantages of user-defined functions, types of function arguments, and the use of namespaces. Additionally, it explains errors and exceptions, lists predefined exceptions, and describes recursive functions with examples.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Python Concepts - 4 Marks Answers

1) Define Function, module, package in python.


Function: A function in Python is a reusable block of code that performs a specific task. It allows programmers to break
down complex problems into smaller, manageable parts. Functions improve modularity and reuse of code.
Example:
def greet(name):
print('Hello,', name)

Module: A module is a file containing Python definitions and statements. It helps in organizing code logically and can be
imported into other Python scripts using the import statement.
Example: import math

Package: A package is a collection of modules organized in directories containing a special __init__.py file. It allows
hierarchical structuring of the module namespace.

2) Define Exception.
An exception is a runtime error that occurs during the execution of a program. It disrupts the normal flow of the program
and can be handled using try-except blocks. Handling exceptions prevents program crashes and allows for graceful
error messages.
Example:
try:
a = 10 / 0
except ZeroDivisionError:
print('Cannot divide by zero')

3) Enlist advantages of user defined function.


1. Code Reusability: Functions allow the reuse of code multiple times, avoiding duplication.
2. Modularity: They help in dividing large programs into smaller modules.
3. Easy to Debug: Isolated blocks of code are easier to test and debug.
4. Improved Readability: Functions enhance clarity and structure in programs.
5. Reduces Code Complexity: By hiding implementation details inside a function.

4) Write syntax of user defined function.


The general syntax of a user-defined function in Python is:

def function_name(parameters):
'''optional docstring'''
# function body
return [expression]

Example:
def add(a, b):
Python Concepts - 4 Marks Answers
return a + b

5) Enlist types of function arguments.


1. Positional Arguments: Values passed in the same order as parameters.
2. Keyword Arguments: Arguments passed with parameter names.
3. Default Arguments: Parameters with default values.
4. Variable-length Arguments: Allows passing arbitrary number of arguments using *args or **kwargs.

Example:
def show(name, age=18):
print(name, age)

6) State use of namespace in Python


A namespace is a container that holds variable names and their values. It ensures that names are unique and prevents
naming conflicts.
Types:
- Local Namespace: Inside a function
- Global Namespace: Top-level of a script
- Built-in Namespace: Includes built-in functions like print()

Use: It allows Python to determine which object name refers to at any point in the program.

7) Write syntax to import module in python.


Modules can be imported in the following ways:
1. import module_name
2. import module_name as alias
3. from module_name import function_name
4. from module_name import *

Example:
import math
print(math.sqrt(16))

8) Define the terms i) error ii) exception


i) Error: Errors are issues in a program that prevent it from executing. Syntax errors are the most common type.
Example: print('Hello' -> SyntaxError

ii) Exception: Exceptions occur during execution and can be handled. Examples include FileNotFoundError,
ZeroDivisionError. They are caught using try-except blocks.

9) List predefined exceptions in python.


Python Concepts - 4 Marks Answers
Common predefined exceptions:
- ZeroDivisionError: Division by zero
- IndexError: Accessing out-of-range list index
- ValueError: Wrong value passed to a function
- FileNotFoundError: File not found on disk
- TypeError: Operation on incompatible data types
- KeyError: Accessing missing dictionary key

10) What is recursive function.


A recursive function is a function that calls itself to solve smaller instances of a problem. It is useful in problems like
factorial, Fibonacci, tree traversals, etc.

Example:
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n - 1)

Recursive functions must have a base case to avoid infinite recursion.

You might also like