Lesson 2
Modifying Variables with Functions in Python
Lesson Introduction

Welcome to the lesson on modifying variables with functions in Python! Understanding how functions can modify variables is crucial for complex programs. By the end of this lesson, you will comprehend how passing variables to functions can affect your code, specifically how lists and other mutable types can be modified.

Modifying Lists

In Python, data types are categorized into mutable and immutable types. This is important when passing variables to functions. Mutable types include lists, dictionaries, and sets, while immutable types include integers, strings, and tuples.

It is important to understand how copying works in Python. Take a look at this example:

Python
1# List is a mutable type 2a = [1, 2, 3] 3b = a 4 5print("Original a:", a) # Original a: [1, 2, 3] 6print("Original b:", b) # Original b: [1, 2, 3]

The a variable stores a reference to the list. When a is copied to b, the list is not copied – only the reference is. Both a and b reference the same list in this example. Modifying a will also modify b and vice versa:

Python
1# List is a mutable type 2a = [1, 2, 3] 3b = a 4 5print("Original a:", a) # Original a: [1, 2, 3] 6print("Original b:", b) # Original b: [1, 2, 3] 7 8a[0] = 4 9 10print(a) # [4, 2, 3] 11print(b) # [4, 2, 3]

Here, we only modified a, but as b is the reference to the same list, it is also modified.

Example of Modifying a List with a Function

It is most important when working with functions. Python's mutable and immutable types show different behaviors when passed to functions.

For mutable types like lists, the reference to the original data is passed. Changes made to the parameter in the function reflect in the original variable.

For immutable types like integers, only a copy of the value is passed to the function. Changes made do not affect the original variable.

Let's use the cyclic_shift function to see how we can modify lists within a function.

Python
1def cyclic_shift(x): 2 x.insert(0, x.pop(-1)) 3 4a = [1, 2, 3] 5cyclic_shift(a) 6print(a) # [3, 1, 2]

Since lists are mutable, x references the same list as a does, the cyclic_shift function modifies the original a list directly. This applies to dictionaries and sets as well, as they are mutable types.

Pass-By-Reference vs. Pass-By-Value

Now, consider this increment function example:

Python
1def increment(x): 2 x += 1 3 4a = 5 5increment(a) 6print(a) # 5

The value of a remains 5 because increment only changes the local copy of x. x and a are different variables, and modifying x won't affect a.

Implications of Modifying Variables within Functions

Understanding whether a parameter is passed by reference or value is crucial to avoid unintended side effects. Modifying mutable types within a function can lead to bugs if not anticipated.

If the function should not alter the original data, consider copying the data:

Python
1def safe_cyclic_shift(x): 2 r = x.copy() 3 r.insert(0, r.pop(-1)) 4 return r 5 6a = [1, 2, 3] 7new_a = safe_cyclic_shift(a) 8print("Original a:", a) # Original a: [1, 2, 3] 9print("New a:", new_a) # New a: [3, 1, 2]

The copy method creates a copy of the list stored via the x reference. This ensures the function will not modify the original x directly.

Lesson Summary

In this lesson, we learned:

  • The difference between mutable and immutable types in Python.
  • How passing mutable types to functions can modify the original data.
  • The difference between pass-by-reference (for mutable types) and pass-by-value (for immutable types).
  • The importance of avoiding unintended side effects when modifying variables within functions.

Now it's time to apply what you’ve learned. You’ll be given hands-on exercises to practice modifying variables with functions, ensuring you understand and can implement these concepts effectively. Good luck!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.