Functions — Python 练习题
A function is a named, reusable block of code. You define it once with def and call it by name as many times as you need. Functions keep programs organized and eliminate repeated code.
Parameters are the variable names listed in the definition — def greet(name): has one parameter, name. Arguments are the actual values passed in a call — greet('Ada') passes the argument 'Ada'. The argument is copied into the parameter when the function runs.
The return statement sends a value back to the caller and ends the function immediately. A function without a return statement gives back None. Printing inside a function is not the same as returning — print shows a value on screen, while return hands a value back for further computation.
Scope controls where variables are visible. A variable created inside a function is local to that function and disappears when it ends; a variable created outside is global to the script. Assigning to a variable inside a function creates a new local variable rather than changing the global one.
Tracing function calls means following the argument values into the parameters, executing the body, and substituting the returned value back at the call site — essential for predicting program output.
练习的技能
- Defining functions with def
- Distinguishing parameters from arguments
- Using return values in expressions
- Tracing nested function calls
- Predicting local vs global variable behavior
- Knowing when a function returns None
练习题:Functions
说明: 仔细解答每一道题。清晰地展示所有解题过程。在指定位置或按指示在另一张纸上写出最终答案。
-
1.Write a Python function named is_even that takes one integer parameter n and returns True if n is even and False otherwise.
Functions — 练习题(续)
-
2.What is the output of the following code? Show your reasoning. def mystery(x, y): result = x * 2 + y return result print(mystery(3, 5))
Functions — 练习题(续)
-
3.Consider the following function definition: def greet(name, greeting="Hello"): return greeting + ", " + name + "!" What will be the output of each of the following function calls? a. greet("Alice") b. greet("Bob", "Hi") c. greet(greeting="Hey", name="Carol")
Functions — 练习题(续)
-
4.Write a function named calculate_average that takes a list of numbers as a parameter and returns the average (mean) of those numbers. You may assume the list is not empty.
Functions — 练习题(续)
-
5.What is the output of the following code? Explain what happens with the variable x. def modify(x): x = x + 10 return x x = 5 modify(x) print(x)
Functions — 练习题(续)
-
6.Which of the following is the correct way to define a function in Python that takes two parameters and returns their sum?
- A. def add(a, b): return a + b
- B. function add(a, b) { return a + b; }
- C. def add(a, b): print(a + b)
- D. def add(a, b) return a + b
Functions — 练习题(续)
-
7.Write a function named count_vowels that takes a string as a parameter and returns the number of vowels (a, e, i, o, u) in the string. Your function should work for both uppercase and lowercase letters.
Functions — 练习题(续)
-
8.What is the output of the following code? def outer(x): def inner(y): return y * 2 return inner(x) + 1 print(outer(5))
Functions — 练习题(续)
-
9.Write a function named find_max that takes a list of numbers as a parameter and returns the largest number in the list. Do not use the built-in max() function.
Functions — 练习题(续)
-
10.Consider the following function: def process(data, multiplier=2, offset=0): result = [] for item in data: result.append(item * multiplier + offset) return result What will be the output of each of the following function calls? a. process([1, 2, 3]) b. process([1, 2, 3], 3) c. process([1, 2, 3], offset=5) d. process([1, 2, 3], 3, 1)
参考答案
-
1.
最终答案: def is_even(n): return n
-
2.
The function mystery computes , which is then printed.最终答案: 11
-
3.
最终答案: a. "Hello, Alice!" b. "Hi, Bob!" c. "Hey, Carol!"
-
4.
最终答案: def calculate_average(numbers): total = 0 for num in numbers: total += num return total / len(numbers)
参考答案(续)
-
5.
The variable x inside the function is a local copy. Modifying it inside the function does not affect the global x, so when print(x) is called, it still prints the original value of 5.最终答案: 5
-
6.
最终答案: a
-
7.
最终答案: def count_vowels(s): vowels = "aeiouAEIOU" count = 0 for char in s: if char in vowels: count += 1 return count
-
8.
The inner function inner doubles its argument (), and then the outer function adds 1, giving .最终答案: 11
参考答案(续)
-
9.
最终答案: def find_max(numbers): largest = numbers[0] for num in numbers: if num > largest: largest = num return largest
-
10.
最终答案: a. [2, 4, 6] (multiplier=2, offset=0) b. [3, 6, 9] (multiplier=3, offset=0) c. [7, 9, 11] (multiplier=2, offset=5) d. [4, 7, 10] (multiplier=3, offset=1)
需要避免的常见错误
- Confusing print with return
- Calling a function before it is defined
- Expecting a local variable to exist outside its function
- Passing the wrong number of arguments
- Forgetting that a function without return gives None
生成您自己的练习表
想要一套难度自选的 functions 新题?使用 WorksheetSmith 在几秒内编译一份可打印的自定义 PDF。
最后更新:2026