Untitled

 avatar
unknown
plain_text
2 months ago
17 kB
1
Indexable
NAME: JENIL K. SAVALIYA ENROLLMENT NO:2202020111550
     
     
pg. 1
1. Write a python program to input UserName and print welcome message
in same line using two print statement
SOLUATION.
print("Enter your username: ") username
= input()
print("Welcome, " + username + "!")
OUTPUT:
 
     
     
pg. 2
2. Write a python program to find the area and circumference of a circle.
SOLUATION
radius = float(input("Enter the radius of the circle: "))
# Calculate the area of the circle area
= pi * radius ** 2 circumference = 2 *
pi * radius
print(f"Area: {area:.2f}")
print(f"Circumference: {circumference:.2f}")
OUTPUT:
 
     
     
pg. 3
3. Write a python program to find the simple interest for the given data
SOLUATION:
print("Enter the principal amount: ") principal =
float(input())
print("Enter the interest rate (in %): ") rate
= float(input())
print("Enter the time (in years): ") time
= float(input())
interest = (principal * rate * time) / 100
print(f"The simple interest is: {interest:.2f}")
OUTPUT:
 
     
     
pg. 4
4.Write a python program to swap the content of two variables using third
variable
SOLUATION:
x = 5
y = 10
print("Before swapping:")
print("x =", x)
print("y =", y)
temp = x
x = y y =
temp
print("After swapping:")
print("x =", x) print("y
=", y)
OUTPUT:
 
     
     
pg. 5
5. Write a python program to swap the content of three variables
without using third variable .
SOLUATION:
x = 5
y = 10
z = 15
print("Before swapping:")
print("x =", x) print("y
=", y) print("z =", z)
x, y, z = z, x, y
print("After swapping:")
print("x =", x) print("y
=", y) print("z =", z)
OUTPUT:
 
     
     
pg. 6
6. Write a python program to check whether given year is leap year or
not.
def is_leap_year(year):
if year % 4 == 0: if year
% 100 == 0: if year %
400 == 0:
return True
else:
return False else:
return True
else:
return False
year = int(input("Enter a year: ")) if
is_leap_year(year):
print(year, "is a leap year.")
else:
print(year, "is not a leap year.")
OUTPUT:
 
     
     
pg. 7
7. Write a python program to convert Fahrenheit to Centigrade
SOLUATION:
def fahrenheit_to_centigrade(fahrenheit):
centigrade = (fahrenheit - 32) * 5.0/9.0
return centigrade
fahrenheit = float(input("Enter temperature in Fahrenheit: "))
centigrade = fahrenheit_to_centigrade(fahrenheit)
print(f"{fahrenheit} degrees Fahrenheit is equal to {centigrade} degrees Centigrade.")
OUTPUT:
 
     
     
pg. 8
8. Write a python program to accept two integers from user and display
addition, Subtraction, Multiplication, Division of two integers.
SOLUATION:
x = 40 y =
12 add = x
+ y sub = x
- y pro = x
* y div = x
 
     
     
pg. 9
/ y
print(add)
print(sub)
print(pro)
print(div)
OUTPUT:
9. Write a python program to find maximum no from 3 number using if,
if..elif and nested if
 
     
     
pg. 10
SOLUATION:
a = int(input("Enter first number: ")) b
= int(input("Enter second number: "))
c = int(input("Enter third number: "))
if a > b: if
a > c:
print(a, "is the maximum") else:
print(c, "is the maximum")
else: if b > c:
print(b, "is the maximum") else:
print(c, "is the maximum")
Using if-elif statements:
a = int(input("Enter first number: ")) b
= int(input("Enter second number: "))
c = int(input("Enter third number: "))
if a > b and a > c: print(a,
"is the maximum") elif b >
a and b > c:
print(b, "is the maximum") else:
print(c, "is the maximum")
Using nested if statements:
a = int(input("Enter first number: ")) b
= int(input("Enter second number: "))
c = int(input("Enter third number: "))
if a > b: if
a > c:
print(a, "is the maximum") else:
print(c, "is the maximum") elif
b > c:
print(b, "is the maximum") else:
print(c, "is the maximum")
 
     
     
pg. 11
OUTPUT:
 
     
     
pg. 12
10. Program to print full pyramid using
SOLUATION:
n = int(input("Enter the number of rows: "))
for i in range(n):
print(' ' * (n - i - 1) + '*' * (2 * i + 1))
OUTPUT
 
     
     
pg. 13
11. Write a python program to print the factorial of a inputted
number.(Ex. N= 5 O/p: 5*4*3*2*1=120)
SOLUATION:
def factorial(n):
if n == 0:
return 1 else:
return n * factorial(n-1)
num = int(input("Enter a number: "))
print("The factorial of", num, "is", factorial(num))
OUTPUT:
 
     
     
pg. 14
12. Write a python program to print the Fibonacci series up to a given
number
SOLUATION:
def fibonacci(n):
a, b = 0, 1 print(a,
end=' ') while b <
n: print(b, end='
')
a, b = b, a + b
num = int(input("Enter a number: ")) print("Fibonacci
series up to", num, ":")
fibonacci(num)
OUTPUT:
 
     
     
pg. 15
13.Write a python program to print multiplication tables of given range
SOLUATION :
def print_multiplication_tables(start, end):
for i in range(start, end+1):
print(f"Multiplication table for
{i}:") for j in range(1, 11): print(f"{i}
x {j} = {i*j}") print()
start = int(input("Enter the starting number: "))
end = int(input("Enter the ending number: "))
print_multiplication_tables(start, end)
OUTPUT:
 
     
     
pg. 16
14. Write program to check whether the given number is palindrome or
not.
SOLUATION:
def is_palindrome(n):
return str(n) == str(n)[::-1]
num = int(input("Enter a number: "))
if is_palindrome(num):
print(num, "is a palindrome")
else:
print(num, "is not a palindrome")
OUTPUT:
 
     
     
pg. 17
15. Write program to print the Pascal triangle
SOLUATION:
def is_palindrome(n):
return str(n) == str(n)[::-1]
 
     
     
pg. 18
num = int(input("Enter a number:
")) if is_palindrome(num):
print(num, "is a palindrome")
else:
print(num, "is not a palindrome")
OUTPUT:
16. Write program to find the Armstrong numbers between 100 and 1000.
SOLUATION
def is_armstrong(n):
return n == sum(int(digit) ** len(str(n)) for digit in str(n))
armstrong_numbers = [n for n in range(100, 1001) if is_armstrong(n)]
 
     
     
pg. 19
print("Armstrong numbers between 100 and 1000:")
print(armstrong_numbers)
OUTPUT:
 
     
     
pg. 20
17. Write a python program to input student details (Roll No, Name , 3
Subjects Marks) and Display Result Sheet with proper format
SOLUATION:
def display_result_sheet(): roll_no =
input("Enter Roll No: ") name =
input("Enter Name: ") subject1 =
input("Enter Subject 1 Marks: ") subject2
= input("Enter Subject 2 Marks: ")
subject3 = input("Enter Subject 3 Marks:
")
print("\nResult Sheet")
print("=============")
print(f"Roll No: {roll_no}")
print(f"Name: {name}")
print("Subjects:") print(f"
Subject 1: {subject1}") print(f"
Subject 2: {subject2}") print(f"
Subject 3: {subject3}")
print(f"Total Marks: {int(subject1) + int(subject2) + int(subject3)}")
print(f"Percentage: {(int(subject1) + int(subject2) + int(subject3)) / 3}%")
display_result_sheet()
OUTPUT:
 
     
     
pg. 21
 
     
     
pg. 22
18. Write a python program to check whether inputted string is
palindrome or not
SOLUATION
def is_palindrome(s): return
s == s[::-1]
string = input("Enter a string: ") if
is_palindrome(string):
print(string, "is a palindrome") else:
print(string, "is not a palindrome")
OUTPUT:
 
     
     
pg. 23
19. Write a python program to count No of Uppercase Characters,
Lowercase Characters, Digits, Special Characters and Space from the
sentence.
SOLUATION:
def count_characters(sentence):
uppercase = 0
lowercase = 0
digits = 0 special
= 0
spaces = 0
for char in sentence:
if char.isupper():
uppercase += 1 elif
char.islower():
lowercase += 1 elif
char.isdigit():
digits += 1 elif
char.isspace():
spaces += 1 else:
special += 1
print(f"Uppercase: {uppercase}")
print(f"Lowercase: {lowercase}")
 
     
     
pg. 24
print(f"Digits: {digits}") print(f"Special:
{special}")
print(f"Spaces: {spaces}")
sentence = input("Enter a sentence: ")
count_characters(sentence)
OUTPUT:
 
     
     
pg. 25
20. Write a python program to convert sentence in toggle case without
using in-built function.
SOLUATION:
def toggle_case(sentence):
result = "" for char
in sentence: if
char.isupper():
result += char.lower()
elif char.islower():
result += char.upper()
else:
result += char
return result
sentence = input("Enter a sentence: ")
print(toggle_case(sentence))
OUTPUT:
 
     
     
pg. 26
21. Write a python program to display each words along with it’s total no
of characters and at the end display word which have maximum
character.
SOLUATION:
def display_words_with_max_char(sentence):
words = sentence.split()
max_char_word = ""
max_char_count = 0
for word in words:
char_count = len(word)
print(f"{word} - {char_count} characters")
if char_count > max_char_count:
max_char_count = char_count
max_char_word = word
print(f"\nThe word with the maximum number of characters is: {max_char_word}
({max_char_count} characters)")
sentence = input("Enter a sentence: ")
display_words_with_max_char(sentence)
OUTPUT:
 
     
     
pg. 27
22. Write a python program to enter a line and reverse a words from
line which have inputted by user. Ex. String is: : hello girls hello boys hello
All!! Enter Word:hello. O/P: olleh girls olleh boys olleh All!!/
SOLUATION:
def reverse_words(line, word):
words = line.split()
reversed_words = [word[::-1] if w == word else w for w in words]
return ' '.join(reversed_words)
line = input("Enter a line: ") word
= input("Enter a word: ")
print(reverse_words(line, word))
OUTPUT:
 
     
     
pg. 28
23. Write a program that will calculate summation of numbers stored
at even locations and summation of numbers stored at odd locations in
list with 10 element
SOLUATION:
def calculate_summations(numbers):
even_sum = sum(numbers[::2]) odd_sum
= sum(numbers[1::2])
return even_sum, odd_sum
numbers = [int(input(f"Enter number {i+1}: ")) for i in range(10)]
even_sum, odd_sum = calculate_summations(numbers)
print(f"Sum of numbers at even locations: {even_sum}")
print(f"Sum of numbers at odd locations: {odd_sum}")
OUTPUT:
 
     
     
pg. 29
24. Write a python program to create menu driven program of list
which perform insert, update, delete, search, sorting and display element
in or from list. Also provide nested menu for delete, sorting.
SOLUATION:
my_list = []
# Define a function to display the menu
def display_menu(): print("Menu:")
print("1. Insert") print("2. Update")
print("3. Delete") print("4. Search")
print("5. Sorting") print("6. Display")
 
     
     
pg. 30
print("7. Exit")
# Define a function to insert an element into the
list def insert_element(): element =
int(input("Enter an element to insert: "))
my_list.append(element)
print("Element inserted successfully!")
# Define a function to update an element in the list
def update_element():
index = int(input("Enter the index of the element to update:
")) element = int(input("Enter the new value: ")) my_list[index]
= element print("Element updated successfully!") def
delete_element(): print("Delete Menu:") print("1. Delete by
index") print("2. Delete by value") choice = int(input("Enter
your choice: ")) if choice == 1: index = int(input("Enter the
index of the element to delete: ")) del my_list[index]
print("Element deleted successfully!")
elif choice == 2:
value = int(input("Enter the value of the element to delete: "))
my_list.remove(value)
print("Element deleted successfully!")
# Define a function to search for an element in the list
def search_element():
element = int(input("Enter an element to search:
")) if element in my_list: print("Element found!")
else:
print("Element not found!")
# Define a function to sort the list def
sort_list():
print("Sorting Menu:") print("1.
Ascending") print("2. Descending")
choice = int(input("Enter your choice:
")) if choice == 1: my_list.sort()
print("List sorted in ascending order!") elif
choice == 2:
my_list.sort(reverse=True) print("List
sorted in descending order!")
 
     
     
pg. 31
# Define a function to display the
list def display_list(): print("List:")
for element in my_list:
print(element)
# Main program while
True: display_menu()
choice = int(input("Enter your choice: "))
if choice == 1: insert_element() elif
choice == 2: update_element() elif
choice == 3: delete_element() elif
choice == 4:
search_element() elif
choice == 5:
sort_list() elif
choice == 6:
display_list()
elif choice == 7:
break else:
print("Invalid choice! Please try again.")
OUTPUT:
 
     
     
pg. 32
25. Write a python program that reads a string and then prints a string
that capitalizes every other letter in the string.(Ex. Enter String: python
O/p: pYtHoN
SOLUATION:
def capitalize_alternate_letters(s):
result = ""
uppercase =
 
     
     
pg. 33
True for char in
s: if
char.isalpha(): if
uppercase:
result += char.upper()
else:
result += char.lower()
uppercase = not uppercase
else:
result += char
return result
s = input("Enter a string: ")
print(capitalize_alternate_letters(s))
OUTPUT:
 
     
     
pg. 34
26. Write a python program to find minimum element from a list of
element along with its index in the list.
OUTPUT:
def find_min_element(lst):
min_element = min(lst) min_index
= lst.index(min_element)
return min_element, min_index
lst = [int(x) for x in input("Enter a list of numbers: ").split()]
min_element, min_index = find_min_element(lst)
print(f"Minimum element: {min_element}, Index: {min_index}")
OUTPUT:
 
     
     
pg. 35
27. Write a python program to count frequency of a given element in
a list of numbers.
OUTPUT:
def count_frequency(lst, element):
count = lst.count(element)
return count
lst = [int(x) for x in input("Enter a list of numbers: ").split()]
element = int(input("Enter the element to search: "))
frequency = count_frequency(lst, element)
print(f"Frequency of {element}: {frequency}")
OUTPUT:
 
     
     
pg. 36
28. Write a python program to create menu driven program of
dictionary which perform insert, update, delete, and display element in
or from dictionary. Also provide nested menu for delete element
SOLUATION:
my_dict = {}
# Define a function to display the menu
def display_menu(): print("Menu:")
print("1. Insert") print("2. Update")
print("3. Delete") print("4. Display")
print("5. Exit")
# Define a function to insert an element into the dictionary
def insert_element():
key = input("Enter the key: ")
value = input("Enter the value: ")
my_dict[key] = value
print("Element inserted successfully!")
# Define a function to update an element in the dictionary def
update_element():
key = input("Enter the key: ") value =
input("Enter the new value: ")
my_dict[key] = value print("Element
updated successfully!")
# Define a function to delete an element from the
dictionary def delete_element(): print("Delete Menu:")
 
     
     
pg. 37
print("1. Delete by key") print("2. Delete by value") choice =
int(input("Enter your choice: ")) if choice == 1:
key = input("Enter the key:
") if key in my_dict: del
my_dict[key] print("Element
deleted successfully!") else:
print("Key not found!") elif
choice == 2:
value = input("Enter the value: ") if
value in my_dict.values():
key = list(my_dict.keys())[list(my_dict.values()).index(value)]
del my_dict[key] print("Element
deleted successfully!") else:
print("Value not found!") def
display_dict():
print("Dictionary:") for key,
value in my_dict.items():
print(f"{key}: {value}")
while True:
display_menu()
choice = int(input("Enter your choice: "))
if choice == 1: insert_element() elif
choice == 2: update_element() elif
choice == 3: delete_element() elif
choice == 4: display_dict() elif choice ==
5:
break else: print("Invalid choice! Please
try again.")
OUTPUT:
 
     
     
pg. 38




29. Create a dictionary whose keys are month names and values are the
number of days in the corresponding months. (a) Ask the user to enter a month
name and use the dictionary to tell how many days are in the month. (b) Print
out all the keys in the alphabetical order. (c) Print out all the months with 31
days. (d) Print Out the (key-value) pairs sorted by the number of days in each
month
SOLUATION:
month_days = {
"January": 31,
"February": 28,
"March": 31,
"April": 30,
"May": 31,
"June": 30,
 
     
     
pg. 39
"July": 31,
"August": 31,
"September": 30,
"October": 31,
"November": 30,
"December": 31
}
# (a) Ask the user to enter a month name and use the dictionary to tell how many days are
in the month month = input("Enter a month name: ") if month in month_days:
print(f"{month} has {month_days[month]} days.")
else:
print("Invalid month name!")
# (b) Print out all the keys in the alphabetical
order print("Month names in alphabetical order:")
for month in sorted(month_days.keys()):
print(month)
# (c) Print out all the months with 31
days print("Months with 31 days:") for
month, days in month_days.items():
if days == 31:
print(month)
 
     
     
pg. 40
# (d) Print Out the (key-value) pairs sorted by the number of days in each
month print("Month days sorted by number of days:") for month, days in
sorted(month_days.items(), key=lambda x: x[1]):
print(f"{month}: {days}")
OUTPUT:
 
     
     
pg. 41
Leave a Comment