
Python Functions and Data Structures
When you call a function in Python, the order of arguments matters. Positional arguments must come before any keyword arguments. If you place a keyword argument first, Python raises a…

Given the function definition `def areaofcircle(pi=3.14, radius):`, which call will cause an error?
What will be printed by the following code? ```python def fact(n): if n==0: return 1 else: return n*fact(n-1) print(fact(4)) ```
Which of the following lambda expressions correctly adds three numbers?
If `L = [1, 2, 3, 4, 5]`, what is the result of `L[1:4]`?
What does the `set.add(elem)` method do when `elem` is already present in the set?
Consider the dictionary `d = {'a':1, 'b':2}`. What is the result of executing `d['c'] = 3` followed by `d['a'] = 5`?
Which expression correctly computes the sum of all elements in a 2‑D list `matrix`?
What will be the output of the following code? ```python s = " hello world " print(s.strip().title()) ```
Given `A = {1,2,3,4,5}` and `B = {4,5,6,7,8}`, which expression yields the set `{1,2,3}`?
Understanding Python Function Calls and Argument Order
When you call a function in Python, the order of arguments matters. Positional arguments must come before any keyword arguments. If you place a keyword argument first, Python raises a SyntaxError. This rule helps the interpreter match each value to the correct parameter without ambiguity.
- Correct order:
func(1, 2, name="Alice") - Incorrect order:
func(name="Alice", 1, 2)→ SyntaxError
Remember: keywords can't jump the line. Always list positional arguments first, then keywords.
Default Parameters and Their Placement
Python allows default values for parameters, but they must appear after any parameters without defaults. A function defined as def areaofcircle(pi=3.14, radius): is invalid because radius lacks a default value while pi provides one.
Calling areaofcircle() triggers an error because the required radius argument is missing. The interpreter cannot infer a value for a non‑default parameter.
- Valid definition:
def areaofcircle(radius, pi=3.14): - Valid call:
areaofcircle(5)(uses defaultpi)
Key takeaway: Missing required argument = error.
Recursive Functions: The Factorial Example
Recursion is a powerful technique where a function calls itself. The classic factorial function demonstrates this:
def fact(n):
if n == 0:
return 1
else:
return n * fact(n-1)
print(fact(4))
The call fact(4) computes 4 × 3 × 2 × 1 = 24. The base case (n == 0) stops the recursion, returning 1.
Why it works: each recursive step reduces n by one until the base case is reached.
Lambda Expressions: Anonymous Functions
Lambda functions provide a concise way to create small, unnamed functions. To add three numbers, the correct lambda is:
lambda a, b, c: a + b + c
This expression takes three arguments (a, b, c) and returns their sum. Other examples illustrate common mistakes:
lambda a,b: a+b– only two arguments.lambda a,b,c: a-b-c– performs subtraction.lambda a,b,c: a*b*c– multiplies instead of adds.
Think “a plus b plus c”.
List Slicing Basics
Python lists support slicing with the syntax list[start:stop]. The start index is inclusive, while stop is exclusive.
Given L = [1, 2, 3, 4, 5], the slice L[1:4] returns [2, 3, 4]. It starts at index 1 (the second element) and stops before index 4.
- Inclusive start:
1 → 2 - Exclusive end:
4 → stops before 5
Remember: start inclusive, end exclusive.
Working with Sets: Adding Elements
Sets are collections of unique items. The method set.add(elem) inserts elem only if it is not already present. If the element exists, the set remains unchanged—no error is raised.
my_set = {1, 2, 3}
my_set.add(2) # set stays {1, 2, 3}
my_set.add(4) # set becomes {1, 2, 3, 4}
Think “set = no repeats”.
Dictionary Updates and Insertions
Dictionaries map keys to values. Assigning a new key creates an entry; assigning an existing key overwrites its value.
d = {'a': 1, 'b': 2}
# Add a new key
d['c'] = 3 # {'a': 1, 'b': 2, 'c': 3}
# Overwrite an existing key
d['a'] = 5 # {'a': 5, 'b': 2, 'c': 3}
The final dictionary is {'a': 5, 'b': 2, 'c': 3}. No KeyError occurs because assignment creates keys on the fly.
Mnemonic: Add then overwrite key.
Summing Elements in a 2‑D List (Matrix)
To compute the total of all numbers in a matrix, you need to sum each row individually and then sum those results. The idiomatic Python expression is:
total = sum(sum(row) for row in matrix)
This uses a generator expression that iterates over each row, calculates sum(row), and feeds those intermediate sums into the outer sum.
- Incorrect:
sum(matrix)– tries to add lists, causing aTypeError. - Incorrect:
sum(matrix[0]) + sum(matrix[1])– works only for a 2‑row matrix. - Incorrect:
matrix.sum()– lists have nosummethod.
Think of summing rows inside a big bowl to get the final total.
Key Takeaways for Python Functions and Data Structures
- Positional arguments must precede keyword arguments; otherwise you get a SyntaxError.
- Default parameters cannot appear before required ones; missing required arguments raise errors.
- Recursive functions need a clear base case; factorial demonstrates correct recursion.
- Lambda expressions are concise; ensure the correct number of parameters and the intended operation.
- List slicing uses inclusive start and exclusive end indices.
- Sets ignore duplicate additions; they never raise an error for existing elements.
- Dictionary assignment both adds new keys and updates existing ones without raising
KeyError. - Summing a 2‑D list efficiently uses a nested
sumwith a generator expression.
Mastering these concepts will make you more confident when writing clean, bug‑free Python code.
