0% found this document useful (0 votes)
27 views5 pages

Lab 7

Uploaded by

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

Lab 7

Uploaded by

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

Programming Fundamentals (SWE-102) SSUET/QR/114

LAB # 07

FUNCTIONS
OBJECTIVE
Create a python function using different argument types.

EXERCISE:

A. Point out the errors, if any, and paste the output also in the following Python programs.
1. Code:
define sub(x, y)
return x + y

Correct Code: Output:


def sub(x,y):
return x + y
def main():
print(sub(2,3))
main()

2. Code:
define describe_pet(pet_name, animal_type='dog'):
print("\nI have a " , animal_type ,".")
print("My " , animal_type + "'s name is " , pet_name + ".")

Correct Code: Output:


def describe_pet(pet_name, animal_type='dog'):
print("\nI have a " , animal_type ,".")
print("My " , animal_type + "'s name is " , pet_name
+".")

describe_pet(pet_name='Jack' )

2023F-BSE-088
Programming Fundamentals (SWE-102) SSUET/QR/114

3. Code:

def type_of_int(i):
if i // 2 == 0:
return 'even'
else:
return 'odd'

Correct Code: Output:

def type_of_int(i):
if i % 2 == 0:
return 'even'
else:
return 'odd'
def main():
i = int(input("Enter a number: "))
print(type_of_int(i))
main()

B. What will be the output of the following programs:

1. Code: Output:

def test(a):
def add(b):
a =+ 1
return a+b
return add
func = test(4)
print(func(4))

2. Code: Output:

def return_none():
return
print(return_none())

2023F-BSE-088
Programming Fundamentals (SWE-102) SSUET/QR/114

3. Code:

def test_range(n):
if n in range(3,9):
print( n,"is in the range")
else :
print("The number is outside the given range.")
test_range(7)
test_range(10)
test_range(4)

Output

C. Write Python programs for the following:

1. Write a function called favorite_book() that accepts one parameter, title. The
function should print a message, such as One of my favorite books is Alice in
Wonderland. Call the function, making sure to include a book title as an argument in
the function call.

Code:

def Favourite_book(title):
print("One of my favourite book is",title)

def main():
Favourite_book("Alice in Wonderland")
main()

Output

2023F-BSE-088
Programming Fundamentals (SWE-102) SSUET/QR/114

2. Write a function called max( ), that returns the maxium of three integer numbers.

Code:

def max_number(int1,int2,int3):
return max(int1,int2,int3)

def main():
int1 = eval(input("Enter a no::"))
int2 = eval(input("Enter a no::"))
int3 = eval(input("Enter a no::"))
maximum = max_number(int1,int2,int3)
print("The Maximum of",int1,",",int2,"and",int3,"is",maximum)
main()
Output

3. Write a Python program to find GCD of two numbers.

Code: Output:

def gcd(x,y):
while y:
x,y = y, x % y
return x

def main():
no1 = int(input("Enter the first number: "))
no2 = int(input("Enter the second number: "))
result = gcd(no1,no2)
print("The GCD of ",no1,"and",no2,"is",result)
main ()

2023F-BSE-088
Programming Fundamentals (SWE-102) SSUET/QR/114

4. Write a function called describe_city() that accepts the name of a city and its country.
The function should print a simple sentence, such as Reykjavik is in Iceland. Give the
parameter for the country a default value. Call your function for three different cities, at
least one of which is not in the default country.

Code: Output:

def describe_city(city, country='USA'):


print(city,"is in",country,"\b.")

def main():
describe_city('Reykjavik', 'Iceland')
describe_city('Paris', 'France')
describe_city('New York')
main()

2023F-BSE-088

You might also like