Learn › Class 12 › Computer Science / Informatics Practices › Python revision, functions, exceptions
Functions in Python
Example 1: A function with a default parameter
Open one step at a time. Try the next step yourself first.
- Step 1. def power(x, n=2): return x ** n
Show step 2
power(3) uses n = 2 and returns 9.
Show step 3
power(2, n=5) returns 2 ** 5 = 32.
Example 2: Local and global x
Open one step at a time. Try the next step yourself first.
- Step 1. x = 5 at the top level.
Show step 2
def f(): x = 10 creates a new local x inside f.
Show step 3
After calling f(), print(x) still shows 5, because the global x was never changed.