Python Function Coding Questions
1. Write a function `greet()` that prints 'Hello, World!'. Call the function.
2. Define a function `add(a, b)` that returns the sum of two numbers.
3. Write a function `multiply(x, y=5)` that multiplies two numbers, with a default value of 5 for `y`.
4. Create a function `is_even(n)` that returns `True` if `n` is even and `False` otherwise.
5. Write a function `reverse_string(s)` that returns the reverse of a given string.
6. Modify the `greet()` function to return 'Hello, World!' instead of printing it.
7. Write a function `calculate_area(length, width)` that returns the area of a rectangle.
8. Define a function `square(n)` that returns the square of a number.
9. Create a function `convert_to_uppercase(text)` that takes a string and returns it in uppercase.
10. Write a function `compare_numbers(a, b)` that returns the larger number.
11. Define a function `describe_person(name, age, city)` and call it using positional arguments.
12. Call `describe_person()` using keyword arguments in different orders.
13. Write a function `power(base, exponent=2)` that returns `base` raised to `exponent`. Call it with both positional and keyword
arguments.
14. Create a function `full_name(first, last, middle='')` that returns the full name. Test with and without the middle name.
15. Write a function `order_food(item, quantity=1, price)` where `quantity` has a default value, but `price` must always be
specified.
16. Define a global variable `counter = 0`. Write a function `increment()` that increases `counter` by 1. Call it twice and print
`counter`.
17. Modify `increment()` to use `global counter` inside the function.
18. Create a function `use_local_variable()` where you define `x = 10` inside it and print `x`.
19. Write a function `modify_global()` that modifies a global variable `status` without using `global`.
20. Define a function `nested_function_demo()` with a nested function inside it that accesses a local variable of the outer
function.
21. Write a lambda function to add two numbers and store it in `add_lambda`. Call it with arguments.
22. Create a lambda function `square_lambda` that squares a given number.
23. Write a lambda function that returns `True` if a number is even, else `False`.
24. Define a lambda function to find the maximum of two numbers.
25. Use a lambda function inside `sorted()` to sort a list of tuples based on the second element.
26. Given a list `numbers = [10, 15, 20, 25, 30]`, use `filter()` to extract even numbers.
27. Use `filter()` to get words from `words = ['apple', 'banana', 'cherry', 'date']` that have more than 5
letters.
28. Use `filter()` with a lambda function to filter out negative numbers from `nums = [-3, -2, -1, 0, 1, 2, 3]`.
29. Given a list of names, filter out names that start with the letter 'A' using `filter()`.
30. Use `filter()` to extract all numbers greater than 50 from a given list.
Answers: