1. Write a Python program to find the sum of all even numbers in a given list.
Program:
def sum_even_numbers(lst):
return sum(num for num in lst if num % 2 == 0)
# Test the function
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
result = sum_even_numbers(numbers)
print(result)
2. Write a Python function to check if a given number is prime.
Program:
def is_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
# Test the function
number = int(input("Enter a number: "))
if is_prime(number):
print(number, "is a prime number.")
else:
print(number, "is not a prime number.")
3. Write a Python program that finds and prints the largest and smallest elements in a given list.
Program:
def find_min_max(lst):
min_num = min(lst)
max_num = max(lst)
return min_num, max_num
# Test the function
numbers = [10, 5, 7, 12, 3, 9, 15]
min_value, max_value = find_min_max(numbers)
print("Minimum value:", min_value)
print("Maximum value:", max_value)
4. Write a Python program to count the number of vowels in a given string.
Program:
def count_vowels(text):
vowels = 'aeiouAEIOU'
count = 0
for char in text:
if char in vowels:
count += 1
return count
# Test the function
input_string = input("Enter a string: ")
num_vowels = count_vowels(input_string)
print("Number of vowels:", num_vowels)
5. Write a Python program to reverse a given string.
Program:
def reverse_string(text):
return text[::-1]
# Test the function
input_string = input("Enter a string: ")
reversed_string = reverse_string(input_string)
print("Reversed string:", reversed_string)
6. Write a Python program to find the second largest number in a given list.
Program:
def find_second_largest(lst):
lst.sort()
return lst[-2]
# Test the function
numbers = [10, 5, 7, 12, 3, 9, 15]
second_largest = find_second_largest(numbers)
print("Second largest number:", second_largest)
7. Write a Python program that checks if two strings are anagrams (contain the same characters in a different order).
Program:
def are_anagrams(str1, str2):
return sorted(str1.lower()) == sorted(str2.lower())
# Test the function
string1 = input("Enter the first string: ")
string2 = input("Enter the second string: ")
if are_anagrams(string1, string2):
print("The strings are anagrams.")
else:
print("The strings are not anagrams.")
8. Write a Python program to calculate the average of numbers in a given list.
Program:
def calculate_average(lst):
return sum(lst) / len(lst)
# Test the function
numbers = [10, 5, 7, 12, 3, 9, 15]
average = calculate_average(numbers)
print("Average:", average)
9. Write a Python program to find the factorial of a given number using a loop.
Program:
def factorial(n):
result = 1
for i in range(1, n + 1):
result *= i
return result
# Test the function
num = int(input("Enter a number: "))
print("Factorial of", num, "is", factorial(num))
10: Write a Python program to check if a given string is a pangram (contains all the letters of the alphabet).
Program
import string
def is_pangram(text):
text = text.lower()
alphabet = set(string.ascii_lowercase)
return set(text) >= alphabet
# Test the function
input_string = input("Enter a string: ")
if is_pangram(input_string):
print("The string is a pangram.")
else:
print("The string is not a pangram.")
No comments:
Post a Comment