Function (1) ...... Bruh
Function (1) ...... Bruh
a) Fun
b) Define
c) Def
d) Function
e) None
Answer: None
a) 3
b) 4
c) 4 is maximum
d) None of the mentioned
Answer: c
x = 50
def func(x):
print('x is', x)
x=2
print('Changed local x to', x)
func(x)
print('x is now', x)
Answer:
x is 50
Changed local x to 2
x is now 50
What will be the output of the following Python
code?
x = 50
def func():
global x
print('x is', x)
x=2
print('Changed global x to', x)
func()
print('Value of x is', x)
Answer:
x is 50
Changed global x to 2
Value of x is 2
What will be the output of the following Python
code?
Hello
WorldWorldWorldWorldWorld
What will be the output of the following Python
code?
func(3, 7)
func(25, c = 24)
func(c = 50, a = 100)
Answer:
a is 3 and b is 7 and c is 10
a is 25 and b is 5 and c is 24
a is 100 and b is 5 and c is 50
What will be the output of the following Python
code?
def fun(s):
k=len(s)
m=" "
for i in range(0,k):
if(s[i].isupper()):
m=m+s[i].lower()
elif s[i].isalpha():
m=m+s[i].upper()
else:
m=m+’**'
print(m)
fun(‘YoungMinds@google.com’)
Answer:
yOUNGmINDS**GOOGLE**COM
What will be the output of the following Python
code?
250 # 150
250 # 100
130 # 100
What will be the output of the following Python
code?
a = 10
def call():
global a
a = 15
b = 20
print(a)
call ()
Answer:
15
Find the errors in the following Python code?
Def factors( )
for i in range(2,n):
if n%i == 0:
print (i, “is a factor of”,n)
def factors(n):
for i in range(2,n):
if n%i == 0: # indent
print (i, “is a factor of”,n)
generatesq (“150”)
Answer:
import math
def generatesq (n) :
for i in range (100, n) :
print (math.sqrt (i))
generatesq (150)
Find the errors in the following Python code?
volcylinder (h=7, 3)
print (x)
Answer:
1
01
010
1010
10101
def pattern(n=4):
k=1
for i in range(n):
for j in range(i+1):
print(k%2,end=" ")
k=k+1
print()
When not passing user Input as argument default arguments are used
1
01
010
1010
Write a program to covert decimal number to any other
number by using function and default arguments.
def convert(d,b=2):
s=""
while d>0:
s=str(d%b)+s
d=d//b
print(s,end='')
return(0)
def pronic_no():
for i in range (1, 101):
x = i * (i+1)
if (x <= 101):
print (x, end=" ")
print ("Pronic numbers between 1 and 100: ")
pronic_no()
Program to print the combination (nCr) of the given
number
def fact(z):
f=1
if z == 0:
return f;
else:
for i in range(1,z+1):
f = f * i;
return f;
Input:
string =
"big black bug bit a big black dog on his big black nose"
Output:
Duplicate words in a given string:
big black
Solution:
def display(string):
#Converts the string into lowercase
string = string.lower();
#Split the string into words using built-in function
words = string.split(" ");
print("Duplicate words in a given string : ");
for i in range(0, len(words)):
count = 1;
for j in range(i+1, len(words)):
if(words[i] == (words[j])):
count = count + 1;
#Set words[j] to 0 to avoid printing visited word
words[j] = "0"
#Displays the duplicate word if count is greater than 1
if(count > 1 and words[i] != "0"):
print(words[i]);
string = "big black bug bit a big black dog on his big black nose"
display (string)
HOME WORK
1) To input number of lines and a special character and display the following pattern using
function.
Input: 4, *
Output:
12344321
123**321
12****21
1******1
3) To input two sets of numbers and display union or intersection of these two sets using
function based on user’s choice.
Example:
Set1 = [1, 3, 5, 7, 8]
Set2 = [2, 3, 4, 5]
a) Union
b) Intersection
Enter your choice: b
[3, 5]