Starting Out with Python (3rd Edition)
3rd Edition
ISBN: 9780133582734
Author: Tony Gaddis
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Question
Chapter 7, Problem 4AW
Program Plan Intro
Sum of values in the list:
- • In Python, to sum the values in a given list, a “for” loop is employed.
- • A “for” loop can be used to iterate and process each data elements using the list indexing and compute the sum of the list.
Algorithm to compute the sum of the values in a given list is as follows:
- • Create a list of integers and declare a variable named “listIntegers” to refer the list.
- • Declare a variable named “totalSum” and initialize it to zero.
- • Declare a variable named “index” and initialize it to zero.
- • Use a “for” loop to iterate and process through the list from starting index to the last index.
- • Every time the “for” loop processes an “index”, add the index to the “totalSum”.
- • When the “for” loop ends, use a “print()” to display the “totalSum” which gives the total value of the list.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
python
LAB: Subtracting list elements from max
When analyzing data sets, such as data for human heights or for human weights, a common step is to adjust the data. This can be done by normalizing to values between 0 and 1, or throwing away outliers.
Write a program that adjusts a list of values by subtracting each value from the maximum value in the list. The input begins with an integer indicating the number of integers that follow.
//
Lab Goal : This lab was designed to teach you more about list processing and algorithms.Lab Description : Write a program that will search a list to find the first odd number. If an odd number is found, then find the first even number following the odd number. Return the distance between the first odd number and the LAST even number. Return -1 if no odd numbers are found or there are no even numbers following an odd number
Chapter 7 Solutions
Starting Out with Python (3rd Edition)
Ch. 7.2 - What will the following code display? numbers =...Ch. 7.2 - Prob. 2CPCh. 7.2 - Prob. 3CPCh. 7.2 - Prob. 4CPCh. 7.2 - Prob. 5CPCh. 7.2 - Prob. 6CPCh. 7.2 - Prob. 7CPCh. 7.2 - Prob. 8CPCh. 7.3 - Prob. 9CPCh. 7.3 - Prob. 10CP
Ch. 7.3 - Prob. 11CPCh. 7.3 - Prob. 12CPCh. 7.3 - Prob. 13CPCh. 7.4 - What will the following code display? names =...Ch. 7.5 - Prob. 15CPCh. 7.5 - Prob. 16CPCh. 7.5 - Prob. 17CPCh. 7.5 - Prob. 18CPCh. 7.8 - Prob. 19CPCh. 7.8 - Prob. 20CPCh. 7.8 - Write a set of nested loops that display the...Ch. 7.9 - Prob. 22CPCh. 7.9 - Prob. 23CPCh. 7.9 - Prob. 24CPCh. 7.9 - Prob. 25CPCh. 7 - This term refers to an individual item in a list....Ch. 7 - This is a number that identifies an item in a...Ch. 7 - Prob. 3MCCh. 7 - This is the last index in a list. a. 1 b. 99 c. 0...Ch. 7 - This will happen if you try to use an index that...Ch. 7 - This function returns the length of a list. a....Ch. 7 - When the operator's left operand is a list and...Ch. 7 - This list method adds an item to the end of an...Ch. 7 - This removes an item at a specific index in a...Ch. 7 - Prob. 10MCCh. 7 - If you call the index method to locate an item in...Ch. 7 - Prob. 12MCCh. 7 - This file object method returns a list containing...Ch. 7 - Which of the following statement creates a tuple?...Ch. 7 - Prob. 1TFCh. 7 - Prob. 2TFCh. 7 - Prob. 3TFCh. 7 - Prob. 4TFCh. 7 - A file object's writelines method automatically...Ch. 7 - You can use the + operator to concatenate two...Ch. 7 - Prob. 7TFCh. 7 - You can remove an element from a tuple by calling...Ch. 7 - Prob. 1SACh. 7 - Prob. 2SACh. 7 - What will the following code display? values = [2,...Ch. 7 - Prob. 4SACh. 7 - Prob. 5SACh. 7 - Prob. 6SACh. 7 - Prob. 1AWCh. 7 - Prob. 2AWCh. 7 - Prob. 3AWCh. 7 - Prob. 4AWCh. 7 - Write a function that accepts a list as an...Ch. 7 - Prob. 6AWCh. 7 - Prob. 7AWCh. 7 - Prob. 8AWCh. 7 - Total Sales Design a program that asks the user to...Ch. 7 - Prob. 2PECh. 7 - Rainfall Statistics Design a program that lets the...Ch. 7 - Prob. 4PECh. 7 - Prob. 5PECh. 7 - Larger Than n In a program, write a function that...Ch. 7 - Drivers License Exam The local driver s license...Ch. 7 - Name Search If you have downloaded the source code...Ch. 7 - Prob. 9PECh. 7 - World Series Champions If you have downloaded the...Ch. 7 - Prob. 11PE
Knowledge Booster
Similar questions
- 1. Lottery Number Generator:Design a program that generates a seven-digit lottery number. The program should generate seven random numbers, each in the range of 0 through 9, and assign each number to a list element. (Random numbers were discussed in Chapter 6.) Then write another loop that displays the contents of the list python coding pleasearrow_forwardCode the following write a For loop that iterates through a list of products (any number of products) that are sold in a store and prints them. [examples: fan, fridge, freezer, washer, etc.]. b) copy the code from a) and use the same loop to add more products(any)to the list. You must use list methods to add. c) Copy the code from c), and search for a specific product (any), if the product is found the loop is terminated. Make sure to use else in the for loop. d) Create an empty dictionary, then add 4 entries (the key is a word, the value is word’s frequency in a book). [example: {‘street’: 23}]. e) Create a for loop to access the entries from d) and print the keys and values. f) Copy the code from e) and search for a specific key in the dictionary and prints its value. The program prints a message “ the {key} is not found”, if the key does not exist. ..?arrow_forwardstatistics.py: Write a program that reads a list of integers from the user, until they enter -12345 (the -12345 should not be considered part of the list). Then print the mean, median, and standard deviation of the list. The mean is the average of the numbers. The median is the “middle” value if n is odd, or the average of the two “middle” values if n is even. The standard deviation is the average amount of variability For example: $ python3 statistics.py154102-12345mean: 7.75median: 7standard deviation: 5.909032633745278arrow_forward
- C Program using while loopWrite a program that accepts a positive integer n. The program should then output a list of all whole numbers from 1 to narrow_forwardCreate a hand of cards using python code: - 52 cards are in the deck To create a hand of cards, first prompt the user for the number of cards to deal between 2 and 5. You must check the users input for the correct value. If the value is incorrect, tell the user, it is incorrect and ask for a new number. Take the number of cards entered by the user and from the deck of cards place those number of cards in the list called hand. Hint: use the pop command to take from the shuffled deck of cards and place into the hand list. Always make sure you have cards left in the deck of cards when creating a hand.arrow_forwardFind an even number Complete the following function according to its docstring. Use a for loop. The lists we test will not all be shown; instead, some will be described in English. If you fail any test cases, you will-need to read the description and make your own test. 1 def first_even(items: list[int]) -> int: """Return the first even number from items. Return-1 if items contains 3. no even numbers. 4 5. >>> first_even([5, 8, 3, 2]) 8 7. >>> first_even([7, 1]) -1arrow_forward
- python question: Write a code that takes a sentence as an argument and places its words in a list of words in alphabetical order If the sentence is: "Am I good" The function returns: ['Am', 'I', 'good']arrow_forwardPython Language - Bad Luck Numbersarrow_forwardCS102 Programming language is C++.arrow_forward
- Lab Exercises: Write a program to find all numbers between 24 and 1337 (both included) that are divisible by 2, but are not multiples of 9. The result obtained should be printed in a list called my_numbers. 1. Write a program that will act as a calculator. The user will input the whole equation in this form " 5+5 " and the program needs to calculate the answer. The allowed operators are "+ - */ %" 2. 3. Write a program that asks the user for a word, the program needs to print if the word is a palindrome or not. Note: Palindrome is a word, phrase, or sequence that reads the same backwards as forwards, e.g. madam Write a program that takes a list of numbers(separated by comma) from the user E.g. [1,2,1,4,1] and creates a similar dictionary to this: 4. { "1":{ "Location":0, 2, 4, "Number of Occ":3 ). "2":{ "Location":1, "Number of Oc":1 ). "4":{ "Location":3, "Number of Oc":1 }arrow_forwardPlease do this in C# (C Sharp) Kilogram Converter In a medical office, employees must use weight measurements in kilograms and pounds. Create a program (console based ) that displays a table of kilograms 0-20 and the equivalent weight in pounds. The application should use a loop to display weights in a list box. Use this conversion: 1 kg = 2.2 lbs Calculating Factorials The notation n! represents to the factorial of the nonnegative integer n. The factorial of n is the product of all the nonnegative integers from 1 through n. For example, 3! = 1 x 2 x 3 = 6 9! = 1 x 2 x 3 x 4 x 5 x 6 x 7 x 8 x 9 = 362,880 Create a program (console based ) that lets the user enter a nonnegative integer and then uses a loop to calculate the factorial of that number. Display the factorial on a label or in a message box.arrow_forwardWrite a nested if statement that will determine the student's final letter grade. Display the numeric and letter grades for the Final average and final exam. Write the nested code in python. import randomdef main(): # main function definition aGrades=[] # empty list to store Grades for i in range(7): # loop to read 7 inputs print("Input scores ",i+1," : ", end="") # a message to enter grade n=int(input()) # input grade aGrades.append(n) # add grade into list print("Grade list before randomize: ",aGrades) # print list random.shuffle(aGrades) # randomize list numExams=len(aGrades)-1 # compute numExams FinalExam=aGrades[-1] # store final grade TotalPoints=sum(aGrades)-aGrades[-1] # compute TotalPoints TestAverage=TotalPoints/numExams # compute TestAverage FinalAverage=TestAverage*.6 + FinalExam*.4 # compute FinalAverage print("Grade list after randomize is: ",aGrades) # print list print("Test Average =…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education