Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Assignment 8 (python), Assignments of Computer Science

Hello this is a assignment from the professor , everything is correct.

Typology: Assignments

2023/2024

Uploaded on 11/04/2024

paresh-kumar-2
paresh-kumar-2 🇨🇦

1 document

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
# Q1. Write a Python program to find the maximum value in the list [3,
5, 1, 8, 2].
# Defining the list
numbers = [3, 5, 1, 8, 2]
# Finding the maximum value
max_value = max(numbers)
# Outputing the maximum value
print("The maximum value in the list is:", max_value)
The maximum value in the list is: 8
# Q2. Write a Python program to find the minimum value in the list
[10, 20, -2, 5, 30, 15].
# Defining the list
numbers = [10, 20, -2, 5, 30, 15]
# Finding the minimum value
min_value = min(numbers)
# Outputing the minimum value
print("The minimum value in the list is:", min_value)
The minimum value in the list is: -2
# Q3. Write a Python program to calculate the sum of the numbers in
the list [1, 2, 3, 4, 5].
# Defining the list
numbers = [1, 2, 3, 4, 5]
# Calculating the sum of the numbers
total_sum = sum(numbers)
# Outputing the sum
print("The sum of the numbers in the list is:", total_sum)
The sum of the numbers in the list is: 15
# Q4. Write a Python program to sort the list [56, 12, 3, 89, 42] in
ascending and descending order.
# Defining the list
numbers = [56, 12, 3, 89, 42]
# Sorting the list in ascending order
ascending_order = sorted(numbers)
pf3

Partial preview of the text

Download Assignment 8 (python) and more Assignments Computer Science in PDF only on Docsity!

Q1. Write a Python program to find the maximum value in the list [3,

5, 1, 8, 2].

Defining the list

numbers = [ 3 , 5 , 1 , 8 , 2 ]

Finding the maximum value

max_value = max(numbers)

Outputing the maximum value

print("The maximum value in the list is:", max_value) The maximum value in the list is: 8

Q2. Write a Python program to find the minimum value in the list

[10, 20, -2, 5, 30, 15].

Defining the list

numbers = [ 10 , 20 , - 2 , 5 , 30 , 15 ]

Finding the minimum value

min_value = min(numbers)

Outputing the minimum value

print("The minimum value in the list is:", min_value) The minimum value in the list is: -

Q3. Write a Python program to calculate the sum of the numbers in

the list [1, 2, 3, 4, 5].

Defining the list

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

Calculating the sum of the numbers

total_sum = sum(numbers)

Outputing the sum

print("The sum of the numbers in the list is:", total_sum) The sum of the numbers in the list is: 15

Q4. Write a Python program to sort the list [56, 12, 3, 89, 42] in

ascending and descending order.

Defining the list

numbers = [ 56 , 12 , 3 , 89 , 42 ]

Sorting the list in ascending order

ascending_order = sorted(numbers)

Sorting the list in descending order

descending_order = sorted(numbers, reverse=True)

Outputing the sorted lists

print("List in ascending order:", ascending_order) print("List in descending order:", descending_order) List in ascending order: [3, 12, 42, 56, 89] List in descending order: [89, 56, 42, 12, 3]

Q5. Write a Python program to calculate the absolute value of the

number -25.

Defining the number

number = - 25

Calculating the absolute value

absolute_value = abs(number)

Outputing the absolute value

print("The absolute value of the number is:", absolute_value) The absolute value of the number is: 25

Q6. Write a Python program to round the number 7.678 to the nearest

integer.

Defining the number

number = 7.

Rounding the number to the nearest integer

rounded_number = round(number)

Outputing the rounded number

print("The rounded number is:", rounded_number) The rounded number is: 8

Q7. Write a Python program to raise 5 to the power of 3.

Defining the base and exponent

base = 5 exponent = 3

Calculating 5 raised to the power of 3

result = pow(base, exponent)

Outputing the result

print("5 raised to the power of 3 is:", result) 5 raised to the power of 3 is: 125