0% found this document useful (0 votes)
4 views

python list

The document contains several Python programs that perform various operations on lists, including summing all numbers, summing only positive numbers, excluding multiples of 3, summing nested lists, summing the digits of numbers, and multiplying items in a list. Each program is structured with a list of numbers, initialization of a sum or product variable, and a loop to perform the required calculations. The results are printed for each operation.

Uploaded by

pneel240909
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)
4 views

python list

The document contains several Python programs that perform various operations on lists, including summing all numbers, summing only positive numbers, excluding multiples of 3, summing nested lists, summing the digits of numbers, and multiplying items in a list. Each program is structured with a list of numbers, initialization of a sum or product variable, and a loop to perform the required calculations. The results are printed for each operation.

Uploaded by

pneel240909
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
You are on page 1/ 6

# List of numbers

numbers = [3, 7, 2, 9, 4]

# Initialize sum

total = 0

# Loop through the list and add each number to total

for num in numbers:

total += num
# Print the result

print("The sum of all items in the list is:", total)

Write a Python program to find the sum of only the positive numbers in a
given list

# List of numbers

numbers = [4, -3, 7, -1, 0, 9, -5]

# Initialize sum

positive_sum = 0

# Loop through the list and add only positive numbers

for num in numbers:

if num > 0:

positive_sum += num

# Print the result

print("The sum of positive numbers in the list is:", positive_sum)

 Write a Python program to sum all numbers in a list except those that
are multiples of 3.

# List of numbers

numbers = [5, 9, 12, 7, 4, 15, 8]

# Initialize sum
total = 0

# Loop through the list and add numbers that are not multiples of 3

for num in numbers:

if num % 3 != 0:

total += num

# Print the result

print("The sum of numbers excluding multiples of 3 is:", total)

 Write a Python program to find the sum of numbers in a nested list.

# Nested list of numbers

numbers = [[1, 2], [3, 4, 5], [6], [7, 8, 9]]

# Initialize sum

total = 0

# Loop through each sublist

for sublist in numbers:

# Loop through each number in the sublist

for num in sublist:

total += num

# Print the result

print("The sum of all numbers in the nested list is:", total)


 Write a Python program to find the sum of the digits of each element in
a list.

# List of numbers

numbers = [123, 45, 67, 890]

# Loop through each number in the list

for num in numbers:

digit_sum = 0

temp = num

# Add the digits of the number

while temp > 0:

digit = temp % 10

digit_sum += digit

temp = temp // 10

# Print the result for each number

print("Sum of digits of", num, "is", digit_sum)

Write a Python program to multiply all the items in a list.

# List of numbers

numbers = [2, 3, 4, 5]

# Initialize product

product = 1

# Loop through the list and multiply each number


for num in numbers:

product *= num

# Print the result

print("The product of all items in the list is:", product)

 Write a Python program to find the product of only the odd numbers in
a list.

# List of numbers

numbers = [2, 3, 5, 6, 7, 8, 9]

# Initialize product

product = 1

# Variable to check if there's at least one odd number

found_odd = False

# Loop through the list and multiply only odd numbers

for num in numbers:

if num % 2 != 0:

product *= num

found_odd = True

# Print the result

if found_odd:
print("The product of all odd numbers in the list is:", product)

else:

print("There are no odd numbers in the list.")

You might also like