Function WS Solution
Function WS Solution
SCHOOLbbbbCOM
3. What will be the output of the following code?
def JumbleUp(mystr):
L=len(mystr)
str2= ‘ ’
str3 = ‘ ’
for i in range(0, L, 2):
str2=str2+mystr[i+1]+mystr[i]
for ch in str2:
if ch>=‘R’ and ch<=‘U’:
str3+= ‘$’
else:
str3+=ch.lower()
return str3
mystr = “HARMONIOUS”
mystr = JumbleUp(mystr)
print (mystr)
ahm$nooi$$
4. Find and write the output of the following python code:
def display(s):
l = len(s)
m=""
for i in range(0,l):
if s[i].isupper():
m=m+s[i].lower()
elif s[i].isalpha():
m=m+s[i].upper()
elif s[i].isdigit():
m=m+"$"
else:
m=m+"*"
print(m)
display("[email protected]")
exam$$*CBSE*COM
5. Find the write the output of the following code:
def Change(P ,Q=30):
P=P+Q
Q=P-Q
print( P,"#",Q)
return (P)
R=150
S=100
R=Change(R,S)
print(R,"#",S)
S=Change(S)
250 # 150
250 # 100
130 # 100
6. Find and write the output of following python code:
def Alter(M,N=50):
M = M + N
N = M - N
print(M,"@",N)
return M
A=200
B=100
A = Alter(A,B)
print(A,"#",B)
B = Alter(B)
300 @ 200
300 # 100
150 @ 100
7. Find and write the output of following python code:
def Total(Number=10):
Sum=0
for C in range(1,Number+1):
if C%2==0:
continue
Sum+=C
return Sum
print(Total(4))
print(Total(7))
print(Total())
4
16
25
8. Find and write the output of following python code:
X = 100
def Change(P=10, Q=25):
global X
if P%6==0:
X+=100
else:
X+=50
Sum=P+Q+X
print(P,'#',Q,'$',Sum)
Change()
Change(18,50)
Change(30,100)
10 # 25 $ 185
18 # 50 $ 318
30 # 100 $ 480
9. Find and write the output of following python code:
def Func1(A,B):
if A % B == 0:
return 10
else:
return A + Func1(A,B-1)
val = Func1(20,15)
print(val)
110
10.
11. Rewrite the following Python program after removing all the syntactical errors (if any),
underlining each correction:
def checkval:
x = input("Enter a number")
if x % 2 = 0:
print x, "is even"
else if x<0:
print x, "should be positive"
else;
print x, "is odd"
x=int(input("Enter a number:"))
if x % 2 ==0:
print(x, "is even")
elif x<0:
print(x,"should be negative")
else:
print(x,"is odd")
12. What will be the output of the following code:
def Fun1(mylist):
for i in range(len(mylist)):
if mylist[i]%2==0:
mylist[i]/=2
else:
mylist[i]*=2
list1=[21, 20, 6,7, 9, 18, 100, 50, 13]
Fun1(list1)
print(list1)
[42, 10.0 ,3.0, 14, 18, 9.0, 50.0, 25.0, 26]
13. Write a Python function that accepts a string and calculate the number of uppercase letters and
lowercase letters.
Sample String : PythonProgramminG
Expected Output:
Original String : PythonProgramminG
No. of Uppercase characters : 3
No. of Lowercase characters : 14
Str="PythonProgrammingG"
lower=0
upper=0
for i in Str:
if(i.islower()):
lower+=1
else:
upper+=1
print(“Original String:” Str)
print("The number of lowercase characters is:",lower)
print("The number of uppercase characters is:",upper)
14. Write a program that checks whether the given 4 digit number is an Armstrong Number.
Note:An Armstrong number is defined as the sum of nth power of each digit to a n digit number is equal
to that number.
def is_armstrong(number):
num_str = str(number)
num_digits = len(num_str)
sum_of_powers = sum(int(digit) ** num_digits for digit in num_str)
return sum_of_powers == number
num = int(input("Enter a number to check if it's an Armstrong number: "))
if is_armstrong(num):
print(f"{num} is an Armstrong number.")
else:
print(f"{num} is not an Armstrong number.")
15. Which Line Number Code will never execute?
def Check(num): #Line 1
if num%2==0: #Line 2
print("Hello") #Line 3
return True #Line 4
print("Bye") #Line 5
else: #Line 6
return False #Line 7
C = Check(20)
print(C)
Line 5
16.
Data types will not be mentioned. The data type must be included.
They are written in the function
They are written in the function call.
definition.
b.
Local variables can only be accessed within the function or module in which they are defined, in
contrast to global variables, which can be used throughout the entire program.
In Python, Global variable can be defined using global Keyword, also we can make changes to the
variable in local context.
c.
Default arguments – Parameters that have a default value assigned to them in a function definition.
When argument is not passed for that parameter in the function call, then it will use the default value.
Positional Arguments – Parameters that are identified by their position or order in the function
definition and call. That is the arguments passed should be in the same order as the parameters in the
function call.
18. Find the output for the following:
def Position(C1,C2,C3):
C1[0]=C1[0]+2
C2=C2+1
C3="python"
P1=[20]
P2=4
P3="school"
Position(P1,P2,P3)
print(P1, ",", P2, ",", P3)
[22] , 4 , school
19. Rewrite the following codes after removing error. Underline each correction done.
24. Write a function which takes two string arguments and returns the string comparison result of the two
passed strings.
def stringCompare(str1, str2):
if len(str1)!= len(str2):
return False
else:
for i in range (len(str1)):
if len(str1)!= len(str2):
return False
else:
return True
first_string=input("Enter the string:")
second_string=input("Enter the second string:")
if stringCompare(first_string,second_string):
print("Given strings are same")
else:
print("Given strings are different")
25. Write a function called removeFirst that accepts a list as a parameter. It should remove the value at index
0 from the list. Note that it should not return anything (returns None). Note that this function must actually
modify the list passed in, and not just create a second list when the first item is removed. You may assume
the list you are given will have at least one element.
def removeFirst(l):
l.pop(0)
return l
l=[3,4,5,6,8]
l2=print(removeFirst(l))