Local variables

Local variables


A variable with local scope can be accessed only within the function/block that it is created in. When a variable is created inside the function/block, the variable becomes local to it. A local variable only exists while the function is executing.

Example

X=50

def test ( ):

y = 20

print "Value of x is ‟, X, „; y is ‟ , y

print "Value of x is ‟, X, „ y is „ , y

On executing the code we will get

Value of x is 50; y is 20

The next print statement will produce an error, because the variable y is not accessible outside the function body.

A global variable remains global, till it is not recreated inside the function/block.

Example

x=50

def test ( ):

x=5

y=2

print "Value of x & y inside the function are " , x , y

print "Value of x outside the function is " , x

This code will produce following output:

Value of x & y inside the function are 5 2

Value of x outside the function is 50

If we want to refer to global variable inside the function then keyword global will be prefixed with it.

Example

x=50

def test ( ):

global x

x =2

y = 2

print „Value of x & y inside the function are „ , x , y

print „Value of x outside function is „ , x

This code will produce following output:

Value of x & y inside the function are 2 2

Value of x outside the function is 2

Qus. 1 : <p>What is the output of the following code?</p><pre><span style="font-size: 14px;">x = 50<br></span><span style="font-size: 14px;">def func (x):<br></span><span style="font-size: 14px;">&nbsp; &nbsp; x = 2<br></span><span style="font-size: 14px;">func (x)<br></span><span style="font-size: 14px;">print ('x is now', x)</span></pre>

  1. x is now 50
  2. x is now 2
  3. x is now 100
  4. Error
Qus. 2 : <p>What is the value of the following Python code?</p><pre>&gt;&gt;&gt;print(36 / 4)</pre>

  1. 9
  2. 4
  3. 9.0
  4. 4.0
Qus. 3 : <p>What is the output of following Python code?</p><pre>&gt;&gt;print(5*(2//3))</pre><p><br></p>

  1. 3
  2. 3.3
  3. 0
  4. error
Qus. 4 : Given a string x="hello" What is the output of x.count('l')?

  1. 2
  2. 1
  3. 0
  4. none
Qus. 5 : Which of the following functions is a built-in function in pythonn

  1. factorial()
  2. print()
  3. seed()
  4. sqrt()
Qus. 6 : <p>What will be the output of following?</p><pre><span style="font-size: 14px;">Y=[2,5J,6]<br></span><span style="font-size: 14px;">Y.sort()</span></pre>

  1. [2,6,5J]
  2. [5J,2,6]
  3. Error
  4. [6,5J,2]
Qus. 7 : <p>What is the output of the following?</p><pre>x=123<br>for i in x:<br>&nbsp; &nbsp; print(i)</pre><div><br></div>

  1. 1 2 3
  2. 123
  3. Error
  4. none of these
Qus. 8 : Debugging is the process of fixing a______in the software.

  1. procedure
  2. function
  3. bug
  4. None of these

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 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


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


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