Python globals Function with Examples

The globals function in Python provides a dictionary that represents the current global symbol table. A symbol table is characterized as a data structure that holds all essential information regarding the program. This encompasses variable names, methods, classes, and more.

Python globals Function Syntax

It has the following syntax:

Example

globals()

Parameters

It does not contain any parameters.

Return

It provides a dictionary representation of the existing global symbol table.

Python globals Function Example

The following example illustrates how to alter a global variable by utilizing the globals function in Python.

Example

age = 22

globals()['age'] = 22

print('The age is:', age)

Output:

Output

The age is: 22

Explanation:

In the example provided, we designate a variable named (age) as a global variable, and this global variable is altered by utilizing the globals function.

Input Required

This code uses input(). Please provide values below: