What does the `global` keyword allow in Python?

Master Python with the PCAP Certification! Explore interactive quizzes and detailed explanations to ensure your exam success. Gain confidence and get certified!

The global keyword in Python is used to allow modifications to a global variable within a function. When a variable is declared as global inside a function, any assignments or changes to that variable affect the variable defined in the global scope rather than creating a new local variable. This means that if you want to read from a global variable in a function and also change its value, you need to declare it as global first.

For example, if you have a global variable x and you want to increase its value within a function, you would declare x as global at the beginning of that function. This allows you to directly access and modify x, ensuring that the changes reflect in the outer scope.

The other options reflect misunderstandings about the global keyword’s purpose and usage.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy