Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Working With Global Variables in Python Functions (Summary)

You’ve learned that you can access global variables directly in your functions. However, to modify a global variable in a function, you must use either the global keyword or the globals() function.

Global variables allow you to share data across multiple functions, which can be useful in some situations. However, you should use this type of variable carefully and sparingly to avoid writing code that’s difficult to understand, debug, test, and maintain.

In this video course, you’ve learned how to:

  • Work with global variables in Python
  • Directly access global variables within your Python functions
  • Modify and create global variables within functions using the global keyword
  • Access, create, and modify global variables within your functions with the globals() function
  • Use strategies to avoid using global variables in Python code

Now you know what it takes to correctly use global variables inside functions in Python, so you can use them effectively where appropriate. You also know that avoiding global variables is the best way to go most of the time. This will allow you to write more modular, maintainable, and reusable code.

Download

Sample Code (.zip)

1.9 KB
Download

Course Slides (.pdf)

4.6 MB

Become a Member to join the conversation.