0% found this document useful (0 votes)
1 views2 pages

Python Answers Detailed

Uploaded by

udhay0325
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)
1 views2 pages

Python Answers Detailed

Uploaded by

udhay0325
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

1. Function: Definition: A function is a reusable block of code that performs a specific task.

Syntax:
def function_name(parameters): return value Use: It helps in modular programming and avoids
repetition. Example: def add(a, b): return a + b

2. Types of Arguments: Definition: Arguments are values passed to a function. Types: Positional,
Keyword, Default, Variable-length (*args, **kwargs). Syntax: def func(a, b=5): return a + b Example:
func(3) or func(a=3, b=4)

3. Negative Indexing: Definition: Access elements from the end using negative indices. Syntax:
list[-1], list[-2] Use: Helps to access last elements easily. Example: l = [10, 20, 30] print(l[-1]) # 30

4. Need for Functions: Definition: Functions divide large programs into smaller modules. Use:
Improves readability, reusability, and debugging. Syntax: def func(): pass Example: Separate logic
into functions for clarity.

5. Add Two Lists: Definition: Combine corresponding elements of lists. Syntax: result = [l1[i] + l2[i]
for i in range(len(l1))] Example: l1=[1,2]; l2=[3,4] result=[l1[i]+l2[i] for i in range(len(l1))]

6. Area of Circle: Definition: Calculate area using function. Formula: πr² Syntax: def area(r): return
3.14 * r * r Example: area(5)

7. Tuple as Return: Definition: Function can return multiple values using tuple. Syntax: def func():
return (a, b) Example: def fun(): return (1,2) x,y = fun()

8. Fruitful Function: Definition: A function that returns a value. Syntax: def func(): return value Use:
Used when output is needed. Example: def square(x): return x*x

9. Negative Indexing Example: Definition: Access list from end. Syntax: list[-index] Example:
l=[5,6,7] print(l[-2]) # 6

10. List Methods: Definition: Built-in functions for list operations. Methods: append(), remove(),
sort(), pop(), extend() Syntax: [Link](10) Example: l=[1,2]; [Link](3)

11. Built-in Functions: Definition: Predefined functions in Python. Examples: len(), type(), sum()
Syntax: len(list) Example: len([1,2,3])

12. Function Without Argument: Definition: Function with no parameters but returns value. Syntax:
def func(): return value Example: def greet(): return "Hello"

13. Local vs Global Variables: Definition: Local inside function, global outside. Syntax: global x Use:
Scope control. Example: x=10 def f(): global x

14. Tuple as Argument: Definition: Tuple passed as parameter. Syntax: def func(t): return sum(t)
Example: func((1,2,3))

15. List Concatenation: Definition: Joining two lists. Syntax: l3 = l1 + l2 Example: [1,2] + [3,4]

16. List vs Tuple: Definition: Both are collections. Difference: List is mutable, tuple is immutable.
Syntax: list = [] tuple = () Example: l[0]=5 (allowed), t[0]=5 (error)

17. Python List: Definition: Ordered, mutable collection. Syntax: l = [elements] Example: l=[1,2,3]

18. Function Syntax: Definition: Basic structure of function. Syntax: def name(): statements
Example: def show(): print("Hi")

19. Sorting: Definition: Arranging elements in order. Syntax: [Link]() Example: l=[3,1,2] [Link]()
20. Add Two Lists (map): Definition: Add lists using map function. Syntax: map(function, list1, list2)
Example: list(map(lambda x,y:x+y,[1,2],[3,4]))

You might also like