0% found this document useful (0 votes)
16 views11 pages

Functions with questions part 2

Uploaded by

Nirmal Pal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views11 pages

Functions with questions part 2

Uploaded by

Nirmal Pal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

LEGB Rule

Ex: 1
def outer(): 10
a=10 #local
print(a)
outer()

Ex: 2
a=10 is local variable which cannot be accessed outside of the function
and will result in error as given below.
def outer(): NameError: name 'a' is not defined
a=10
print(a)
outer()
print(a)

Ex: 3
def outer(): 10
a=10 #local 100
print(a)
a=100 #global
outer()
print(a)

Ex: 4
def outer(): 100
print(a) #return global value 100
a=100 #global
outer()
print(a)

Ex: 5
def outer(): 10
a=10 #nonlocal 20
print(a) 10
def inner(): 100
a=20 #local
print(a)
inner()
print(a)
a=100 #global
outer()
print(a)

Ex:6

def outer(): 10
a=10 10
print(a) 10
def inner(): 100
print(a)
inner()
print(a)
a=100
outer()
print(a)

Ex:7

def outer(): 100


print(a) 100
def inner(): 100
print(a) 100
inner()
print(a)
a=100 #global
outer()
print(a)

Ex: 8 Accessing builtin value of pi


from math import pi 3.141592653589793
def outer(): 3.141592653589793
print(pi) 3.141592653589793
def inner(): 3.141592653589793
print(pi)
inner()
print(pi)
outer()
print(pi)

Ex: 9

from math import pi 100


def outer(): 200
pi=100 100
print(pi) 200
def inner():
global pi
print(pi)
inner()
print(pi)
pi=200
outer()
print(pi)

Ex: 10

def outer(): 20
a=20 50
print(a) #20 20
def inner(): 50
global a
a=50
print(a) #50
inner()
print(a) #20
a=100
outer()
print(a) #50

Ex: 11

def outer(): 20
a=20 50
print(a) #20 50
def inner(): 100
nonlocal a
a=50
print(a) #50
inner()
print(a) #50 because of nonlocal
a=100
outer()
print(a) #100

Flow of control

Ex: 12

def sum(): sum is 30


a,b=10,20 sub is -100
return a+b
def sub():
a,b=100,200
return a-b
s=sum()
m=sub()
print("sum is ", s)
print("sub is ", m)

Ex: 13

def sub(): sum is -100


a,b=100,200
return a-b
def sum():
return sub()
s=sum()
print("sum is ", s)

Immutable and Mutable Arguments

Ex:14 Immutable Arguments

a=10 10
print(a) 140706674972720
print(id(a)) 30
a+=20 140706674973360
print(a)
print(id(a))

Ex:15 Mutable Arguments

a=[11,22,33,44] [11, 22, 33, 44]


print(a) 1565382159104
print(id(a)) [200, 22, 33, 44]
a[0]=200 1565382159104
print(a)
print(id(a))

Concept of immutable arguments (integer)using Functions

Ex: 16

def display(a): 100


a+=20 Address is 140706674975600
print(a) 120
print("address is ",id(a)) address is 140706674976240
a=100 100
print(a) Address 140706674975600
print("Address is ",id(a))
display(a)
print(a)
print("Address ", id(a))

Ex: 17 Declaring variable a as global


def display(): 100
global a Address is 140706674975600
a+=20 120
print(a) address is 140706674976240
print("address is ",id(a)) 120
a=100 Address 140706674976240
print(a)
print("Address is ",id(a))
display()
print(a)
print("Address ", id(a))

Concept of mutable arguments (integer) using Functions

Ex: 18

def display(a): [11, 22, 33, 44]


a[0]=500 Address is 1565381724608
print(a) [500, 22, 33, 44]
print("address is ",id(a)) address is 1565381724608
a=[11,22,33,44] [500, 22, 33, 44]
print(a) Address 1565381724608
print("Address is ",id(a))
display(a)
print(a)
print("Address ", id(a))

Concept of mutable arguments using Dictionary data type


Ex: 19
def display(a): {'Name': 'Ram', 'marks': 98,
a['marks']=90 'class': 'XII'}
print(a) Address is 1565381726400
print("address is ",id(a)) {'Name': 'Ram', 'marks': 90,
a={'Name':'Ram','marks':98,'class':'XII'} 'class': 'XII'}
print(a) address is 1565381726400
print("Address is ",id(a)) {'Name': 'Ram', 'marks': 90,
display(a) 'class': 'XII'}
print(a) Address 1565381726400
print("Address ", id(a))
Mean
 Definition: The mean is the average of all the numbers in a dataset.
Calculation: Sum up all the numbers and then divide by the count of
numbers.
 Usage in statistics module: statistics.mean(data)
import statistics

data = [10, 20, 30, 40, 50]


mean_value = statistics.mean(data)
print(mean_value) # Output: 30

Mode
 Definition: The mode is the number that appears most frequently in
a dataset.
 Usage in statistics module: statistics.mode(data)
import statistics

data = [10, 20, 20, 30, 40]


mode_value = statistics.mode(data)
print(mode_value) # Output: 20

Median
 Definition: The median is the middle value of a dataset when it is
ordered in ascending or descending order. If there is an even
number of observations, the median is the average of the two middle
numbers.
 Usage in statistics module: statistics.median(data)

import statistics

data = [10, 20, 30, 40, 50]


median_value = statistics.median(data)
print(median_value) # Output: 30

 Explanation: The ordered list [10, 20, 30, 40, 50] has 30 as the middle
value.

For an even number of observations:


import statistics
data = [10, 20, 30, 40]
median_value = statistics.median(data)
print(median_value) # Output: 25
Explanation: The ordered list [10, 20, 30, 40] has the middle values 20
and 30. The median is the average of these two, (20 + 30) / 2 = 25.

Questions

1. Consider the code given below:

b=100
def test(a):
__________ # missing statement
b=b+a
print(a,b)
test(10)
print(b)

Which of the following statements should be given in the blank for


#Missing Statement, if the output produced is 110?

a. global a
b. global b=100
c. global b
d. global a=100

2. The place where a variable can be used is called its


a. area
b. block
c. function
d. Scope

3. What is the output of the following segment?

chr(ord(‘A’))

a. A
b. B
c. a
d. Error

4. Assertion(A): Python Standard Library consists of various modules.


Reasoning(R): A function in a module is used to simplify the code and
avoids repetition.

Ans: Both A and R are true but R is not the correct explanation for A

5. Write a function countNow(PLACES) in Python, that takes the


dictionary, PLACES as an argument and displays the names (in
uppercase)of the places whose names are longer than 5 characters.
For example, consider the following dictionary.
PLACES= {1:"Delhi",2:"London",3:"Paris",4:"New York",5:"Doha"}
The output should be: LONDON NEW YORK

PLACES= {1:"Delhi", 2:"London", 3:"Paris", 4:"New York", 5:"Doha"}


def countNow(PLACES):
for p in PLACES.values():
if len(p)>5:
print(p.upper(),end=" ")
countNow(PLACES)

Output:
LONDON NEW YORK

6. Write a function, lenWords(STRING), that takes a string as an


argument and returns a tuple containing length of each word of a
string. For example, if the string is "Come let us have some fun", the
tuple will have (4, 3, 2, 4, 4, 3)

def lenWords(STRING):
T=()
L=STRING.split()
for word in L:
print(word)
length=len(word)
T=T+ (length,)
return T
st="Come let us have some fun"
result=lenWords(st)
print("Length of each word of a string\n",result)

Output:
Come
let
us
have
some
fun
Length of each word of a string
(4, 3, 2, 4, 4, 3)

7. Write the Python statement for each of the following tasks using
BUILT-IN functions/methods only:
(i) To insert an element 200 at the third position, in the list L1.

L1 = [10, 20, 30, 40]


L1.insert(2, 200)
print(L1)

Output:
[10, 20, 200, 30, 40]

(ii) To check whether a string named, message ends with a full stop /
period or not.

message = "This is a first message."


print(message.endswith('.')) # Output: True

message = "This is second message"


print(message.endswith('.')) # Output: False

8. A list named student Age stores age of students of a class. Write the
Python command to import the required module and (using built-in
function) to display the most common age value from the given list.

import statistics

print( statistics.mode(studentAge) )

You might also like