0% found this document useful (0 votes)
8 views6 pages

Python Functions Assignment Questions

The document contains a series of Python code snippets along with instructions to output the results of each snippet. It includes various function definitions and calls, demonstrating concepts such as default arguments, global variables, and recursion. Additionally, there are assignments related to coding questions from specified pages.

Uploaded by

charu18yadav
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)
8 views6 pages

Python Functions Assignment Questions

The document contains a series of Python code snippets along with instructions to output the results of each snippet. It includes various function definitions and calls, demonstrating concepts such as default arguments, global variables, and recursion. Additionally, there are assignments related to coding questions from specified pages.

Uploaded by

charu18yadav
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

📝 Assignment: Output-Based Questions (Chapter 2 : Functions)

Instructions: Write the output of the following Python code snippets. Assume no syntax errors
unless otherwise mentioned. Answer all questions.

Q1.
def multiply(x, y):
return x * y

result = multiply(3, 4)
print(result)

Q2.

def greet(name):
print("Hello", name)

x = greet("Asha")
print(x)

Q3.
def message(text="Welcome"):
print(text)

message()
message("Hello!")

Q4.

def student(name, grade):


print(name, "is in grade", grade)

student("Rahul", 12)
student(grade=11, name="Riya")
Q5.
def report(x, y):
print("Status:", x, ", Level:", y)

report("Active", 3)
report(y=5, x="Inactive")​


Q6.​
def info(a, b):
print(a, "-->", b)

info("Xavier", 9)
info(b="Ten", a="Zoe")

Q7.
x = 10

def change():
x = 20
print("Inside function:", x)

change()
print("Outside function:", x)


Q8. Default arguments in action

def info(name, city="Delhi"):


print(name, "lives in", city)

info("Anjali")
info("Rohan", "Mumbai")

Q9.
def greet(name="Guest", message="Welcome"):
print(message, name)
greet()​
greet("Aman")
greet("Neha", "Hello")
Q10.

def describe(person="Someone", age=18):


print(person, "is", age, "years old")

describe()
describe("Rita")
describe(age=25, person="Anil")

Q11.
def power(base, exponent=2):
return base ** exponent

print(power(3))
print(power(2, 5))

Q12.
def calc(x, y):
return x + y, x - y

a, b = calc(8, 3)
print(a)
print(b)​

Q13.
def calc(x, y):
return x + y, x - y

a = calc(8, 3)
print(a)
Q14. ​
def square(n):
return n * n

def sum_of_squares(x, y):


return square(x) + square(y)

print(sum_of_squares(2, 3))

Q15.
def fact(n):
if n == 0:
return 1
else:
return n * fact(n-1)

print(fact(4))

Q16.

def add_item(item, my_list=[]):


my_list.append(item)
return my_list

print(add_item(1))
print(add_item(2))

Q17.
def show():
print("CBSE Class 12 Python")

x = show()
print(type(x))
Q18.
x = 5
def change():
global x
x = x * 10 + 2
print(x)
x = 9
change()

Q19.
def combine(s):
s = s + "ly"
return s * 3

result = combine("love")
print(result)

Q20.​
def funstr(s):
T = ""
for i in s:
if [Link]():
T = T+i
return T
x = "Python 3.9"
result = funstr(x)
print(x, result, sep = "*")

Q21.​
V=25
def fun(ch):
V = 50
print(V,end=ch)
print(V,end=”*”)
fun(“!”)
print(V)
Q21.
S= "Wow Hi Madam“
L=[Link]()
for W in L:
x=[Link]()
if x==x[::-1]:
for i in x:
print(i,end="*")
else:
for i in x:
print(i,end="#")
print()

Assignment: Coding Questions (Chapter 2 : Functions)


●​ Solve all the write a program questions on the pages mentioned below
●​ Practice solved coding questions pg 2.41 to 2.60
●​ Unsolved questions- pg 2.60 to 2.64

Common questions

Powered by AI

Global variables allow cross-function access by using a common reference point. As shown with 'x = 5' and function 'change()', declaring 'global x' permits direct manipulation of 'x', making 'x = x * 10 + 2' executable, updating the global 'x' to 52 . This can lead to code that's harder to debug due to potential side-effects since any function with 'global' can change the same variable, risking unintended modifications. It highlights the importance of careful global variable usage for encapsulation integrity.

Recursion involves a function calling itself with modified arguments until a base condition stops further calls. In 'fact(n)', it calculates factorials recursively. For 'fact(4)', '4 * fact(3)' is evaluated repeatedly until 'fact(0)', which returns 1, culminating in '4 * 3 * 2 * 1', resulting in 24 . The recursion ends with the base case 'n == 0' which ensures return of 1, facilitating backward calculation.

In Python, a function like 'power(base, exponent=2)' returns a single value representing base^exponent. 'print(power(3))' gives 9. Whereas 'calc(x, y)', returning a tuple '(x + y, x - y)', allows multiple values to be unpacked simultaneously. 'a, b = calc(8, 3)' unpacks results to 'a = 11' and 'b = 5', illustrating tuple unpacking's utility for returning multiple outputs from a single function call . This differentiation encourages concise, efficient value handling.

In Python, a variable defined outside a function is global unless declared otherwise within a function. In 'change()', defining 'x = 20' creates a local variable separate from the global 'x=10'. Therefore, 'Inside function: 20' is printed. If 'global x' is used as in a different snippet, 'x = x * 10 + 2' modifies the global 'x', resulting in '52'. Outside 'change()', the initial 'x' value remains unchanged at '10' unless explicitly updated by 'global' .

Keyword arguments allow explicit pairing of argument names with values, enhancing readability and allowing out-of-order declaration. In 'student(grade=11, name="Riya")', named parameters are used, enabling the call to be made without concern for order. Similarly, 'report(y=5, x="Inactive")' and 'info(b="Ten", a="Zoe")' use keyword arguments to ensure clarity and specificity in assigning values, contrasting positional arguments which require adherence to parameter order .

For input 'Python 3.9', 'funstr(s)' iterates through each character in the string, appending those that are digits to 'T'. Consequently, '39' is created by concatenating numeric characters '3' and '9' . This illustrates the use of loop iteration and conditional checks (e.g., 'i.isdigit()') for selective string reconstruction—a typical string manipulation strategy in Python programming.

Immutable data types like integers or strings don't change after assignment, creating new objects for any modifications, whereas mutable types like lists allow in-place modification. In 'x = 10', integers are immutable. If changed inside 'change()', the new 'x = 20' refers to a distinct object locally. Conversely, function 'add_item(item, my_list=[])' modifies 'my_list' directly through 'append', demonstrating how mutable types persist modifications across calls unless explicitly recreated . This impacts how data is shared and managed in memory during execution.

The document employs 'split' to separate 'S = "Wow Hi Madam"' into a list of words ['Wow', 'Hi', 'Madam']. Loop control via 'for W in L:' iterates over this list. The 'if' conditional checks revese equivalence to determine structure, either '#'-separated or '*'-separated 'upper' representations. This manipulation showcases Python's powerful string handling, employing logical checks and transformations for structured data processing, including palindrome checks and string traversal .

Default arguments allow a function to initialize parameters with default values if no arguments are provided. For example, in 'info(name, city="Delhi")', calling 'info("Anjali")' results in 'Anjali lives in Delhi' because 'city' defaults to 'Delhi' . Similarly, 'greet()' results in 'Welcome Guest' because both 'name' and 'message' have default values. In 'describe(person="Someone", age=18)', calling 'describe()' yields 'Someone is 18 years old' for the same reason .

Using mutable default arguments like lists can lead to unexpected behavior because the default list is shared among all calls to the function. For 'add_item(item, my_list=[])', the list 'my_list' accumulates items across different function calls. For instance, calling 'add_item(1)' then 'add_item(2)' results in the same list being returned with elements [1, 2], not individual lists [1] and [2] as might be expected . This can cause bugs if one assumes a fresh list each time.

You might also like