INTODUCTION TO PYTHON
LAB REPORT # 09
NAME:
Umm E Salma
INSTRUCTOR:
Ma’am : Shakira Musa Baig
BATCH:
CS-SP-21
I’D:
CK-21-110092
DEPARTMENT:
BSCS(5th semester)
DATE:
05/6/2023
Week 9
Labsheet 9
Introduction to PYTHON
OBJECTIVE:
To enhance understanding and proficiency in working with functions in Python, including
parameter passing, mathematical computations, and return statements.
LAB EXERCISES:
1. Write a Python function called `multiply` that takes two numbers as input and returns
their product. Test the function by calling it with different input values.
CODE:
def product_num(num1, num2): #user-defind function
num = (num1 * num2) #calculate product
return num #return value
# take inputs
num1 = 2
num2 = 3
# function call
product = product_num(num1, num2)
# print multiplication value
print("The Product of Number:", product)
OUTPUT:
2. Write a Python function called `is_even` that takes an integer as input and returns `True`
if the number is even and `False` otherwise. Test the function by calling it with different
input values.
CODE:
def is_even(num):
if num %2 == 0:
print(num, 'is even')
else:
print(num, 'is ood')
def main():
is_even(6)
is_even(5)
main()
OUTPUT:
3. Write a Python function called `reverse_string` that takes a string as input and returns
the string in reverse order. Test the function by calling it with different input strings.
CODE:
def reverse_string(name):
reverse = name[::-1]
print('in reverse of', name, 'is:', reverse)
def main():
name = input('Enter any name: ')
reverse_string(name)
name = input('Enter any name: ')
reverse_string(name)
main()
OUTPUT:
4. Write a Python function called `calculate_average` that takes a list of numbers as input
and returns the average of those numbers. Test the function by calling it with different input
lists.
CODE:
def calculate_average(num):
return sum(num) / len(num)
def main():
result1 = calculate_average([5, 8, 3])
print("Result 1:", result1)
result2 = calculate_average([10, 20, 30])
print("Result 2:", result2)
result3 = calculate_average([10.5, 5.5, 4.5])
print("Result 3:", result3)
main()
OUTPUT:
5. Write a Python function called `count_vowels` that takes a string as input and returns
the number of vowels (a, e, i, o, u) in the string. Test the function by calling it with different
input strings.
CODE:
def count_vowels(string):
count = 0
for char in string:
if char.lower() in "aeiou":
count += 1
return count
def main():
name1 = 'SALMA'
result1 = count_vowels(name1)
print("The number of vowels in", name1, "is =", result1)
name2 = 'ALIF ALI'
result2 = count_vowels(name2)
print("The number of vowels in", name2, "is =", result2)
name3 = "SAAID"
result3 = count_vowels(name3)
print("The number of vowels in", name3, "is =", result3)
main()
OUTPUT:
6. Write a Python function called `is_prime` that takes an integer as input and returns `True`
if the number is prime and `False` otherwise. Test the function by calling it with different
input values.
CODE:
def prime(num):
if num <= 1:
return False
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
return False
return True
def main():
number1 = 89
result1 = prime(number1)
print("The number", number1, "is:", result1)
number2 = 45
result2 = prime(number2)
print("The number", number2, "is:", result2)
main()
OUTPUT:
7. Write a Python function called `calculate_factorial` that takes a positive integer as input
and returns its factorial. Test the function by calling it with different input values.
CODE:
def calculate_factorial(number):
factorial = 1
for i in range(1, number + 1):
factorial *= i
return factorial
def main():
number1 = 11
result1 = calculate_factorial(number1)
print("The factorial of", number1, "=", result1)
number2= 14
result2 = calculate_factorial(number2)
print("The factorial of", number2, "=", result2)
number3 = 3
result3 = calculate_factorial(number3)
print("The factorial of", number3, "=", result3)
main()
OUTPUT:
8. Write a Python function called `capitalize_names` that takes a list of names as input and
returns a new list with all the names capitalized. Test the function by calling it with
different input lists.
CODE:
def capitalize_names(name_list):
capitalized_names = []
for name in name_list:
capitalized_names.append(name.capitalize())
return capitalized_names
def main():
result1 = capitalize_names(["umm", "e", "salma"])
print("umm e salma:", result1)
main()
OUTPUT:
9. Write a Python function called `is_palindrome` that takes a string as input and returns
`True` if the string is a palindrome (reads the same forward and backward) and `False`
otherwise. Test the function by calling it with different input strings.
CODE:
def is_palindrome(string):
return string == string[::-1]
def main():
result1 = is_palindrome("racecar")
print("Result 1:", result1)
result2 = is_palindrome("saudia")
print("Result 2:", result2)
main()
OUTPUT:
10. Write a Python function called `calculate_power` that takes two numbers, base and
exponent, as input and returns the result of base raised to the power of exponent. Test the
function by calling it with different input values.
CODE:
def calculate_power(base, exponent):
return base ** exponent
def main():
result1 = calculate_power(6, 11)
print("Result 1:", result1)
result2 = calculate_power(33, 0)
print("Result 2:", result2)
result3 = calculate_power(71, -2)
print("Result 3:", result3)
main()
OUTPUT:
11. (Sum the digits in an integer) Write a function that computes the sum of the digits in
an integer. Use the following function header:
def sumDigits(n):
For example, sumDigits(2+3+4) returns 9. (Hint: Use the % operator to extract digits, and
the // operator to remove the extracted digit. For instance, to extract 4 from 234, use 234
% 10 To remove 4 from 234, use 234 // 10 Use a loop to repeatedly extract and remove the
digits until all the digits are extracted.) Write a test program that prompts the user to enter
an integer and displays the sum of all its digits.
CODE:
def sumDigits(n):
total = 0
while n != 0:
digit = n % 10
total += digit
n //= 10
return total
def main():
result1 = sumDigits(234)
print("Result 1:", result1)
result2 = sumDigits(12345)
print("Result 2:", result2)
result3 = sumDigits(987654321)
print("Result 3:", result3)
main()
OUTPUT:
12. (Display an integer reversed) Write the following function to display an integer in
reverse order:
def reverse(number):
For example, reverse(3456) displays 6543. Write a test program that prompts the user to
enter an integer and displays its reversal.
CODE:
def reverse(number):
return int(str(number)[::-1])
def main():
number1 = 20020218
result1 = reverse(number1)
print("The reverse of", number1, "is:", result1)
number2 = 312312
result2 = reverse(number2)
print("The reverse of", number2, "is:", result2)
number3 = 987432
result3 = reverse(number3)
print("The reverse of", number3, "is:", result3)
main()
OUTPUT:
13. (Sort three numbers) Write the following function to display three numbers in
increasing order:
def displaySortedNumbers(num1, num2, num3):
Write a test program that prompts the user to enter three numbers and invokes the
function to display them in increasing order.
CODE:
def displaySortedNumbers(num1, num2, num3):
sorted_numbers = sorted([num1, num2, num3])
return sorted_numbers
def main():
result1 = displaySortedNumbers(10, 3, 6)
print("Result 1:", result1)
result2 = displaySortedNumbers(15, 33, 99)
print("Result 2:", result2)
result3 = displaySortedNumbers(5, 5, 3)
print("Result 3:", result3)
main()
OUTPUT: