VS

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

NAME: AMAN SUYAL

ROLL NO: 10
SECTION: E2
SUBJECT: PBC 401
Q.1 : Write a python program to check if a value entered by ther user is palindrome or not.

CODE:-

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

temp=n

rev=0

while(n>0):

dig=n%10

rev-rev*10+dig

n=n//10

if(temp==rev):

print("The number is a palindrome!")

else:

print("The number isn't a palindrome!")

OUTPUT:
NAME: AMAN SUYAL
ROLL NO: 10
SECTION: E2
SUBJECT: PBC 401

Q.2 : write a python program to check if a value entred by the user is armstrong or not.

CODE:- num = int(input("Enter a number:

"))

# initialize sum sum = 0

# find the sum of the cube of each digit


temp = num while temp
> 0:
digit = temp % 10 sum +=
digit ** 3 temp //= 10

# display the result if num == sum:


print(num,"is an Armstrong number") else:
print(num,"is not an Armstrong number")

OUTPUT:
NAME: AMAN SUYAL
ROLL NO: 10
SECTION: E2
SUBJECT: PBC 401

Q.3 : cosider a number entred by user. Now calculate the factorial of this number in
python. CODE :-

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


factorial = 1 if n >= 1: for i in range (1,
n+1): factorial=factorial *i
print("Factorial of the given number is: ", factorial)

OUTPUT:
NAME: AMAN SUYAL
ROLL NO: 10
SECTION: E2
SUBJECT: PBC 401

Q.4 : write a python program to print the fibonacci sequence upto the range entred by the
user.

CODE:-

nterms = int(input("How many terms? "))

n1, n2 = 0, 1 count = 0

if nterms <= 0:
print("Please enter a positive integer") elif nterms
== 1: print("Fibonacci sequence upto",nterms,":")
print(n1) else: print("Fibonacci sequence:") while
count < nterms:
print(n1) nth = n1
+ n2 # update values
n1 = n2 n2 = nth
count += 1

OUTPUT:
NAME: AMAN SUYAL
ROLL NO: 10
SECTION: E2
SUBJECT: PBC 401

Q.5 : write a python program to check wheter the number entred by the user is a perfect
number or not.

CODE:-

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


sum1 = 0 for i in range(1,
n): if(n % i == 0):
sum1 = sum1 + i if (sum1 == n): print("The
number is a Perfect number!") else: print("The
number is not a Perfect number!")

OUTPUT:
NAME: AMAN SUYAL
ROLL NO: 10
SECTION: E2
SUBJECT: PBC 401
Q6. 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).

CODE:

def replace_spaces_with_tabs(text):
return text.replace(" ", " ")

long_string = "This is an example string that we will use to replace spaces with tabs."
print(long_string)
result = replace_spaces_with_tabs(long_string)
print(result)

OUTPUT:
NAME: AMAN SUYAL
ROLL NO: 10
SECTION: E2
SUBJECT: PBC 401
Q7. Take any list and print it in the following manner:
a) Print only last 3 elements
b) Print all values except first and last value
c) Print only first 3 elements
CODE:

sample_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(sample_list)

# a) Print only last 3 elements


last_three = sample_list[-3:]
print("Last 3 elements:", last_three)

# b) Print all values except first and last value


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

# c) Print only first 3 elements


first_three = sample_list[:3]
print("First 3 elements:", first_three)

OUTPUT:
NAME: AMAN SUYAL
ROLL NO: 10
SECTION: E2
SUBJECT: PBC 401
Q8. Python program to remove duplicate values from a list.

CODE:

def remove_duplicates(input_list):

return list(set(input_list))

sample_list = [1, 2, 2, 3, 4, 4, 5]

print("original list:",sample_list)

unique_list = remove_duplicates(sample_list)

print("List after removing duplicates:", unique_list)

OUTPUT:
NAME: AMAN SUYAL
ROLL NO: 10
SECTION: E2
SUBJECT: PBC 401
Q9. 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.

CODE:

initial_tuple = (1, 2.5, 'hello', 3, 4.75, 'world', 5)

print("the tuple is:", initial_tuple)

non_decimal_values = []

for item in initial_tuple:

if not isinstance(item, float):

non_decimal_values.append(item)

new_tuple = tuple(non_decimal_values)

char_values = ('a', 'b', 'c')

final_tuple = new_tuple + char_values

print("Final tuple:", final_tuple)

OUTPUT:
NAME: AMAN SUYAL
ROLL NO: 10
SECTION: E2
SUBJECT: PBC 401
Q10. Make a dictionary in which keys are in numbers and values are cube of that key.

a) Print this dictionary


b) Print only its value
c) Now first empty that dictionary than again print it.

CODE:
numbers = [1, 2, 3, 4, 5]
cube_dict = {}
cube_dict[number] = number ** 3

print("Dictionary with keys and their cube values:", cube_dict)


print("Values in the dictionary:")
for value in cube_dict.values():
print(value)

cube_dict.clear()
print("Dictionary after emptying:", cube_dict)

OUTPUT:
NAME: AMAN SUYAL
ROLL NO: 10
SECTION: E2
SUBJECT: PBC 401
Q11. Write a python program having a user defined function which will calculate the
total number of vowels used in string entered by a user.

CODE:

def count_vowels(input_string):

vowels = 'aeiouAEIOU'

vowel_count = 0

for char in input_string:

if char in vowels:

vowel_count += 1

return vowel_count

user_input = input("Enter a string: ")

total_vowels = count_vowels(user_input)

print("Total number of vowels in the entered string:", total_vowels)

OUTPUT:
NAME: AMAN SUYAL
ROLL NO: 10
SECTION: E2
SUBJECT: PBC 401
Q12. A shop will give discount of 10% if the cost of purchased quantity is more than 1000
rupees. Now write a python program having a user defined function which will first
calculate whether a user purchased quantities more than 1000 rupees or not and then
accordingly it will print the total cost for user.

CODE:
def calculate_discount(total_cost):
if total_cost > 1000:
discount = total_cost * 0.10
discounted_price = total_cost - discount
return discounted_price, discount
else:
print(“discount is not applicable”)
total_cost = float(input("Enter the total cost of purchased quantities (in rupees): "))
final_cost, discount_given = calculate_discount(total_cost)
if discount_given > 0:
print(f"You have received a discount of {discount_given} rupees.")
else:
print("No discount applied.")
print(f"The final cost after discount is: {final_cost} rupees.")

OUTPUT:
NAME: AMAN SUYAL
ROLL NO: 10
SECTION: E2
SUBJECT: PBC 401
Q13. Suppose a company decided to give bonus of 5% to their employee if his/her year of
service in the company is more than 5 years. Now write a python program having a user
defined function which will print the net bonus amount. Ask user to input the salary and the
year of service.
CODE: def calculate_bonus(salary, years_of_service):

if years_of_service > 5:

bonus = salary * 0.05

else:

bonus = 0

return bonus

salary = float(input("Enter the salary (in rupees): "))

years_of_service = int(input("Enter the years of service: "))

bonus_amount = calculate_bonus(salary, years_of_service)

print(f"The net bonus amount is: {bonus_amount} rupees.")

OUTPUT:
NAME: AMAN SUYAL
ROLL NO: 10
SECTION: E2
SUBJECT: PBC 401
Q14. Write a lambda function in python which will multiplies two values (i.e. x*y). Now
using this function write a python program to print the table of a number (from 1 to 10)
entered by a user.
CODE:

multiply = lambda x, y: x * y

number = int(input("Enter a number to print its multiplication table: "))

print(f"Multiplication table for {number}:")

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

result = multiply(number, i)

print(f"{number} x {i} = {result}")

OUTPUT:
NAME: AMAN SUYAL
ROLL NO: 10
SECTION: E2
SUBJECT: PBC 401
Q15. Write a Python program to square and cube every number in a given list of integers
using Lambda.

CODE:
numbers = [1, 2, 3, 4, 5]
square = lambda x: x ** 2
cube = lambda x: x ** 3
squared_numbers = list(map(square, numbers))
cubed_numbers = list(map(cube, numbers))
print("Original numbers:", numbers)
print("Squared numbers:", squared_numbers)
print("Cubed numbers:", cubed_numbers)

OUTPUT:

You might also like