[go: up one dir, main page]

0% found this document useful (0 votes)
30 views4 pages

Python Concepts 8 Marks

The document discusses various Python concepts including types of function arguments, namespaces, exception handling, and user-defined exceptions. It explains positional, keyword, default, and variable-length arguments, as well as built-in, global, enclosed, and local namespaces. Additionally, it provides examples of try-except blocks for handling exceptions and creating custom exceptions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views4 pages

Python Concepts 8 Marks

The document discusses various Python concepts including types of function arguments, namespaces, exception handling, and user-defined exceptions. It explains positional, keyword, default, and variable-length arguments, as well as built-in, global, enclosed, and local namespaces. Additionally, it provides examples of try-except blocks for handling exceptions and creating custom exceptions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Python Concepts - 4 Marks Answers

1) Explain types of function arguments.


Python functions support various types of arguments to provide flexibility:

1. **Positional Arguments**: Passed in the order of parameters.


Example:
def greet(name, age):
print(name, age)

greet('Alice', 25)

2. **Keyword Arguments**: Passed using parameter names.


greet(age=25, name='Alice')

3. **Default Arguments**: Have default values.


def greet(name, age=18):
print(name, age)

greet('Bob')

4. **Variable-length Arguments**:
- *args: Multiple positional args
- **kwargs: Multiple keyword args

def show(*args):
for i in args:
print(i)

2) Explain types of namespaces in python.


Namespaces are used to manage variable names to avoid conflicts.

Types:
1. **Built-in Namespace**: Includes built-in functions and exceptions.
Example: print(), len(), Exception

2. **Global Namespace**: Variables defined at top-level of a script or module.


x = 10 # global variable

3. **Enclosed Namespace**: For nested functions.


def outer():
a=5
def inner():
print(a)
Python Concepts - 4 Marks Answers

4. **Local Namespace**: Inside a function.


def func():
y = 20 # local variable

3) Explain how to handle exception in python programming.


Python provides a mechanism to handle exceptions using try-except blocks. This ensures the program doesn't crash.

Example:
try:
x = int(input('Enter number: '))
result = 10 / x
print(result)
except ZeroDivisionError:
print('Division by zero error')
except ValueError:
print('Invalid input')
finally:
print('Execution completed')

try block contains risky code, except handles errors, finally executes cleanup code.

4) Explain try ---except block for exception handling in python.


The try-except block is used to catch and handle exceptions gracefully.

Syntax:
try:
# risky code
except ExceptionType:
# handling code

Example:
try:
a = 10 / 0
except ZeroDivisionError:
print('Cannot divide by zero')
print('Program continues...')

Multiple except blocks can handle different exceptions. You can also catch all exceptions using except Exception.

5) How to create a user defined exception?


Python Concepts - 4 Marks Answers
User-defined exceptions are custom exceptions created by inheriting from the Exception class.

Example:
class NegativeValueError(Exception):
pass

def set_age(age):
if age < 0:
raise NegativeValueError('Age cannot be negative')
print('Age set to', age)

try:
set_age(-5)
except NegativeValueError as e:
print('Error:', e)

6) Differentiate between local variable and global variable.


Local Variable:
- Declared inside a function.
- Scope limited to the function.

Global Variable:
- Declared outside all functions.
- Accessible across all functions.

Example:
x = 10 # global

def func():
y = 5 # local
print('Local y:', y)

print('Global x:', x)

7) Explain various functions of math module.


The math module provides mathematical functions:

- math.sqrt(x): Returns square root


- math.pow(x, y): x raised to power y
- math.ceil(x): Rounds up
- math.floor(x): Rounds down
- math.pi: Value of pi
Python Concepts - 4 Marks Answers
- math.factorial(n): Factorial of n

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

8) Explain with program how to create user defined exception in python.


Step 1: Create a class inheriting from Exception
Step 2: Use raise statement to trigger it
Step 3: Handle using try-except

Example:
class AgeTooSmallError(Exception):
pass

def check_age(age):
if age < 18:
raise AgeTooSmallError('Age must be 18 or above')
print('Access granted')

try:
check_age(16)
except AgeTooSmallError as e:
print('Exception:', e)

You might also like