Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Q1: Solve the above question using a for loop. Write two programs, one using continue statement and one without using continue. In [ ]: nums

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Q1: Solve the above question using a for loop. Write two programs, one using continue statement and one without using continue. In [ ]: nums 1 ### using a for loop solution 1 2 [2, 5, 0, 4, 3, 0, 10, 0, 8, 8, 20] 3 4. 5 6 7 8 print('done') In [ ]: i ### using a for loop - solution 2 2 3 4 5 6 7 8 print('done') Q2: Write a while loop that filters all non-zero numbers from a given list and places them in a new list, called nozeros . Your code should also output the number of zeros found in the list. For example, given the list nums [2, 5, 0, 4, 3, 0, 10, 0, 8, 8, 20] , your program output should be: = nozeros: [2, 5, 4, 3, 10, 8, 8, 20] total zero entries = 3 In [ ]: 1 ## solution 2 nums = [2, 5, 0, 4, 3, 0, 10, 0, 8, 8, 20] 3 4 5 zeros = 0 # counts O's 6 7 8 9 10 11 12 13 14 15 print('nozeros= ', nozeros) 16 print('total zero entries =', zeros) Q3: Write a Python program that computes the following sum 2, vx {2,5,0,4,3,0, 10, 0, 8, 8, 20) *#0 a) using a while loop b) using a for loop Round your results to three decimal places. In [ ] : [2, 5, 0, 4, 3, 0, 10, 0, 8, 8, 20] 1 nums 2 3 y = 0 4 5 6. 7 8 9 10 print('sum / format(y, '0.3f')) Q4: Modify your code for the above problem to calculate the sum of reciprocals as well as the number of non-zero entries included in the sum calculation. For the given list nums = [2, 5, 0, 4, 3, 0, 10, 0, 8, 8, 20] your output should be: sum = 1.683 total zero entries = 3 total non-zero entries = 8 In [ ] : nums 1 [2, 5, 0, 4, 3, 0, 10, 0, 8, 8, 20] 2 3 4 5 6 7 8 9 10 11 12 13 print('sum = format(y, '0.3f')). 14 print('total zero entries =', zeros) 15 print('total non-zero entries =', totalnums 16 17 I zeros)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Neo4j Data Modeling

Authors: Steve Hoberman ,David Fauth

1st Edition

1634621913, 978-1634621915

More Books

Students also viewed these Databases questions