1. What is the difference between a parameter and an argument?
Sol :- A parameter is a variable defined in the function definition to accept input. An argument is the actual value passed to the function during a function call.
2. What is a default parameter in Python? Provide an example.
Sol:- A default parameter has a predefined value in the function definition.
Example.
def greet(name=”Guest”):
print(f”Hello, {name}!”)
greet() # Output: Hello, Guest!
greet(“Alice”) # Output: Hello, Alice!
3.Can a function have both positional and keyword arguments? Explain.
Sol:- Yes, a function can have both. Positional arguments are passed based on their order, and keyword arguments are explicitly named.
4.What is a keyword argument? How is it different from a positional argument?
Sol:- Keyword arguments specify the parameter name, making the order irrelevant. Positional arguments rely on the order of parameters.
5.What is an arbitrary positional argument (*args)?
Sol:- args allows a function to accept any number of positional arguments.
Example
def add(*numbers):
return sum(numbers)
print(add(1, 2, 3)) # Output: 6
6. What is an arbitrary keyword argument (**kwargs)?
Sol:- kwargs allows a function to accept any number of keyword arguments.
Example
def info(**details):
return details
print(info(name=”Alice”, age=25)) # Output: {‘name’: ‘Alice’, ‘age’: 25}
7.Why do we use the * symbol in function parameters?
Sol:- To unpack multiple arguments into a tuple (*args) or a dictionary (**kwargs).
8.What happens if a required argument is not provided in a function call?
Sol:- A TypeError is raised indicating missing required arguments.
9.What is the purpose of the return statement in a function?
Sol:- To send a result or value back to the caller.
10.What is the difference between a required argument and a default argument?
Sol:- A required argument must be provided in the function call, while a default argument has a predefined value.
11. What will happen if the order of positional and keyword arguments is reversed in a function call?
Sol:- A SyntaxError will occur because positional arguments must come before keyword arguments.
12. Explain the use of *args and **kwargs in a function.
Sol :- args collects multiple positional arguments, and **kwargs collects multiple keyword arguments.
13.What happens when too many arguments are passed to a function?
Sol:- A TypeError is raised for extra arguments.
14. Can we call a function without passing any arguments? Explain.
Sol:- Yes, if the function has default parameters or does not require arguments.
15. What happens when a default argument comes before a required argument in a function definition?
Sol:- A SyntaxError occurs because required arguments must precede default ones.
16. Can you pass an argument by reference in Python? Why or why not?
Sol:- Mutable objects are passed by reference, so changes persist, while immutable objects are passed by value.
17. What is the scope of a variable?
Sol:- The part of the code where a variable is accessible.
18.What are the different types of variable scopes in Python?
Sol:- Local, Enclosed, Global, Built-in (LEGB).
19. What is a local variable? Provide an example.
Sol:- Defined inside a function, accessible only within it.
20. What is a global variable? Provide an example.
Sol:- Defined outside any function, accessible throughout the program.
21. What is the purpose of the global keyword?
Sol:- To modify global variables inside a function.