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
using python 3 2. Write a python program that finds the numbers that are divisible by...
using python 3 2. Write a python program that finds the numbers that are divisible by both 2 and 7 but not 70, or that are divisible by 57 between 1 and 1000. 3. Write a function called YesNo that receives as input each of the numbers between 1 and 1000 and returns True if the number is divisible by both 2 and 7 but not 70, or it is divisible by 57. Otherwise it returns False. 4. In your...
using python 3 2. Write a python program that finds the numbers that are divisible by...
using python 3 2. Write a python program that finds the numbers that are divisible by both 2 and 7 but not 70, or that are divisible by 57 between 1 and 1000. 3. Write a function called YesNo that receives as input each of the numbers between 1 and 1000 and returns True if the number is divisible by both 2 and 7 but not 70, or it is divisible by 57. Otherwise it returns False. 4. In your...
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,
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……..
In Python please:  Number the Lines in a File: Create a program that adds line numbers to...
In Python please:  Number the Lines in a File: Create a program that adds line numbers to a file. The name of the input file will be read from the user, as will the name of the new file that your program will create. Each line in the output file should begin with the line number, followed by a colon and a space, followed by the line from the input file.
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...
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.
Write a program in Python to print all possible combinations of phone numbers. The length of...
Write a program in Python to print all possible combinations of phone numbers. The length of the number will be given. Also 3 digits will be given, which can not be used. No two consecutive digits can be same. A number containing 4 would always have 4 in the beginning.
Using Python, generate 100 random numbers ranging from 1 to 5, then plot them using matplotlib...
Using Python, generate 100 random numbers ranging from 1 to 5, then plot them using matplotlib or Seaborn as a histogram
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT