0% found this document useful (0 votes)
4 views7 pages

Python Programming Exercise

Uploaded by

shantha.merlion
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
4 views7 pages

Python Programming Exercise

Uploaded by

shantha.merlion
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 7

Python Programming Exercise

Introduction

This document presents a collection of Python programming challenges designed to


test your understanding of various programming concepts. Each challenge is
accompanied by a clear problem statement and a well-structured solution.

Challenges

1. Maximum of Three Numbers


2. Sum of Numbers in a List
3. Product of Numbers in a List
4. String Reversal
5. Factorial Calculation
6. Number Range Check
7. Uppercase and Lowercase Letter Counting
8. Unique Elements in a List
9. Prime Number Check
10. Even Numbers in a List
11. Perfect Number Check
12. Palindrome Check
13. Pascal's Triangle
14. Pangram Check
15. Hyphen-Separated Word Sorting

Note: The provided solutions are examples. There might be other efficient or
alternative ways to solve these problems.

1. Write a Python function to find the Max of three numbers.

def maxed(self):
print(max(self))

listed = [12,66,4]
maxed(listed)

2. Write a Python function to sum all the numbers in a list.


Sample List : (8, 2, 3, 0, 7)
Expected Output : 20

def sumed(self):
added = sum(self)
return added
SampleList= (8, 2, 3, 0, 7, 1)
print(sumed(SampleList))

3. Write a Python function to multiply all the numbers in a list.


Sample List : (8, 2, 3, -1, 7)
Expected Output : -336

def multiplication(self):
mul = 1
for i in self:
mul *= i
return mul

SampleList = (8, 2, 3, -1, 7)


print(multiplication(SampleList))

4. Write a Python program to reverse a string.


Sample String : "1234abcd"
Expected Output : "dcba4321"

SampleString = "1234abcd"

print(SampleString[::-1])

5. 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):
if n < 0:
raise ValueError("Factorial is not defined for negative numbers.")
elif n == 0:
return 1
else:
result = 1
for i in range(1, n + 1):
result *= i
return result

number = 5
factorial_result = factorial(number)
print("The factorial of", number, "is", factorial_result)

6. Write a Python function to check whether a number falls in a given range.

def range_check(self):
for i in self:
if i in range(1,11):
print(f"{i}, is in my range")

sample = [1,355,3,222]
range_check(sample)

7. Write a Python function that accepts a string and calculate the number of upper
case letters and lower case letters.
Sample String : 'The quick Brow Fox'
Expected Output :
No. of Upper case characters : 3
No. of Lower case Characters : 12

def alpha_count(self):
upper_count = 0
lower_count = 0

for char in self:


if char.isupper():
upper_count += 1
elif char.islower():
lower_count += 1
print("No. of Upper case characters : ", upper_count)
print("No. of Lower case characters : ", lower_count)

String = 'The quick Brow Fox'


alpha_count(String)

8. Write a Python function that takes a list and returns a new list with unique elements
of the first list.
Sample List : [1,2,3,3,3,3,4,5]
Unique List : [1, 2, 3, 4, 5]

def unique_list(self):
seted = set(self)
return seted

SampleList = [1,2,3,3,3,3,4,5]
print(unique_list(SampleList))

9. Write a Python function that takes a number as a parameter and check the number
is prime or not.
Note : A prime number (or a prime) is a natural number greater than 1 and that has no
positive divisors other than 1 and itself.

def is_prime(number):
if number <= 1:
return False
for i in range(2, int(number ** 0.5) + 1):
if number % i == 0:
return False
return True

num = int(input("Enter a number : "))


if is_prime(num):
print(f"{num} is a prime number.")
else:
print(f"{num} is not a prime number.")

10. Write a Python program to print the even numbers from a given list.
Sample List : [1, 2, 3, 4, 5, 6, 7, 8, 9]
Expected Result : [2, 4, 6, 8]

def even_check(self):
even_list = []
for i in self:
if i % 2 == 0:
even_list.append(i)
return even_list

Sample_List = [1, 2, 3, 4, 5, 6, 7, 8, 9, 12]


print(even_check(Sample_List))

11. Write a Python function to check whether a number is perfect or not.


According to Wikipedia : In number theory, a perfect number is a positive integer that
is equal to the sum of its proper positive divisors, that is, the sum of its positive
divisors excluding the number itself (also known as its aliquot sum). Equivalently, a
perfect number is a number that is half the sum of all of its positive divisors
(including itself).
Example : The first perfect number is 6, because 1, 2, and 3 are its proper positive divisors,
and 1 + 2 + 3 = 6. Equivalently, the number 6 is equal to half the sum of all its positive
divisors: ( 1 + 2 + 3 + 6 ) / 2 = 6. The next perfect number is 28 = 1 + 2 + 4 + 7 + 14. This is
followed by the perfect numbers 496 and 8128.

def is_perfect_number(num):
if num <= 1:
return False

divisors_sum = 0
for divisor in range(1, num):
if num % divisor == 0:
divisors_sum += divisor

return divisors_sum == num

# Example usage:
number = int(input("Enter a number: "))
if is_perfect_number(number):
print(f"{number} is a perfect number.")
else:
print(f"{number} is not a perfect number.")

12. Write a Python function that checks whether a passed string is palindrome or not.
Note: A palindrome is a word, phrase, or sequence that reads the same backward as
forward, e.g., madam or nurses run.

def check_palindrome(self):
backword = self[::-1]

if backword == self:
print(f"Your word {self}, is a palindrome string")
else:
print(f"Your word {self}, isn't a palindrome string")

word = "madam"
check_palindrome(word)
13. Write a Python function that prints out the first n rows of Pascal's triangle.
Note : Pascal's triangle is an arithmetic and geometric figure first imagined by Blaise
Pascal.

def print_pascal_triangle(n):

triangle = []
for i in range(n):
row = [1]
if i > 0:
previous_row = triangle[i - 1]
for j in range(1, i):
row.append(previous_row[j - 1] + previous_row[j])
row.append(1)
triangle.append(row)

for row in triangle:


print(" ".join(map(str, row)))

n = int(input("Enter the number of rows: "))


print_pascal_triangle(n)

14. Write a Python function to check whether a string is a pangram or not.


Note : Pangrams are words or sentences containing every letter of the alphabet at
least once.
For example : "The quick brown fox jumps over the lazy dog"

import string

def is_pangram(sentence):

sentence = sentence.lower().replace(' ', '')

alphabet = set(string.ascii_lowercase)

return set(sentence) >= alphabet

sentence = "The quick brown fox jumps over the lazy dog"
if is_pangram(sentence):
print("The sentence is a pangram.")
else:
print("The sentence is not a pangram.")

15. Write a Python program that accepts a hyphen-separated sequence of words as


input and prints the words in a hyphen-separated sequence after sorting them
alphabetically.
Sample Items : green-red-yellow-black-white
Expected Result : black-green-red-white-yellow

def sort_hyphenated_words(words):

word_list = words.split('-')

sorted_list = sorted(word_list)

sorted_words = '-'.join(sorted_list)

return sorted_words

words = "green-red-yellow-black-white"
sorted_words = sort_hyphenated_words(words)
print(sorted_words)

You might also like