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 will be the output of the following Python code?</p><pre><span style="font-size: 14px;">def foo():<br></span><span style="font-size: 14px;"> return total + 1<br></span><span style="font-size: 14px;">total = 0<br></span><span style="font-size: 14px;">print(foo())</span></pre>
- 0
- 1
- error
- none of the mentioned
Qus. 2 : <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;"> 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>
- x is now 50
- x is now 2
- x is now 100
- Error
Qus. 3 : <p>What is the value of the following Python code?</p><pre>>>>print(36 / 4)</pre>
- 9
- 4
- 9.0
- 4.0
Qus. 4 : Given a string x="hello" What is the output of x.count('l')?
- 2
- 1
- 0
- none
Qus. 5 : Which of the following functions is a built-in function in pythonn
- factorial()
- print()
- seed()
- 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>
- [2,6,5J]
- [5J,2,6]
- Error
- [6,5J,2]
Qus. 7 : <p>What is the output of the following?</p><pre>x=123<br>for i in x:<br> print(i)</pre><div><br></div>
- 1 2 3
- 123
- Error
- none of these
Qus. 8 : Debugging is the process of fixing a______in the software.
- procedure
- function
- bug
- 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 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