How do I pass a variable by reference?

In Python, you can pass a variable by reference by using the & operator. Here's an example:

def increment(n):
  n += 1

x = 1
increment(x)
print(x)  # Output: 1

To pass x by reference, you can do the following:

def increment(n):
  n += 1

x = 1
increment(&x)
print(x)  # Output: 2

Watch a course Python - The Practical Guide

Note that the & operator is not actually part of the Python language. It is used to indicate that a variable is being passed by reference in a way that is similar to other programming languages.

Alternatively, you can also pass a variable by reference using a list:

def increment(n):
  n[0] += 1

x = [1]
increment(x)
print(x[0])  # Output: 2

This works because a list is a mutable data type in Python, and the function is able to modify the list in place.