Set 1: Strings in Python
1. Count the number of vowels in a given string
string = input("Enter a string: ")
vowels = "aeiouAEIOU"
count = 0
for ch in string:
if ch in vowels:
count += 1
print("Number of vowels:", count)
2. Check if a given string is a palindrome
string = input("Enter a string: ")
if string == string[::-1]:
print("Palindrome")
else:
print("Not a palindrome")
3. Find the number of occurrences of each character in a string
string = input("Enter a string: ")
frequency = {}
for ch in string:
frequency[ch] = [Link](ch, 0) + 1
print(frequency)
4. Convert a string into uppercase without using upper()
string = input("Enter a string: ")
result = ""
for ch in string:
if 'a' <= ch <= 'z':
result += chr(ord(ch) - 32)
else:
result += ch
print("Uppercase:", result)
5. Remove all spaces from a given string
string = input("Enter a string: ")
no_space = [Link](" ", "")
print("Without spaces:", no_space)
6. Reverse the words in a sentence
sentence = input("Enter a sentence: ")
words = [Link]()
reversed_sentence = " ".join(words[::-1])
print("Reversed sentence:", reversed_sentence)
7. Replace all vowels in a string with '*'
string = input("Enter a string: ")
vowels = "aeiouAEIOU"
for v in vowels:
string = [Link](v, "*")
print("Modified string:", string)
8. Find the longest word in a sentence
sentence = input("Enter a sentence: ")
words = [Link]()
longest = max(words, key=len)
print("Longest word:", longest)
9. Count the number of words in a paragraph
paragraph = input("Enter a paragraph: ")
words = [Link]()
print("Number of words:", len(words))
10. Check if two strings are anagrams
str1 = input("Enter first string: ")
str2 = input("Enter second string: ")
if sorted([Link]()) == sorted([Link]()):
print("Strings are anagrams")
else:
print("Strings are not anagrams")
Set 2: Functions in Python
1. Function that returns the sum of two numbers
def add(a, b):
return a + b
print(add(5, 10))
2. Factorial using recursion
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n-1)
print(factorial(5))
3. Largest number in a list
def largest_number(lst):
return max(lst)
print(largest_number([3, 7, 1, 9, 5]))
4. Check whether a number is prime
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n**0.5)+1):
if n % i == 0:
return False
return True
print(is_prime(17))
5. Convert Celsius to Fahrenheit
def c_to_f(celsius):
return (celsius * 9/5) + 32
print(c_to_f(37))
6. Count uppercase and lowercase letters
def count_case(s):
upper = sum(1 for c in s if [Link]())
lower = sum(1 for c in s if [Link]())
return upper, lower
print(count_case("Hello World"))
7. Remove duplicates from a list
def unique_list(lst):
return list(set(lst))
print(unique_list([1, 2, 2, 3, 4, 4, 5]))
8. nth Fibonacci number using recursion
def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)
print(fibonacci(6))
9. Reverse a string without slicing
def reverse_string(s):
result = ""
for ch in s:
result = ch + result
return result
print(reverse_string("Python"))
10. Check whether a number is even or odd
def even_odd(num):
if num % 2 == 0:
return "Even"
else:
return "Odd"
print(even_odd(7))