0% found this document useful (0 votes)
39 views4 pages

Practice PT2 IX

The document contains multiple Python programs that allow users to input a list of numbers or letters and perform various operations such as calculating the sum, average, product, and frequency of specific values. It includes functionalities to find the largest, smallest, second largest, and second smallest numbers, as well as to separate even and odd numbers. Additionally, it provides a way to count positive, negative, and zero numbers, and to track the frequency of specific letters and numbers.

Uploaded by

Neha Makhija
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views4 pages

Practice PT2 IX

The document contains multiple Python programs that allow users to input a list of numbers or letters and perform various operations such as calculating the sum, average, product, and frequency of specific values. It includes functionalities to find the largest, smallest, second largest, and second smallest numbers, as well as to separate even and odd numbers. Additionally, it provides a way to count positive, negative, and zero numbers, and to track the frequency of specific letters and numbers.

Uploaded by

Neha Makhija
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Q.

Program to input a list of numbers from the user up to a user-specified number


# Get the maximum number of inputs from the user
max_count = int(input("Enter the number of elements you want to input: "))

# Initialize an empty list to store the numbers


numbers = []

# Loop to input numbers


for i in range(max_count):
number = float(input(f"Enter number {i + 1}: "))
[Link](number)

# Display the entered list


print("The numbers you entered are:", numbers)

Q. Program to input a list of numbers until a user-specified total number of elements is


reached and calculate the sum of all the entered numbers

# Get the total number of elements to be entered


total_elements = int(input("Enter the total number of elements you want to input: "))

# Initialize an empty list to store the numbers


numbers = []

# Initialize a variable to store the sum of numbers


sum_of_numbers = 0

# Loop to input numbers


for i in range(total_elements):
number = float(input(f"Enter number {i + 1}: "))
[Link](number)
sum_of_numbers += number # Add the entered number to the sum

# Display the entered list and their sum


print("\nThe numbers you entered are:", numbers)
print("The sum of the entered numbers is:", sum_of_numbers)

Q. # Program to input a list of numbers and find the largest and smallest number

# Get the total number of elements to be entered


total_elements = int(input("Enter the total number of elements you want to input: "))

# Initialize an empty list to store the numbers


numbers = []

# Loop to input numbers


for i in range(total_elements):
number = float(input(f"Enter number {i + 1}: "))
[Link](number)
# Find the largest and smallest numbers
largest_number = max(numbers)
smallest_number = min(numbers)

# Display the results


print("\nThe numbers you entered are:", numbers)
print("The largest number is:", largest_number)
print("The smallest number is:", smallest_number)

Q. Program to calculate average and product of a list of numbers


# Get the total number of elements to be entered
total_elements = int(input("Enter the total number of elements you want to input: "))

# Initialize an empty list to store the numbers


numbers = []

# Initialize variables for sum and product


sum_of_numbers = 0
product_of_numbers = 1

# Loop to input numbers


for i in range(total_elements):
number = float(input(f"Enter number {i + 1}: "))
[Link](number)
sum_of_numbers += number
product_of_numbers *= number

# Calculate the average


average = sum_of_numbers / total_elements

# Display the results


print("\nThe numbers you entered are:", numbers)
print("The average is:", average)
print("The product is:", product_of_numbers)

# Program to count positive, negative, and zero numbers


# Get the total number of elements to be entered
total_elements = int(input("Enter the total number of elements you want to input: "))

# Initialize counters
positive_count = 0
negative_count = 0
zero_count = 0

# Loop to input numbers


for i in range(total_elements):
number = float(input(f"Enter number {i + 1}: "))
if number > 0:
positive_count += 1
elif number < 0:
negative_count += 1
else:
zero_count += 1

# Display the counts


print("\nPositive numbers:", positive_count)
print("Negative numbers:", negative_count)
print("Zeroes:", zero_count)

# Program to find the second largest and second smallest numbers

# Get the total number of elements to be entered


total_elements = int(input("Enter the total number of elements you want to input: "))

# Input the numbers into a list


numbers = []
for i in range(total_elements):
number = float(input(f"Enter number {i + 1}: "))
[Link](number)

# Sort the list


[Link]()

# Find second largest and second smallest


if total_elements >= 2:
second_largest = numbers[-2]
second_smallest = numbers[1]
else:
second_largest = second_smallest = None # Not enough elements

# Display the results


print("\nThe numbers you entered are:", numbers)
print("The second largest number is:", second_largest)
print("The second smallest number is:", second_smallest)

Q. Program to separate even and odd numbers from a list

# Get the total number of elements to be entered


total_elements = int(input("Enter the total number of elements you want to input: "))

# Input the numbers into a list


numbers = []
even_numbers = []
odd_numbers = []

for i in range(total_elements):
number = int(input(f"Enter number {i + 1}: "))
[Link](number)
if number % 2 == 0:
even_numbers.append(number)
else:
odd_numbers.append(number)

# Display the results


print("\nThe numbers you entered are:", numbers)
print("Even numbers:", even_numbers)
print("Odd numbers:", odd_numbers)

Q Write a program to input a list of letters (alphabets from the user and display the
frequency of Alphabet ‘D”?
# Get the total number of letters to be entered
total_letters = int(input("Enter the total number of letters you want to input: "))

# Initialize an empty list to store the letters


letters = []

# Loop to input letters


for i in range(total_letters):
letter = input(f"Enter letter {i + 1}: ").strip().upper() # Convert to uppercase for uniformity
[Link](letter)

# Count the frequency of 'D'


frequency_of_d = [Link]('D')

# Display the results


print("\nThe letters you entered are:", letters)
print("The frequency of the letter 'D' is:", frequency_of_d)

# Program to input a list of numbers and display the frequency of the number 10

# Get the total number of elements to be entered


total_elements = int(input("Enter the total number of numbers you want to input: "))

# Initialize an empty list to store the numbers


numbers = []

# Loop to input numbers


for i in range(total_elements):
number = int(input(f"Enter number {i + 1}: "))
[Link](number)

# Count the frequency of 10


frequency_of_ten = [Link](10)

# Display the results


print("\nThe numbers you entered are:", numbers)
print("The frequency of the number 10 is:", frequency_of_ten)

You might also like