Lokesh Hero
Lokesh Hero
Solve 1 . #Pattern 1
num_rows = 5
sum = 0
sign = 1
term = sign * (x ** i)
sum += term
return sum
result = series_sum(x, n)
sum = 0
for i in range(1, n + 1) :
term = x ** i / i
sum += term
if is_perfect_square(num):
print(num, "is a perfect square.")
else:
print(num, "is not a perfect square.")
if is_armstrong_number(num):
print(num, "is an Armstrong number.")
else:
print(num, "is not an Armstrong number.")
if is_palindrome(num):
print(num, "is a palindrome.")
else:
print(num, "is not a palindrome.")
if __name__ == "__main__":
main()
Q7.Input a number and check if the number is a
prime or composite number
Solve.
def is_prime(num):
if num <= 1:
return False
elif num <= 3:
return True
elif num % 2 == 0 or num % 3 == 0:
return False
i=5
while i * i <= num:
if num % i == 0 or num % (i + 2) == 0:
return False
i += 6
return True
def main():
num = int(input("Enter a number: "))
if is_prime(num):
print(num, "is a prime number.")
else:
print(num, "is a composite number.")
if __name__ == "__main__":
main()
Q8.Display the terms of a Fibonacci series.
Solve.
def fibonacci_series(limit):
fib_series = []
a, b = 0, 1
while a <= limit:
fib_series.append(a)
a, b = b, a + b
return fib_series
def main():
limit = int(input("Enter the limit for the Fibonacci
series: "))
fib_series = fibonacci_series(limit)
print("Fibonacci series up to", limit, ":", fib_series)
if __name__ == "__main__":
main()
Q9. Compute the greatest common divisor and least
common multiple of two integers.
Solve.
def gcd(a, b):
while b:
a, b = b, a % b
return a
def main():
num1 = int(input("Enter the first integer: "))
num2 = int(input("Enter the second integer: "))
if __name__ == "__main__":
main()
Q10 . Count and display the number of vowels,
consonants, uppercase, lowercase characters in
string.
Solve.
def count_characters(string):
vowels = 0
consonants = 0
uppercase = 0
lowercase = 0
def main():
string = input("Enter a string: ")
vowels, consonants, uppercase, lowercase =
count_characters(string)
largest = numbers[0]
smallest = numbers[0]
for num in numbers:
if num > largest:
largest = num
elif num < smallest:
smallest = num
def main():
numbers = input("Enter a list of numbers
separated by spaces: ").split()
numbers = [float(num) for num in numbers]
largest, smallest = find_largest_smallest(numbers)
if __name__ == "__main__":
main()
return numbers
def main():
# Input a list of numbers
numbers = input("Enter a list of numbers
separated by spaces: ").split()
numbers = [int(num) for num in numbers] #
Convert input strings to integers
def main():
sequence_type = input("Enter the type of sequence
(list/tuple): ").lower()
if sequence_type == 'list':
elements = input("Enter a list of elements
separated by spaces: ").split()
sequence = [int(elem) for elem in elements] #
Convert input strings to integers
elif sequence_type == 'tuple':
elements = input("Enter a tuple of elements
separated by spaces: ").split()
sequence = tuple([int(elem) for elem in
elements]) # Convert input strings to integers
else:
print("Invalid sequence type. Please enter 'list'
or 'tuple'.")
return
if search_element(sequence,
search_element_value):
print("Element", search_element_value, "is
present in the", sequence_type)
else:
print("Element", search_element_value, "is not
present in the", sequence_type)
Q15. Create a dictionary with the roll number, name
and marks of n students in a class and display the
names of students who have marks above 75.
Solve.
def students_above_75_marks(students_dict):
above_75_students = [student['name'] for student
in students_dict.values() if student['marks'] > 75]
return above_75_students
def main():
n = int(input("Enter the number of students: "))
students_dict = {}
above_75_students =
students_above_75_marks(students_dict)
if __name__ == "__main__":
main()
Q16. Write a program in python that reverses a
string.
str1="this is a book"
for ch in str1:
print(ch, end='')
print()
print(str1[::-1])
length = len(str1) print("\nThe length of string is",
length)
s="this is a book"
ch=set(s) # Remove
duplicacy
ch=list(ch)
ch.sort()
print (ch)
Q19. Write a program in python to create of tuple
from a list
Solve19. n=int(input())
n=10
L=list()
for i in range(1,n,2):
L.append(i*i)
T=tuple (L)
Print (L)
print (T)
Q20. Write a program in python to print each word
of tuple using while and for loop.
# Using index()
value_to_find = 20
try:
index = my_tuple.index(value_to_find)
print(f"The first occurrence of {value_to_find} is at
index {index}.")
except ValueError:
print(f"{value_to_find} is not present in the
tuple.")
value_to_count = 20
count = my_tuple.count(value_to_count)
print(f"The value {value_to_count} appears {count}
times in the tuple.")
Q22. Write a program in python to create a
dictionary using string #s=input("Enter integer
number with spaces")
Solve22. s="4 5 6”
s=list(s.split())
d=dict()
for i in s:
k=int(i)
d[i]=k*k
print (d)
print(d['4'])
Q23. Write a python script which will Take a string
from the user as input
Print the number of vowels in the input string
if is_palindrome(word):
print(f"'{word}' is a palindrome.")
else:
print(f"'{word}' is not a palindrome.")
Q25. Write a program in python to find maximum
value and its location in a list.
Solve25.
my_list = [10, 30, 20, 50, 40]
max_value, max_index =
find_max_and_location(my_list)