Global Scope

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: What is a function? How is it useful?

A function is a named block in a program which has some certain working. Whenever you call this it performs all its working. It reduces the complexity and reduces the labour of writing same code again and again.

Qus: What is an argument? Give an example of one.

Ans : An argument is the requirement of a function to perform its working. For ex- pow(x,y), here x and y are the arguments which are required to pass to pow() for its working.

Qus. What is a string slice? How is it useful?

Ans: String Slice is a part of a string containing some contiguous characters from the string. It is accessed from the string by providing a range in ―[ ]‖ brackets i.e. S [n:m]. Python returns all the characters at indices n, n+1, n+2 . . . m-1 e.g. 'Barabanki'.[4:7] will return 'ban'.

Qus. Which functions would you chose to use to remove leading and trailing white spaces from a given string?

Ans: Python String strip() function will remove leading and trailing white spaces. If you want to remove only leading or trailing spaces, use lstrip() or rstrip() function instead.

Qus. Suggest appropriate functions for the following tasks –

(a) To check whether the string contains digits.

(b) To find the occurrence a string within another string.

(c) To convert the first letter of a string to upper case.

(d) To convert all the letters of a string to upper case.

(f) To check whether all the letters of the string are in capital letters.

(g) to remove all the white spaces from the beginning of a string.

Ans.  (a) isalnum() (b) find() (c) capitalize()   (d) upper() (f) isupper() (g) lstrip()


Programs

python program input string and prints its characters in different lines two characters per line

View Solution


python to print the number of occurrences of a sub string into a line

View Solution


python program to check the given string is palindrome or not

View Solution


Python program to convert decimal into other number systems

View Solution


python program that inputs a string and then prints it equal to number of times its length

View Solution


Python program to illustrate the global variable

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


CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level NIELIT Study material and Quiz Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Website development Company in Allahabad