
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How do I declare a global variable in Python class?
A global variable is a variable with global scope, meaning that it is visible and accessible throughout the program, unless shadowed. The set of all global variables is known as the global environment or global scope of the program.
We declare a variable global by using the keyword global before a variable. All variables have the scope of the block, where they are declared and defined in. They can only be used after the point of their declaration.
Example
Example of global variable declaration
def f(): global s print(s) s = "Only in spring, but Miami is great as well!" print(s) s = "I am looking for a course in New York!" f() print(s)
Output
I am looking for a course in New York! Only in spring, but Miami is great as well! Only in spring, but Miami is great as well!
- Related Articles
- How to declare a global variable in Python?
- How to declare a global variable in C++
- How to declare a global variable in PHP?
- How do I declare global variables on Android?
- How do we declare variable in Python?
- How do I declare global variables on Android using Kotlin?
- How to declare a variable in Python?
- How do I declare a namespace in JavaScript?
- How do I share global variables across modules in Python?
- How to use a global variable in a Python function?
- How do I check if a Python variable exists?
- How do I call a Variable from Another Function in Python?
- How do I assign a dictionary value to a variable in Python?
- How to declare global Variables in JavaScript?
- How to declare global variables in Android?

Advertisements