In this tutorial, we will learn how to use variables in multiple functions in Python. This involves creating global variables that can be accessed by various functions within your code. By learning this technique, you can streamline your Python code and make it easier to manage and update.
Step 1: Create a Global Variable
A global variable is one that is declared outside any function. It can be accessed and modified within a function, but its scope is not limited to the function. To declare a global variable, simply write the variable in the main body of your code, outside any function definitions. Here’s an example:
1 |
number_of_students = 100 |
Step 2: Accessing Global Variables in Functions
To access a global variable within a function, simply use its name, just like you would do for a local variable.
Example:
1 2 |
def print_students_count(): print("There are", number_of_students, "students.") |
In the example above, the function print_students_count()
is accessing and printing the value of the global variable number_of_students
.
Step 3: Modifying Global Variables in Functions
If you want to modify the value of a global variable within a function, you need to use the global
keyword before the variable name. This informs Python that you’re working with the global variable and not creating a new local variable with the same name.
Example:
1 2 3 |
def add_students(count): global number_of_students number_of_students += count |
In the example above, the function add_students()
takes a parameter count
and adds its value to the global variable number_of_students
to update the total number of students.
Step 4: Using Variables Across Multiple Functions
Now that we know how to create, access, and modify global variables, let’s use them in multiple functions within the same program. Here’s an example that demonstrates the use of the global variable number_of_students
across two functions:
1 2 3 4 5 6 |
def remove_students(count): global number_of_students number_of_students -= count def print_students_count(): print("There are", number_of_students, "students.") |
To put everything together, here’s the complete code example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
number_of_students = 100 def add_students(count): global number_of_students number_of_students += count def remove_students(count): global number_of_students number_of_students -= count def print_students_count(): print("There are", number_of_students, "students.") # Testing the functions print_students_count() add_students(20) print_students_count() remove_students(10) print_students_count() |
Here’s the output of the code above:
There are 100 students. There are 120 students. There are 110 students.
Conclusion
In this tutorial, we learned about global variables in Python and how to use them in multiple functions. We learned how to create, access, and modify global variables within functions using the global
keyword. With these skills, you can now create more efficient and organized Python programs.
Remember, it’s not always encouraged to use global variables as they can make the code harder to understand, debug and maintain. However, in certain situations, global variables can be helpful in making your code simpler and more efficient.