0% found this document useful (0 votes)
4 views

python pra11

The document contains practical exercises for a Python course, including functions to check if a number is prime, calculate the factorial of a non-negative integer, and count uppercase and lowercase letters in a string. Each exercise includes the function code and prompts for user input. The document is structured for educational purposes within a computer engineering curriculum.

Uploaded by

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

python pra11

The document contains practical exercises for a Python course, including functions to check if a number is prime, calculate the factorial of a non-negative integer, and count uppercase and lowercase letters in a string. Each exercise includes the function code and prompts for user input. The document is structured for educational purposes within a computer engineering curriculum.

Uploaded by

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

Gramin Technical and Management Campus

Department of computer engineering


Subject/code: -Python/22616

Name:-Pranusha Shiddhodhan Birhade

DOP: DOS:-

Practical No 11
1) Write a Python function that takes a number as a parameter
and check the number is prime or not.
Code:-
def is_prime(number):

"""Check if a number is prime."""

if number <= 1:

return False

if number <= 3:

return True

if number % 2 == 0 or number % 3 == 0:

return False

i=5

while i * i <= number:

if number % i == 0 or number % (i + 2) == 0:

return False

i += 6

return True

if __name__ == "__main__":

num = int(input("Enter a number to check if it is prime: "))


if is_prime(num):

print(f"{num} is a prime number.")

else:

print(f"{num} is not a prime number.")

Output:-

2) Write a Python function to calculate the factorial of a number (a


non-negative integer). The function accepts the number as an
argument

Code:-
def factorial(n):

"""Calculate the factorial of a non-negative integer n."""

if n < 0:

return "Factorial is not defined for negative numbers."

elif n == 0:

return 1 # 0! is 1

else:

result = 1

for i in range(1, n + 1):

result *= i

return result
num = int(input("Enter a non-negative integer to calculate its factorial: "))

print(f"The factorial of {num} is {factorial(num)}.")

Output:-

3) Write a Python function that accepts a string and calculate the


number of upper case letters and lower case letters.

Code:-
def count_case_letters(input_string):

"""Count the number of uppercase and lowercase letters in a string."""

upper_case_count = 0

lower_case_count = 0

for char in input_string:

if char.isupper():

upper_case_count += 1

elif char.islower():

lower_case_count += 1

return upper_case_count, lower_case_count

if __name__ == "__main__":

user_input = input("Enter a string: ")

upper_count, lower_count = count_case_letters(user_input)

print(f"Number of uppercase letters: {upper_count}")

print(f"Number of lowercase letters: {lower_count}")


Output:-

You might also like