0% found this document useful (0 votes)
11 views44 pages

Function (1) ...... Bruh

The document contains a series of questions and answers related to Python functions, including their definitions, syntax, and examples of usage. It covers topics such as the use of keywords, function outputs, error identification in code, and the differences between parameters and arguments. Additionally, it includes programming exercises and homework assignments focused on function implementation and problem-solving in Python.

Uploaded by

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

Function (1) ...... Bruh

The document contains a series of questions and answers related to Python functions, including their definitions, syntax, and examples of usage. It covers topics such as the use of keywords, function outputs, error identification in code, and the differences between parameters and arguments. Additionally, it includes programming exercises and homework assignments focused on function implementation and problem-solving in Python.

Uploaded by

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

QUESTION ANSWER SESSION

Which of the following is the use of function in


python?

a) Functions are reusable pieces of programs

b) Functions don’t provide better modularity for your


application

c) We can’t also create our own functions

d) All of the mentioned


Answer: a

Explanation: Functions are reusable pieces of


programs. They allow us to give a name to a block of
statements, allowing us to run that block using the
specified name anywhere in our program and any
number of times.
Which keyword is used for function?

a) Fun
b) Define
c) Def
d) Function
e) None
Answer: None

Explanation: The keyword used for function is “def”.


What will be the output of the following Python
code?
def printMax(a, b):
if a > b:
print(a, 'is maximum')
elif a == b:
print(a, 'is equal to', b)
else:
print(b, 'is maximum')
printMax(3, 4)

a) 3
b) 4
c) 4 is maximum
d) None of the mentioned
Answer: c

Explanation: Here, we define a function called printMax that


uses two parameters called a and b. We find out the greater
number using a simple if..else statement and then print the
bigger number.
What will be the output of the following Python
code?

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?

def say(message, times = 1):


print(message * times)
say('Hello')
say('World', 5)
Answer:

Hello
WorldWorldWorldWorldWorld
What will be the output of the following Python
code?

def func(a, b=5, c=10):


print('a is', a, 'and b is', b, 'and c is', c)

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?

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)
Answer:

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)

n = input (Enter the number")


Factors (n)
Answer:

def factors(n):
for i in range(2,n):
if n%i == 0: # indent
print (i, “is a factor of”,n)

n = int( input (“Enter the number”) )


factors (n)
Find the errors in the following Python code?

def generatesq (n) :


for i in range 100, n :
print (math, sqrt (i))

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?

def volcylinder (r, h=5)


return (pi*r*r*h)

volcylinder (h=7, 3)
print (x)
Answer:

from math import pi

def volcylinder (r, h=5):


return (pi*r*r*h)

x = volcylinder (3, h=7)


print (x)
What is the difference between parameters and
arguments?
Answer:

Parameters are values provided in function header.


E.g. def area(r): # r is the parameter

Arguments are the values provided in function call.


E.g. def area(r):
......
x = 50
area (x) # x is the argument
What are the advantages of keyword arguments?
Answer:
 It is easier to use since we need not remember the order
of the arguments.

 We can specify the values for only those parameters


which we want, and others have default values.
PROGRAMS
Write a function to print the following pattern where number of lines is a default argument.
Enter The No of Line
5

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()

line=int(input("Enter number of lines"))


print("When passing user Input as argument default arguments are not used")
pattern(line)
print("When not passing user Input as argument default arguments are used")
pattern()
OUTPUT
Enter number of lines 7
When passing user Input as argument default arguments are not used
1
01
010
1010
10101
010101
0101010

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)

number=int(input("Enter decimal number"))


base=int(input("Enter the base of the number where the number to be
converted"))
convert(number,base)
print()
convert(number)
OUTPUT

Enter decimal number 30


Enter the base of the number where the number to be converted 8
36
11110
Program to print Pronic numbers between 1 to 100
using function.

A number is said to be pronic number if it is a


product of two consecutive numbers.
For examples:
6=2x3
72 = 8 x 9
Solution:

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

Combination (nCr) can be defined as the


combination of n things taken r at a time without
any repetition.

ncr can be calculated as,


nCr = n! / [r! * (n - r)!]
Solution:

def fact(z):
f=1
if z == 0:
return f;
else:
for i in range(1,z+1):
f = f * i;
return f;

n = int(input("Enter the value of n: ")) :8


r = int(input("Enter the value of r: "))
nCr = fact(n) / (fact(r) * fact(n - r))
print("\nnCr = ", nCr)
Program to find the duplicate words in a string

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

2) To determine whether a given number is a twisted prime number using function.


A number is called a twisted prime number if it is a prime number and reverse of this number
is also a prime number.
Examples: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79

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]

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy