0% found this document useful (0 votes)
5 views3 pages

Practice Programs HY

Uploaded by

Neha Makhija
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)
5 views3 pages

Practice Programs HY

Uploaded by

Neha Makhija
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
You are on page 1/ 3

Practice Programs: Python Class 9 Half Yearly Examination 2024

1. Reverse a Given String


s = "hello"
reversed_string = s[::-1]
print(reversed_string) # Output: "olleh"

2. Find the Largest Element in a List


lst = [1, 2, 3, 4, 5]
largest_element = max(lst)
print(largest_element) # Output: 5

3. Count the Number of Vowels in a Given String


s = "hello world"
vowels = "aeiouAEIOU"
count = 0
for char in s:
if char in vowels:
count += 1
print(count) # Output: 3

4. Check if a Given Number is Prime or Not


n = 29
is_prime = True
if n > 1:
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
is_prime = False
break
else:
is_prime = False
print(is_prime) # Output: True

5. Generate the Fibonacci Sequence Up to n Terms


n = 10
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a + b
# Output: 0 1 1 2 3 5 8 13 21 34

6. Calculate the Sum of All Elements in a List


lst = [1, 2, 3, 4, 5]
sum_of_elements = sum(lst)
print(sum_of_elements) # Output: 15

7. Find the Factorial of a Given Number n


n = 5
factorial = 1
for i in range(1, n + 1):
factorial *= i
print(factorial) # Output: 120

8. Find the GCD (Greatest Common Divisor) of Two Numbers


a = 48
b = 18
while b:
a, b = b, a % b
print(a) # Output: 6
9. Convert a Decimal Number to Binary
n = 10
binary = bin(n)[2:]
print(binary) # Output: "1010"

10. Find the Sum of Digits of a Given Number


n = 1234
sum_of_digits = 0
while n > 0:
sum_of_digits += n % 10
n //= 10
print(sum_of_digits) # Output: 10

11. Find the Length of a Given String


s = "hello"
length_of_string = len(s)
print(length_of_string) # Output: 5

12. Check if a Given Year is a Leap Year or Not


year = 2024
is_leap_year = (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
print(is_leap_year) # Output: True

13. Find the Second Largest Number in a List


lst = [1, 2, 3, 4, 5]
lst.sort()
second_largest = lst[-2]
print(second_largest) # Output: 4

14. Remove All Duplicates from a List


lst = [1, 2, 2, 3, 4, 4, 5]
unique_lst = []
for element in lst:
if element not in unique_lst:
unique_lst.append(element)
print(unique_lst) # Output: [1, 2, 3, 4, 5]

15. Find the Common Elements in Two Lists


lst1 = [1, 2, 3, 4, 5]
lst2 = [4, 5, 6, 7, 8]
common_elements = list(set(lst1) & set(lst2))
print(common_elements) # Output: [4, 5]

16. Check if a Given Number is an Armstrong Number


n = 153
sum_of_powers = sum(int(digit)**3 for digit in str(n))
is_armstrong = n == sum_of_powers
print(is_armstrong) # Output: True

17. Check if a Given String is a Palindrome or Not


s = "madam"
is_palindrome = s == s[::-1]
print(is_palindrome) # Output: True

18. Count the Number of Words in a Given Sentence


s = "Hello world, welcome to Python programming!"
word_count = len(s.split())
print(word_count) # Output: 6
19. Find the Sum of All Odd Numbers in a List
lst = [1, 2, 3, 4, 5]
sum_of_odd_numbers = sum(num for num in lst if num % 2 != 0)
print(sum_of_odd_numbers) # Output: 9

20. Check if a Given String is a Pangram or Not


import string
s = "The quick brown fox jumps over the lazy dog"
is_pangram = set(string.ascii_lowercase) <= set(s.lower())
print(is_pangram) # Output: True

21. Find the LCM (Least Common Multiple) of Two Numbers


a = 12
b = 15
greater = max(a, b)
while True:
if greater % a == 0 and greater % b == 0:
lcm = greater
break
greater += 1
print(lcm) # Output: 60

22. Remove All Vowels from a Given String


s = "hello world"
vowels = "aeiouAEIOU"
s_without_vowels = ''.join([char for char in s if char not in vowels])
print(s_without_vowels) # Output: "hll wrld"

23. Find the Median of a List of Numbers


lst = [1, 3, 3, 6, 7, 8, 9]
lst.sort()
n = len(lst)
if n % 2 == 0:
median = (lst[n//2 - 1] + lst[n//2]) / 2
else:
median = lst[n//2]
print(median) # Output: 6

XXXXXXXXXXXXXXXXXXXXXXXXX--------------------------XXXXXXXXXXXXXXXXXXXXXXXXX

You might also like