1.
# Function to accept details and calculate total marks and percentage
def get_student_details():
roll_number = input("Enter your roll number: ")
name = input("Enter your name: ")
marks = []
for i in range(6):
while True:
try:
mark = float(input(f"Enter marks for subject {i + 1}: "))
if mark < 0 or mark > 100:
print("Please enter a valid mark between 0 and 100.")
else:
marks.append(mark)
break
except ValueError:
print("Invalid input. Please enter a numeric value.")
total_marks = sum(marks)
percentage = (total_marks / 600) * 100 # Assuming each subject is out of 100
print("\nStudent Details:")
print(f"Roll Number: {roll_number}")
print(f"Name: {name}")
for i in range(6):
print(f"Marks in Subject {i + 1}: {marks[i]}")
print(f"Total Marks: {total_marks}")
print(f"Percentage: {percentage:.2f}%")
# Call the function
get_student_details()
Outputs 1)Enter your roll number: 32
Enter your name: Daivik
Enter marks for subject 1: 99
Enter marks for subject 2: 98
Enter marks for subject 3: 97
Enter marks for subject 4: 96
Enter marks for subject 5: 97
Enter marks for subject 6: 99
Student Details:
Roll Number: 32
Name: Daivik
Marks in Subject 1: 99.0
Marks in Subject 2: 98.0
Marks in Subject 3: 97.0
Marks in Subject 4: 96.0
Marks in Subject 5: 97.0
Marks in Subject 6: 99.0
Total Marks: 586.0
Percentage: 97.67%
2)Enter your roll number: 33
Enter your name: Parinith
Enter marks for subject 1: 90
Enter marks for subject 2: 91
Enter marks for subject 3: 92
Enter marks for subject 4: 93
Enter marks for subject 5: 94
Enter marks for subject 6: 95
Student Details:
Roll Number: 33
Name: Parinith
Marks in Subject 1: 90.0
Marks in Subject 2: 91.0
Marks in Subject 3: 92.0
Marks in Subject 4: 93.0
Marks in Subject 5: 94.0
Marks in Subject 6: 95.0
Total Marks: 555.0
Percentage: 92.50%
2. import math
def calculate_area_of_circle():
try:
diameter = float(input("Enter the diameter of the circle: "))
if diameter < 0:
print("Diameter cannot be negative. Please enter a positive value.")
return
radius = diameter / 2
area = math.pi * (radius ** 2)
print(f"The area of the circle with diameter {diameter} is {area:.2f}")
except ValueError:
print("Invalid input. Please enter a numeric value.")
# Call the function
calculate_area_of_circle()
Outputs 1. The area of the circle with diameter 6.0 is 28.27
2. The area of the circle with diameter 69.0 is 3739.28
3. def calculate_sum_and_average():
try:
# Accepting three numbers from the user
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
# Calculating the sum
total_sum = num1 + num2 + num3
# Calculating the average
average = total_sum / 3
# Printing the results
print(f"The sum of the numbers is: {total_sum}")
print(f"The average of the numbers is: {average:.2f}")
except ValueError:
print("Invalid input. Please enter numeric values.")
# Call the function
calculate_sum_and_average()
Outputs 1. Enter the first number: 2
Enter the second number: 234567
Enter the third number: 1234567890
The sum of the numbers is: 1234802459.0
The average of the numbers is: 411600819.67
2. Enter the first number: 1234567890
Enter the second number: 0987654321
Enter the third number: 5678904321
The sum of the numbers is: 7901126532.0
The average of the numbers is: 2633708844.00
4. def find_biggest_and_smallest():
try:
# Accepting two numbers from the user
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
# Finding the biggest and smallest numbers
if num1 > num2:
biggest = num1
smallest = num2
elif num2 > num1:
biggest = num2
smallest = num1
else:
biggest = smallest = num1 # Both numbers are equal
# Printing the results
if num1 == num2:
print(f"Both numbers are equal: {num1}")
else:
print(f"The biggest number is: {biggest}")
print(f"The smallest number is: {smallest}")
except ValueError:
print("Invalid input. Please enter numeric values.")
# Call the function
find_biggest_and_smallest()
Outputs 1. Enter the first number: 2345
Enter the second number: 23456
The biggest number is: 23456.0
The smallest number is: 2345.0
2. Enter the first number: 1234
Enter the second number: 1234
Both numbers are equal: 1234.0
5. def find_biggest_among_three():
try:
# Accepting three numbers from the user
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
# Finding the biggest number
if num1 >= num2 and num1 >= num3:
biggest = num1
elif num2 >= num1 and num2 >= num3:
biggest = num2
else:
biggest = num3
# Printing the result
print(f"The biggest number is: {biggest}")
except ValueError:
print("Invalid input. Please enter numeric values.")
# Call the function
find_biggest_among_three()
Outputs 1. Enter the first number: 123456
Enter the second number: 123465
Enter the third number: 1234655
The biggest number is: 1234655.0
2. Enter the first number: 1289
Enter the second number: 1289
Enter the third number: 1289
The biggest number is: 1289.0
6. def test_number_sign():
try:
# Accepting a number from the user
number = float(input("Enter a number: "))
# Determining if the number is positive, negative, or zero
if number > 0:
print("The number is positive.")
elif number < 0:
print("The number is negative.")
else:
print("The number is zero.")
except ValueError:
print("Invalid input. Please enter a numeric value.")
# Call the function
test_number_sign()
Outputs 1. Enter a number: -2. The number is negative.
2. Enter a number: 0. The number is zero.
7. def generate_report_card():
try:
# Accepting marks for 6 subjects from the user
marks = []
for i in range(6):
mark = float(input(f"Enter marks for subject {i + 1}: "))
if mark < 0 or mark > 100:
print("Please enter a valid mark between 0 and 100.")
return
marks.append(mark)
# Calculating total marks and percentage
total_marks = sum(marks)
percentage = (total_marks / 600) * 100 # Assuming each subject is out of 100
# Determining the grade
if percentage >= 90:
grade = 'A+'
elif percentage >= 80:
grade = 'A'
elif percentage >= 70:
grade = 'B'
elif percentage >= 60:
grade = 'C'
elif percentage >= 50:
grade = 'D'
else:
grade = 'F'
# Printing the report card
print("\nReport Card:")
print(f"Total Marks: {total_marks}")
print(f"Percentage: {percentage:.2f}%")
print(f"Grade: {grade}")
except ValueError:
print("Invalid input. Please enter numeric values.")
# Call the function
generate_report_card()
Outputs 1. Enter marks for subject 1: 91
Enter marks for subject 2: 85
Enter marks for subject 3: 73
Enter marks for subject 4: 65
Enter marks for subject 5: 59
Enter marks for subject 6: 48
Report Card:
Total Marks: 421.0
Percentage: 70.17%
Grade: B
2. Enter marks for subject 1: 100
Enter marks for subject 2: 100
Enter marks for subject 3: 100
Enter marks for subject 4: 100
Enter marks for subject 5: 100
Enter marks for subject 6: 98
Report Card:
Total Marks: 598.0
Percentage: 99.67%
Grade: A+
8. def create_and_display_data_structures():
# Creating a list
my_list = [28, 29, 30, 40, 69]
# Creating a tuple
my_tuple = ('hello', 'my', 'friend')
# Creating a dictionary
my_dict = {
'name': 'Nikunj',
'age': 15,
'city': 'Mangalore'}
# Displaying the list
print("List:")
print(my_list)
# Displaying the tuple
print("\nTuple:")
print(my_tuple)
# Displaying the dictionary
print("\nDictionary:")
for key, value in my_dict.items():
print(f"{key}: {value}")
# Call the function
create_and_display_data_structures()
Output List:
[28, 29, 30, 40, 69]
Tuple:
('hello', 'my', 'friend')
Dictionary:
name: Nikunj
age: 15
city: Mangalore
9. # Initialize an empty list
my_list = []
# 1. Using append() to add elements
my_list.append(1)
my_list.append(2)
print("After append():", my_list)
# 2. Using insert() to add elements at specific positions
my_list.insert(1, 'inserted')
print("After insert():", my_list)
# 3. Using extend() to add multiple elements
my_list.extend([3, 4, 5])
print("After extend():", my_list)
Output After append(): [1, 2]
After insert(): [1, 'inserted', 2]
After extend(): [1, 'inserted', 2, 3, 4, 5]
10. # Initialize the list
my_list = [1, 2, 3, 4, 5, 6, 7]
# 1. Using remove() to remove a specific element by value
my_list.remove(4)
print("After remove(4):", my_list)
# 2. Using pop() to remove an element by index and return it
removed_element = my_list.pop(2) # Removes the element at index 2
print("After pop(2):", my_list)
print("Removed element:", removed_element)
# 3. Using pop() without an index to remove and return the last element
last_element = my_list.pop()
print("After pop() (last element removed):", my_list)
print("Removed last element:", last_element)
Output After remove(4): [1, 2, 3, 5, 6, 7]
After pop(2): [1, 2, 5, 6, 7]
Removed element: 3
After pop() (last element removed): [1, 2, 5, 6]
Removed last element: 7
11. # Initialize the list
my_list = [7, 3, 1, 4, 9, 2, 8, 6, 5]
# Slicing the list
sliced_list = my_list[2:7:2] # Extract elements from index 2 to 6 with a step of 2
print("Sliced list [2:7:2]:", sliced_list)
# Sorting the list in ascending order using sort()
my_list.sort()
print("List after sort() in ascending order:", my_list)
# Sorting the list in descending order using sorted()
sorted_list_desc = sorted(my_list, reverse=True)
print("List after sorted() in descending order:", sorted_list_desc)
Outputs Sliced list [2:7:2]: [1, 9, 8]
List after sort() in ascending order: [1, 2, 3, 4, 5, 6, 7, 8, 9]
List after sorted() in descending order: [9, 8, 7, 6, 5, 4, 3, 2, 1]
12. # Initialize the list with different numbers
numbers = [15, 23, 7, 9, 30, 12, 8, 5]
# Initialize the sum variable
total_sum = 0
# Use a for loop to iterate through the list and calculate the sum
for number in numbers:
total_sum += number
# Print the final sum
print("The sum of all numbers in the list is:", total_sum)
Output The sum of all numbers in the list is: 109
13. # Print natural numbers up to 100
print("Natural numbers up to 100:")
for number in range(1, 101):
print(number, end=' ')
print() # Newline for better readability
# Print even numbers up to 100
print("Even numbers up to 100:")
for number in range(1, 101):
if number % 2 == 0:
print(number, end=' ')
print() # Newline for better readability
# Print odd numbers up to 100
print("Odd numbers up to 100:")
for number in range(1, 101):
if number % 2 != 0:
print(number, end=' ')
print() # Newline for better readability
Output Natural numbers up to 100:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
88 89 90 91 92 93 94 95 96 97 98 99 100
Even numbers up to 100:
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86
88 90 92 94 96 98 100
Odd numbers up to 100:
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87
89 91 93 95 97 99
14. # Initialize variables
current_number = 1
total_sum = 0
# Use a while loop to iterate through numbers from 1 to 100
while current_number <= 100:
total_sum += current_number
current_number += 1
# Print the final sum
print("The sum of natural numbers up to 100 is:", total_sum)
Output The sum of natural numbers up to 100 is: 5050
15. # Initialize the list
original_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Create an empty list to store the reversed elements
reversed_list = []
# Use a for loop to iterate through the original list in reverse order
for i in range(len(original_list) - 1, -1, -1):
reversed_list.append(original_list[i])
# Print the reversed list
print("Original list:", original_list)
print("Reversed list:", reversed_list)
Output Original list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Reversed list: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]