Programming with ‘Python’ Practical: 11 Lab Manual (Solved)
Sr. Remark if
Name of Resource Broad Specification Quantity
No. any
1 Computer System Intel i3, 4 GB RAM
For All
2 Operating System Windows/Linux 20
Experiments
3 Development Software Python IDE And VS Code
1. What is the output of the following program?
def myfunc(text, num):
while num > 0:
print(text)
num = num - 1
myfunc('Hello', 4)
Output:
Hello
Hello
Hello
Hello
Explanation:
• The function myfunc() takes two arguments: text and num.
• A while loop runs as long as num > 0.
• Inside the loop, text is printed and num is decremented by 1 in each iteration.
• Since num = 4, the loop runs 4 times, printing "Hello" each time.
2. What is the output of the following program?
num = 1
def func():
num = 3
print(num)
Jamia Polytechnic Akkalkuwa Page No:01 Prepared by: Sayyed Waliullah
Programming with ‘Python’ Practical: 11 Lab Manual (Solved)
func()
print(num)
Output:
3
1
Explanation:
• num = 1 is a global variable.
• Inside func(), a new local variable num = 3 is created.
• When func() is called, it prints 3 (local num).
• After calling func(), print(num) outside the function refers to the global num, which is
still 1.
1. Write a Python function that takes a number as a parameter and check the number is
prime or not.
def is_prime(n):
if n < 2:
return False # Numbers less than 2 are not prime
for i in range(2, int(n ** 0.5) + 1): # Check divisibility up to √n
if n % i == 0:
return False
return True
# Example usage
num = int(input("Enter a number: "))
if is_prime(num):
print(num, "is a prime number")
else:
print(num, "is not a prime number")
2. Write a Python function to calculate the factorial of a number (a non-negative integer).
The function accepts the number as an argument.
def factorial(n):
fact = 1
for i in range(1, n + 1):
fact *= i # Multiply numbers from 1 to n
Jamia Polytechnic Akkalkuwa Page No:02 Prepared by: Sayyed Waliullah
Programming with ‘Python’ Practical: 11 Lab Manual (Solved)
return fact
# Example usage
num = int(input("Enter a number: "))
if num < 0:
print("Factorial is not defined for negative numbers.")
else:
print("Factorial of", num, "is", factorial(num))
[Link] a Python function that accepts a string and calculate the number of upper case
letters and lower case letters.
def count_case(s):
upper = sum(1 for char in s if [Link]())
lower = sum(1 for char in s if [Link]())
print("Uppercase letters:", upper)
print("Lowercase letters:", lower)
# Example usage
text = "Hello World"
count_case(text)
Jamia Polytechnic Akkalkuwa Page No:03 Prepared by: Sayyed Waliullah