Question

In: Computer Science

Using the Python program: a/ Write a program that adds all numbers from 1 to 100...

Using the Python program:

a/ Write a program that adds all numbers from 1 to 100 to a list (in order). Then remove the multiples of 3 from the list. Print the remaining values.

b/ Write a program that initializes a list with ten random integers from 1 to 100. Then remove the multiples of 3 and 5 from the list. Print the remaining values.

Please show your work and explain. Thanks

Solutions

Expert Solution

SOLUTION

a)

list =[i for i in range(1,101)] #creating a list containing numbers from 1 to 100
print(list) #printing the created list

#for loop to traverse the list and remove multiples of 3
for i in list:
  if(i%3 == 0): #condition to check if the element in the list is a multiple of 3 or not
      list.remove(i) #if yes we remove that element
print("\nList after removing multiples of 3:")
print(list)

OUTPUT

b)

import random #importing random package
list = [] #declaring empty list
for i in range(0,10): #for loop to fill list with random numbers between 1 and 100
    n = random.randint(1,100)
    list.append(n)
print(list)

for i in list: #for loop to remove multiples of 3 and 5 from the list
  if(i%3 == 0 or i%5 == 0):
      list.remove(i)
print("\nList after removing multiples of 3 and 5:")
print(list)

OUTPUT


Related Solutions

Java Write a program that displays all the numbers from 100 to 200 that are divisible...
Java Write a program that displays all the numbers from 100 to 200 that are divisible by 5 or 6, but not both Make sure all instructions and outputs for the user are explicit
Write a Python program that calls a function to sum all the numbers in a list...
Write a Python program that calls a function to sum all the numbers in a list and returns the result to the caller. The main program creates a list (with hard-coded or user input) and passes the list as an argument to the function. You may not use the built-in function, sum. The program calls a second function to multiply all the numbers in a list passed to it by main and returns the product back to the caller. List...
Write a program in Python that will print first 100 numbers of the following series: 0,...
Write a program in Python that will print first 100 numbers of the following series: 0, 1, 1, 2, 3, 5, 8……..
JAVA Write a program to sum the numbers from 1 to 100 that are divisible by...
JAVA Write a program to sum the numbers from 1 to 100 that are divisible by 7, and compute the average of those numbers, print both the sum and the average with appropriate messages to the screen. Run the program. Capture the console output. Put the program code and console output at the end of your text file,
Task 1 Write a program that adds the three numbers stored in data registers at 0x20,...
Task 1 Write a program that adds the three numbers stored in data registers at 0x20, 0x30, and 0x40 and places the sum in data register at 0x50 task 4 Modify the program in Task1, so the program will run in infinite loop by using these following functions: GOTO function BRA function CALL function Simulate your program in PIC18 IDE Simulator and attach a screenshot of your simulation while the program is running.
Given n. Write a program in PYTHON to Generate all numbers with number of digits equal...
Given n. Write a program in PYTHON to Generate all numbers with number of digits equal to n, such that the digit to the right is greater than the left digit (ai+1 > ai). E.g. if n=3 (123,124,125,……129,234,…..789)
"PYTHON" Write some code " USING PYTHON" to keep reading numbers from the user until the...
"PYTHON" Write some code " USING PYTHON" to keep reading numbers from the user until the users enters a negative number. The program then prints out: a) the sum of all the numbers b) the average of all the numbers c) the max of the numbers d) the min of the numbers Note we did not cover lists (array) so you will not use them in this problem. Finally, ask the user for their name, then print their name as...
Write a Python program which takes a set of positive numbers from the input and returns...
Write a Python program which takes a set of positive numbers from the input and returns the sum of the prime numbers in the given set. The sequence will be ended with a negative number.
Write a Python program which adds up columns and rows of given table as shown in...
Write a Python program which adds up columns and rows of given table as shown in the specified figure. Example test case (the four lines below “Input cell value” are input from user, and the five lines below “Results:” are the output of the program.): Input number of rows/columns (0 to exit) 4 Input cell value: 25 69 51 26 68 35 29 54 54 57 45 63 61 68 47 59 Result:             25   69   51   26 171...
Write a Python program which adds up columns and rows of given table as shown in...
Write a Python program which adds up columns and rows of given table as shown in the specified figure. Example test case (the four lines below “Input cell value” are input from user, and the five lines below “Results:” are the output of the program.): Input number of rows/columns (0 to exit) 4 Input cell value: 25 69 51 26 68 35 29 54 54 57 45 63 61 68 47 59 Result: 25 69 51 26 171 68 35...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT