0% found this document useful (0 votes)
16 views10 pages

Python Slips Answers

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)
16 views10 pages

Python Slips Answers

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

Slip 1 1.Write a program to convert U.S. Dollar to indian rupees.

Inr_rate = 83.5
usd = float(input("Enter amount in U.S. Dollars: "))
inr = usd * usd_to_inr
print("Amount in Indian Rupees:", inr)

2.WAP to check whether the number is even or odd.

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


if num % 2 == 0:
print("The number is Even")
else:
print("The number is Odd")

Slip 2 1.WAP to convert bits to Megabytes,Gigabytes and Terabytes.

bits = float(input("Enter the number of bits: "))


bytes = bits / 8

megabytes = bytes_val / (1024 * 1024)


gigabytes = bytes_val / (1024 ** 3)
terabytes = bytes_val / (1024 ** 4)

print("Megabytes:", megabytes)
print("Gigabytes:", gigabytes)
print("Terabytes:", terabytes)

2.WAP to check weather the year is leap or not.

year = int(input("Enter a year: "))


if (year % 4 == 0):
print("Leap year")
else:
print("Not a leap year")

Slip 3 1.WAP to find the area of a rectangle

length = float(input("Enter the length of the rectangle: "))


width = float(input("Enter the width of the rectangle: "))
area = length * width
print("Area of the rectangle:", area)

2.WAP to check weather the number is positive,negative or zero.

num = float(input("Enter a number: "))


if num > 0:
print("The number is Positive")
elif num < 0:
print("The number is Negative")
else:
print("The number is Zero")
Slip 4 1.WAP to calculate Surface volume and area of a cylinder.

pi = 3.14
radius = float(input("Enter the radius of the cylinder: "))
height = float(input("Enter the height of the cylinder: "))

surface_area = 2 * pi * radius * (radius + height)


volume = pi * radius ** 2 * height
lateral_area = 2 * pi * radius * height

print("Surface Area of Cylinder:", surface_area)


print("Volume of Cylinder:", volume)
print("Lateral Surface Area:", lateral_area)

Slip 5 1.Write a program to calculate area and perimeter of the square.

side = float(input("Enter the side length of the square: "))


area = side ** 2
perimeter = 4 * side
print("Area of the square:", area)
print("Perimeter of the square:", perimeter)

Slip 6 1..WAP to find square root of a number.

import math
num = float(input("Enter a number: "))
square_root = math.sqrt(num)
print("The square root of", num, "is", square_root)

2.WAP to find out absolute value of input number.

num = float(input("Enter a number: "))


absolute_value = abs(num)
print("The absolute value of", num, "is", absolute_value)

Slip 7 1.WAP to swap The value of Two variables.

a = int(input("Enter the value of a: "))


b = int(input("Enter the value of b: "))
a, b = b, a
print("After swapping:")
print("a =", a)
print("b =", b)
Slip 8 1.WAP that takes the marks of 5 subjects and displays the grade.

marks = []
for i in range(5):
mark = float(input(f"Enter marks for subject {i + 1}: "))
marks.append(mark)

total_marks = sum(marks)
percentage = (total_marks / 500) * 100

if percentage >= 90:


grade = 'A+'
elif percentage >= 80:
grade = 'A'
elif percentage >= 70:
grade = 'B+'
elif percentage >= 60:
grade = 'B'
elif percentage >= 50:
grade = 'C'
else:
grade = 'F'

print(f"Total Marks: {total_marks} / 500")


print(f"Percentage: {percentage}%")
print(f"Grade: {grade}")

Slip 9 WAP to check largest of 3 numbers.

num1 = float(input("Enter first number: "))


num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
if num1 >= num2 and num1 >= num3:
largest = num1
elif num2 >= num1 and num2 >= num3:
largest = num2
else:
largest = num3
print("The largest number is:", largest)

Slip 10 Print the following pattern


12345
1234
123
12
1

for i in range(5, 0, -1):


for j in range(1, i + 1):
print(j, end="")
print()
Slip 11 Print the following pattern
*
***
*****
***
*

# Upper part of the pattern


for i in range(1, 6, 2): # i takes values 1, 3, 5
spaces = (5 - i) // 2
stars = i
print(" " * spaces + "*" * stars)

# Lower part of the pattern


for i in range(3, 0, -2): # i takes values 3, 1
spaces = (5 - i) // 2
stars = i
print(" " * spaces + "*" * stars)

Slip 12 Print the following pattern


1010101
10101
101
1

for i in range(7, 0, -2):


print(" " * ((7 - i) // 2) + "10" * (i // 2) + "1" * (i % 2))

Slip 13 WAP to print all even numbers between 1 to 100 using while loop
num = 2
while num <= 100:
print(num)
num += 2

Slip 14 WAP to print sum of all natural numbers using for loop.

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


sum_of_numbers = 0
for i in range(1, n+1):
sum_of_numbers += i
print("Sum of all natural numbers up to", n, "is:", sum_of_numbers)

Slip 15 WAP to find factorial of a given number

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


factorial = 1
for i in range(1, num + 1):
factorial *= i
print("The factorial of", num, "is:", factorial)
Slip 16 WAP to reverese a given number

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


reverse = 0

while num != 0:
digit = num % 10
reverse = reverse * 10 + digit
num //= 10

print("Reversed number is:", reverse)

Slip 17 WAP to check given number is palindrome or not

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


original_num = num
reversed_num = 0

while num != 0:
digit = num % 10
reversed_num = reversed_num * 10 + digit
num //= 10

if original_num == reversed_num:
print(f"{original_num} is a palindrome.")
else:
print(f"{original_num} is not a palindrome.")

Slip 18 WAP to find sum of digits in a number.

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


sum_of_digits = 0

while num != 0:
digit = num % 10
sum_of_digits += digit
num //= 10
print("Sum of digits is:", sum_of_digits)

Slip 19 WAP to print fibonacci series.

n = int(input("Enter the number of terms in Fibonacci series: "))


a, b = 0, 1
print(a, end=" ")
print(b, end=" ")
for i in range(2,n):
f = a+b
print(f, end=" ")
a=b
b=f
Slip 20 Perform following operations on a list
1.Create a list
2.Find sum of all items in a list
3.find largest and smallest number from list
4.append a new item in a list

my_list = [12, 34, 5, 67, 23, 89]

sum_of_list = sum(my_list)
print("Sum of all items in the list:", sum_of_list)

largest_num = max(my_list)
smallest_num = min(my_list)
print("Largest number in the list:", largest_num)
print("Smallest number in the list:", smallest_num)

new_item = int(input("Enter a new item to append to the list: "))


my_list.append(new_item)
print("Updated list:", my_list)

Slip 21 Perform following operations on a list


1.Create a list
2Add 1 new element
3.reverse list
4.delete 1 element from list

my_list = [10, 20, 30, 40, 50]


print("Original list:", my_list)

my_list.append(60)
print("List after adding a new element:", my_list)

my_list.reverse()
print("List after reversing:", my_list)

my_list.remove(30)
print("List after deleting an element:", my_list)
Slip 22 Perform following operations on a tuple
1.Create a tuple and find min and maximun number from it
2.Find the repeated items of tuple

my_tuple = (4, 7, 2, 7, 9, 3, 7, 2, 5)
print("Tuple:", my_tuple)

print("Minimum value:", min(my_tuple))


print("Maximum value:", max(my_tuple))

repeated_items = []
for item in my_tuple:
if my_tuple.count(item) > 1 and item not in repeated_items:
repeated_items.append(item)

print("Repeated items:", repeated_items)

Slip 23 WAP to create number in words for example:1234->one twp three four.

digit_words = {
'0': 'zero',
'1': 'one',
'2': 'two',
'3': 'three',
'4': 'four',
'5': 'five',
'6': 'six',
'7': 'seven',
'8': 'eight',
'9': 'nine'
}

number = input("Enter a number: ")

print("Number in words:")
for digit in number:
print(digit_words[digit], end=" ")

Slip 24 WAP to create a set ,add members in a set and remove one item from a set.

my_set = {10, 20, 30}


print("Original set:", my_set)

my_set.add(40)
my_set.add(50)
print("Set after adding elements:", my_set)

my_set.remove(20)
print("Set after removing an element:", my_set)
Slip 25 WAP to perform set operations
Union,intersection,Difference,symmetric difference.

set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}

print("Set 1:", set1)


print("Set 2:", set2)

print("Union:", set1.union(set2))

print("Intersection:", set1.intersection(set2))

print("Difference (set1 - set2):", set1.difference(set2))

print("Symmetric Difference:", set1.symmetric_difference(set2))

Slip 26 Write a python function that accepts a string and calculate the number of upper case
and lowercase letters

text = input("Enter a string: ")

upper_count = 0
lower_count = 0

for char in text:


if char.isupper():
upper_count += 1
elif char.islower():
lower_count += 1

print("Uppercase letters:", upper_count)


print("Lowercase letters:", lower_count)

Slip 27 Write a python function to check weather the given number is prime or not.

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

if num <= 1:
print(num, "is not a prime number.")
else:
prime = True
for i in range(2, num):
if num % i == 0:
prime = False
break

if prime:
print(num, "is a prime number.")
else:
print(num, "is not a prime number.")
Slip 28 Write a program to perform follwing math functions.
Pow,sqrt,factorial,reminder

import math

num1 = int(input("Enter 1st num: "))


num2 = int(input("Enter 2nd num: "))

power = pow(num1, num2)


print("Power: ",power)

sqrt_val = math.sqrt(num1)
print("Square root: ",sqrt_val)

factorial_val = math.factorial(num2)
print("Factorial:",factorial_val)

remainder = num1 % num2


print("Remainder: ",remainder)

Slip 29 Take a string from user and perform following functions on it


Capitalize(),isdigit(),replace(),rfind(),index(),split(),isspace(),find()

str = input("Enter a string: ")

capitalized = str.capitalize()
print("Capitalized string:", capitalized)

is_digit = str.isdigit()
print("Is the string all digits?:", is_digit)

old_substring = input("Enter a substring to replace: ")


new_substring = input("Enter the new substring: ")
replaced_string = str.replace(old_substring, new_substring)
print("String after replacement:", replaced_string)

substring_to_find = input("Enter a substring to find last occurrence: ")


last_occurrence = str.rfind(substring_to_find)
print("Last occurrence of the substring:", last_occurrence)

first_occurrence = str.find(substring_to_find)
print("First occurrence of the substring:", first_occurrence)

split_string = str.split()
print("String after splitting:", split_string)

is_space = str.isspace()
print("Is the string all spaces?:", is_space)

index_of_substring = str.index(substring_to_find) if substring_to_find in str else -1


print("Index of first occurrence of the substring:", index_of_substring)
Slip 30 Write a python program to implement multiple inheritance

class ClassA:
def method_A(self):
print("Method from Class A")

class ClassB:
def method_B(self):
print("Method from Class B")

class ChildClass(ClassA, ClassB):


def method_C(self):
print("Method from ChildClass")

child = ChildClass()
child.method_A()
child.method_B()
child.method_C()

Slip 31 Create a class Employee with data members name.dept,salary.Create sutaible methods
for reading and printing employee information

class Employee:
def read_info(self):
self.name = input("Enter employee name: ")
self.dept = input("Enter department: ")
self.salary = float(input("Enter salary: "))

def print_info(self):
print("\nEmployee Information:")
print("Name:", self.name)
print("Department:", self.dept)
print("Salary:", self.salary)

emp = Employee()
emp.read_info()
emp.print_info()

You might also like