Question

Python Given a list like myList = [1, 2, 3, 4]. Your task is to find...

Python

Given a list like myList = [1, 2, 3, 4]. Your task is to find sum of each number with another number. For example, 1+2+1+3+1+4,2+3,2+4,3+4. Use for loop to accomplish this task.

Return Output:

1 + 2
1 + 3
1 + 4
2 + 3
2 + 4
3 + 4

The sum value: 30
0 0
Add a comment Improve this question Transcribed image text
Answer #1
myList = [1, 2, 3, 4]

total = 0
for i in range(len(myList)):
    for j in range(i + 1, len(myList)):
        print(myList[i], '+', myList[j])
        total += (myList[i] + myList[j])
print("\nThe sum value:", total)

1 2 1 +3 1 + 4 2 3 2+ 4 3 +4 The sum value: 30 Process finished with exit code 0

Add a comment
Know the answer?
Add Answer to:
Python Given a list like myList = [1, 2, 3, 4]. Your task is to find...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider the list myList: myList = ["a", "America", "1", [5,3,9], "3", ["Mercy"]] #1 1) The Python...

    Consider the list myList: myList = ["a", "America", "1", [5,3,9], "3", ["Mercy"]] #1 1) The Python statement __________________ returns a #2 2) The Python statement __________________ returns America #3 3) The Python statement __________________ returns Ame #4 The type of the third element is _______. #6 The Python statement __________________ returns the following: ['Mercy'] #7 The python statement temp = _______ converts the third element to integer and assigns the result to temp. #8 Consider the fourth element of myList...

  • require python 5. You have the following list stored in variable myList [ 3, 7, -1,...

    require python 5. You have the following list stored in variable myList [ 3, 7, -1, 2, -10, 6, 8) i. Write a program that creates a new list, with the same numbers as mylist, apart from any negative numbers, which should be removed. Although you are using mylist as an example, it should work for any list of numbers. ii. Now adjust your program so that instead of deleting them, any negative numbers are replaced with a zero.

  • Sc Python 1 Task 2 3 Consider a binary tree of N vertices 4 such that children of node K are 2* K + 1. Vertex 1...

    Sc Python 1 Task 2 3 Consider a binary tree of N vertices 4 such that children of node K are 2* K + 1. Vertex 1 is the root Kand 2 of the tree and each node has an integer value associated with it. Such a tree may be represented as an array of N integers by writing down values from consecutive nodes For example, the tree below 8 Test might be represented as an array o A node...

  • Part 1: Using Idle Write a Python Script that Does the Following 1. At the top...

    Part 1: Using Idle Write a Python Script that Does the Following 1. At the top of your program, import the math library as in from math import * to make the functions in the math module available. Create a variable and assign into it a constant positive integer number of your choice. The number should be at most 10. 1 Suppose we call this variable x for this writeup document Try something like x = 4 2. Create another...

  • In Python: # Q1: create a list that has the following items. # {1, 2 ,...

    In Python: # Q1: create a list that has the following items. # {1, 2 , 3 , 4 , 5} # you need to start with an empty list. and then add the above components # one by one. MyList = [ #create an empty list, 1 point MyList.-----(1) #add 1 to the list, 1 point MyList.-----(2) MyList.-----(3) MyList.-----(4) MyList.-----(5) # now find the length of the list. print(---(MyList)) # 1 point # select the starting and ending point...

  • python List1=[ ["a", 1], ["b", 3], ["c", 3], ["d", 2], ["e", 1], ["f", 4], ["g", 2]]...

    python List1=[ ["a", 1], ["b", 3], ["c", 3], ["d", 2], ["e", 1], ["f", 4], ["g", 2]] 1 def function(Letter,List),example, function("c",List) return 3 2 def function(word,List),example, function("a",List) return 1, function("be",List) return 4, function("bee",[b,5],[e,20]) return 45 Dictionary = ["a","bee","ad","ae"] 3 ["a","b","y","e"] return[["a",1],["ae",2],["bee",5]] 4 input a list, find largest value words can spell, return words and value dont using loop, using recursion for those code

  • Task 1: String with Loop • Write a program that reads a sentence and use a...

    Task 1: String with Loop • Write a program that reads a sentence and use a loop that counts how many elements in a string that are equal to ‘t’. Task 2: List with Function • Write a function fill that fills all elements of a list with a given value. • For example: o The call fill(scores, 13) should fill all elements of the list scores with the value 13. • Write a demo program to show how this...

  • Exercise 1 - Create a List to Sum Values Write a python script that sums the...

    Exercise 1 - Create a List to Sum Values Write a python script that sums the values in a list called add_list that contains the following values: (10, 2, -4, 8). Make sure you include a for loop when iterating through the list to sum the values. Take screenshots of the code and output. Exercise 2 – Create a Dictionary Write a python script that prints a dictionary where the key numbers are 1, 2, 3, 4, and 5 and...

  • 1. use python List to Dictionary Write a function that has three parameters: a list of...

    1. use python List to Dictionary Write a function that has three parameters: a list of unsorted numbers with no duplicates, a start number, and an end number. This function should return a dictionary with all integers between the start and end number (inclusive) as the keys and their respective indices in the list as the value. If the integer is not in the list, the corresponding value would be None. Example unsorted list: [2,1,10,0,4,3] two numbers: 3, 10 returned...

  • In python Write a function named printList to print the elements of a list with labels...

    In python Write a function named printList to print the elements of a list with labels showing each element's order in the list. The function header should like this: def printList(aList): And here is an example run: > > > myList = [92.5, 127.1, 9, 104.2, 78.4] > > > printList(myList) 0 92.5 1 127.1 2 9 3 104.2 4 78.4 Test your function from the Python shell window on various types of lists with varying lengths.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT