Global Scope
A variable, with global scope can be used anywhere in the program. It can be created by defining a variable outside the scope of any function/block.
Rules of global Keyword
The basic rules for global keyword in Python are:
- When we define a variable outside a function, it’s global by default. You don’t have to use global keyword.
- We use global keyword to read and write a global variable inside a function.
- Use of global keyword outside a function has no effect
Use of global Keyword
Example : Accessing global Variable From Inside a Function
c = 1 # global variable
def add():
print(c)
add()
Output:
1
Example : Accessing global Variable From Inside a Function
c = 1 # global variable
def add():
print(c)
add()
Output:
1
Example : Modifying Global Variable From Inside the Function
c = 1 # global variable
def add():
c = c + 2 # increment c by 2
print(c)
add()
Output:
Unbound Local Error: local variable 'c' referenced before assignment
Example : Changing Global Variable From Inside a Function using global keyword
x = 0 # global variable
def add():
global x
x = x + 5 # increment by 2
print ("Inside add() function x value is :", x)
add()
print ("In main x value is :", x)
Output:
Inside add() function x value is : 5
In main x value is : 5
In the above program, x is defined as a global variable. Inside the add() function, global keyword is used for x and we increment the variable x by 5. Now We can see the change on the global variable x outside the function i.e the value of x is 5.
Qus. 1 : Which one of the following is incorrect?
- The variables used inside function are called local variables.
- The local variables of a particular function can be used inside other functions, but these cannot be used in global space.
- The variables used outside function are called global variables.
- In order to change the value of global variable inside function, keyword global is used.
Qus. 2 : <p>What will be the output?</p><pre><span style="font-size: 14px;">x = 10</span><span style="font-size: 14px;"><br></span><span style="font-size: 14px;">def fun():<br></span><span style="font-size: 14px;"> global x<br></span><span style="font-size: 14px;"> x = 20</span><span style="font-size: 14px;"><br></span><span style="font-size: 14px;">fun()<br></span><span style="font-size: 14px;">print(x)</span></pre>
- 10
- 20
- Error
- None
Qus. 3 : A variable declared outside all functions is known as:
- Local Variable
- Static Variable
- Global Variable
- Instance Variable
Programs
Python program to convert decimal into other number systems
View Solution
python program that generates six random numbers in a sequence created with start stop step
View Solution
python program to create a function to calculate factorial value
View Solution
Python program to find the sum of factorial upto n terms using function
View Solution
python program to create a function for reverse the number
View Solution
Python program to check number is Palindrome or not using function
View Solution
Python program to create a function for calculating sum of digit
View Solution
Python program to create recursive function to find factorial
View Solution
python function to accept 2 number and return addition subtraction multiplication and division
View Solution
python function to accept number of days and return week and days to its equivalent
View Solution
python function to extract the day month year from given date
View Solution
Python function to find the sum of all numbers between 100 and 500 which are divisible by 2
View Solution
Python function to get two matrices and multiply them
View Solution
python recursive function to find the sum of digits
View Solution
python recursive function to print the reverse of number
View Solution
Write a function with name dividebyfive which generate and prints a random integer number from the range 0 to 100 and then return True if the randomly generated number is divisible by 5, and False otherwise.
View Solution
python Function to Compute the Sum of n Terms of the Following Series
View Solution
Write a Program to calculate factorial of a number using recursion.
View Solution