0% found this document useful (0 votes)
4 views

Assignment

Uploaded by

simpbac
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Assignment

Uploaded by

simpbac
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

Write a python program to check if a value entered by a user is


Palindrome or not.
Ans.
def is_palindrome(value):

value = str(value)

if value == value[::-1]:

print(f"{value} is a palindrome.")

else:

print(f"{value} is not a palindrome.")

value = input("Enter a value: ")

is_palindrome(value)

2. Write a python program to check if a value entered by a user is


Armstrong or not.
Ans.
def is_armstrong(number):

number = int(number)

sum_of_cubes = sum([int(digit) ** 3 for digit in str(number)])

if number == sum_of_cubes:

print(f"{number} is an Armstrong number.")

else:

print(f"{number} is not an Armstrong number.")

number = input("Enter a number: ")

is_armstrong(number)=
3. Consider a number entered by a user. Now calculate the Factorial of
this number
Ans
def factorial(n):

n = int(n)

fact = 1

for i in range(1, n + 1):

fact *= i

return fact

number = input("Enter a number: ")

print(f"The factorial of {number} is {factorial(number)}")

4. Write a python program to print the Fibonacci sequence up to the


range a user enters.
Ans.
def fibonacci(n):

n = int(n)

a, b = 0, 1

sequence = []

while a < n:

sequence.append(a)

a, b = b, a + b

return sequence

number = input("Enter the range: ")

print("Fibonacci:", fibonacci_(number))
5. Write a python program to check whether the number entered by the
user is a Perfect number or not.
Ans.
def is_perfect_number(n):

n = int(n)

divisors = [i for i in range(1, n) if n % i == 0]

if sum(divisors) == n:

print(f"{n} is a perfect number.")

else:

print(f"{n} is not a perfect number.")

number = input("Enter a number: ")

is_perfect_number(number)
6. Consider any long string. Now replace each space between two words
with the tab (i.e. the space created when you press the key 'Tab' from
your keyboard).
Ans.
def replace_spaces_with_tabs(string):

return string.replace(" ", "\t")

input_string = input("Enter a string: ")

print(replace_spaces_with_tabs(input_string))

7. Take any list and print it in the following manner:


a) Print only the last 3 elements
b) Print all values except the first and last value
c) Print only the first 3 elements
Ans.
my_list = [int(x) for x in input("Enter a list of numbers (comma-separated): ").split(',')]

print("Last 3 elements:", my_list[-3:])

print("All values except first and last:", my_list[1:-1])

print("First 3 elements:", my_list[:3])


8. Python program to remove duplicate values from a list.
Ans.
def remove_duplicates(my_list):

return list(set(my_list))

my_list = [int(x) for x in input("Enter a list of numbers (comma-separated): ").split(',')]

print("List after removing duplicates:", remove_duplicates(my_list))

9. Consider a tuple having values integer, decimal and string. Write a


python program to delete all decimal values and add 3 character
values in it.
Ans.
def modify_tuple(my_tuple):

modified_tuple = tuple(value for value in my_tuple if not isinstance(value, float))

modified_tuple += ('a', 'b', 'c')


return modified_tuple

my_tuple = (1, 2.5, "string", 3, 4.8)

print("Original Tuple:", my_tuple)

print("Modified Tuple:", modify_tuple(my_tuple))

10.Make a dictionary in which keys are in numbers and values are cubes
of that key.
a) Print this dictionary
b) Print only its value
c) Now first empty that dictionary than again print it.
Ans.
cube_dict = {x: x**3 for x in range(1, 6)}

print("Dictionary:", cube_dict)

print("Values:", list(cube_dict.values()))

cube_dict.clear()

print("Emptied Dictionary:", cube_dict)

You might also like