0% found this document useful (0 votes)
2 views6 pages

Python Level 3

The document contains multiple Python programs that perform various tasks, including checking vowel counts in strings, capitalizing words, counting words with a prefix, checking for digits, summing odd and even numbers, finding the longest word, filtering even elements from a list, merging sorted lists, replacing characters in strings, calculating Fibonacci numbers, generating tuples, removing duplicates, counting substring occurrences, and checking for palindromes.

Uploaded by

madumitha1616
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)
2 views6 pages

Python Level 3

The document contains multiple Python programs that perform various tasks, including checking vowel counts in strings, capitalizing words, counting words with a prefix, checking for digits, summing odd and even numbers, finding the longest word, filtering even elements from a list, merging sorted lists, replacing characters in strings, calculating Fibonacci numbers, generating tuples, removing duplicates, counting substring occurrences, and checking for palindromes.

Uploaded by

madumitha1616
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

Write a Python program that takes a word as input.

If its length is
odd, print "Invalid". If even, split it into two equal halves and
check if both halves have the same number of vowels. If they do,
print "They have even vowel count", otherwise print "They do not have
even vowel count

def check_even_vowel_count(word):
vowels = "AEIOUaeiou"

if len(word) % 2 != 0:
print("Invalid")
return

mid = len(word) // 2
first_half, second_half = word[:mid], word[mid:]

count_vowels = lambda s: sum(1 for char in s if char in vowels)

if count_vowels(first_half) == count_vowels(second_half):
print("They have even vowel count")
else:
print("They do not have even vowel count")

word = "helo"
check_even_vowel_count(word)

Write a Python program that takes n lines of input and capitalizes


the first letter of each word in every line. Print the modified lines.

def capitalize_first_letter():
n = int(input("Enter number of lines: "))
lines = [input() for _ in range(n)]

for line in lines:


print([Link]())

# Example usage
capitalize_first_letter()
Count Words with a Given Prefix in a Sentence input : pay attend the
class at periodic time (sentence) at(prefix) output : 2"

def count_words_with_prefix(sentence, prefix):


words = [Link]()
count = sum(1 for word in words if [Link](prefix))
return count

# Example usage
sentence = input("Enter a sentence: ")
prefix = input("Enter a prefix: ")
print(count_words_with_prefix(sentence, prefix))

find the digit input 1:123hii output :no input2: 123456 output :yes

def contains_only_digits(s):
if [Link]():
return "yes"
else:
return "no"

# Test cases
input1 = input()
print("Output:", contains_only_digits(input1)) # Output: no

program that takes input from the user, calculates the sum of odd and
even numbers from the input, and prints the result as a tuple

# Get input from the user as a list of numbers


numbers = list(map(int, input("Enter numbers separated by space:
").split()))

# Calculate the sum of odd and even numbers


odd_sum = sum(num for num in numbers if num % 2 != 0)
even_sum = sum(num for num in numbers if num % 2 == 0)

# Print the result as a tuple


print(tuple((odd_sum, even_sum)))
program to find the longest word in a given string

def longest(sen):
words=[Link]()
longe=max(words,key=len)
return longe

sen=input()
print(longest(sen))

print only even elements of a list, separated by spaces, without


brackets

n=list(map(int,input().split()))

print(*[num for num in n if num%2==0])

Python script that takes multiple sets of inputs, merges them into a
single sorted list, and calculates the sum of the elements:

def merge():
n=int(input())
merges=[]

for i in range(n):
num=int(input())
elements=[int(input()) for t in range(num)]
[Link](elements)

[Link]()
total=sum(merges)

print(merges)
print(total)

merge()
Replacing the characters in the string with other characters or
symbols

def replace_chars(text, replacements):

for old_char, new_char in [Link]():


text = [Link](old_char, new_char)
return text

# Example usage:
original_text = "Hello, World!"
replacements = {'H': '#', 'e': '3', 'o': '0', 'l': '1', 'd': '$'}
new_text = replace_chars(original_text, replacements)
print(new_text)

Nth Fibonacci numbers

def fibonacci(n):
if n <= 0:
return "Invalid Input"
elif n == 1:
return 0
elif n == 2:
return 1
else:
return fibonacci(n - 1) + fibonacci(n - 2)

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


print(f"The {n}th Fibonacci number is: {fibonacci(n)}")
Fibonacci numbers within range

def fibo(start,end):
fibono=[]
a,b=0,1
while a<=end:
if a>=start:
[Link](a)
a,b=b,a+b
return fibono

a=int(input())
b=int(input())

print(fibo(a,b))

python code
input: 2 5(range)
output:[(2,4),(3,9),(4,16),(5,25)]

def gen_tuples(s,e):
return [(i,i**2) for i in range(s,e+1)]

s=int(input())
e=int(input())
print(gen_tuples(s,e))

Remove consecutive repeated elements


Input: occurrence Output: ocurence

def dup(s):
if not s:
return "no dup"
dupli=False
result=[s[0]]
for i in range(1,len(s)):
if s[i]!=s[i-1]:
[Link](s[i])
else:
dupli=True
out="".join(result)
return out if dupli else "NO dup"

s=input()
print(dup(s))

Count substring occurrences in a string

def count_substring_occurrences(string, substring):

return [Link](substring)

# Example usage
main_string = "hello hello world hello"
sub_string = "hello"
count = count_substring_occurrences(main_string, sub_string)
print(f'The substring "{sub_string}" appears {count} times.')

check if a given string is a palindrome:

def is_palindrome(s):
s = [Link]().replace(" ", "") # Convert to lowercase and
remove spaces
return s == s[::-1]

# Example usage
word = input("Enter a word: ")
if is_palindrome(word):
print("It's a palindrome!")
else:
print("It's not a palindrome.")

You might also like