Python Programming Exercise
Python Programming Exercise
Introduction
Challenges
Note: The provided solutions are examples. There might be other efficient or
alternative ways to solve these problems.
def maxed(self):
print(max(self))
listed = [12,66,4]
maxed(listed)
def sumed(self):
added = sum(self)
return added
SampleList= (8, 2, 3, 0, 7, 1)
print(sumed(SampleList))
def multiplication(self):
mul = 1
for i in self:
mul *= i
return mul
SampleString = "1234abcd"
print(SampleString[::-1])
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)
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
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
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
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
# 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)
import string
def is_pangram(sentence):
alphabet = set(string.ascii_lowercase)
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.")
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)