Hướng dẫn how do you find the sum and average in python? - làm thế nào để bạn tìm thấy tổng và trung bình trong python?

Cải thiện bài viết

Show

    Lưu bài viết

  • Đọc
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    Đọc

    Examples:

    Input: [4, 5, 1, 2, 9, 7, 10, 8]
    Output:
    sum =  46
    average =  5.75
    
    Input: [15, 9, 55, 41, 35, 20, 62, 49]
    Output:
    sum =  286
    average =  35.75
    

    Bàn luận

    Đưa ra một danh sách. Nhiệm vụ là tìm tổng và trung bình của danh sách. Trung bình của danh sách được định nghĩa là tổng của các phần tử chia cho số lượng các phần tử.

    Example:

    Python3

    Phương pháp 1: Phương pháp ngây thơ

    Trong phương pháp này, chúng tôi sẽ lặp lại trong danh sách và sẽ thêm từng phần tử vào một số biến lưu trữ tổng của phần tử ITH và sau đó chia tổng với tổng số biến để tìm mức trung bình.

    L =

    sum =  46
    average =  5.75
    
    0
    sum =  46
    average =  5.75
    
    1
    sum =  46
    average =  5.75
    
    2
    sum =  46
    average =  5.75
    
    3
    sum =  46
    average =  5.75
    
    2
    sum =  46
    average =  5.75
    
    5__12

    sum =  46
    average =  5.75
    
    7=
    sum =  46
    average =  5.75
    
    9

    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    0
    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    1
    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    2
    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    3

    Average of the list = 35.75
    5
    Average of the list = 35.75
    6
    Average of the list = 35.75
    7
    Average of the list = 35.75
    8

    Average of the list = 35.75
    5
    Average of the list = 35.75
    6
    Average of the list = 35.75
    1
    Average of the list = 35.75
    2

    Output:

    sum =  46
    average =  5.75
    

    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    4
    sum =  46
    average =  5.75
    
    7
    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    6=
    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    8

    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    9=
    Average of the list = 35.75
    1
    Average of the list = 35.75
    2
    Average of the list = 35.75
    3
    Average of the list = 35.75
    4

    Example:

    Python3

    Phương pháp 2: Sử dụng phương thức Sum ()

    Phương thức SUM () Trả về tổng của danh sách được truyền dưới dạng đối số của nó. Sau đó, chúng tôi sẽ chia tổng cho phương thức Len () để tìm mức trung bình.

    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    0
    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    1
    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    2
    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    3

    Average of the list = 35.75
    5
    Average of the list = 35.75
    6
    Average of the list = 35.75
    7
    Average of the list = 35.75
    8

    Average of the list = 35.75
    5
    Average of the list = 35.75
    6
    Average of the list = 35.75
    1
    Average of the list = 35.75
    2

    Output:

    sum =  46
    average =  5.75
    

    Điều kiện tiên quyết: hàm sum (), hàm len (), hàm tròn (), giảm (), lambda và mean (). Đưa ra một danh sách các số, nhiệm vụ là tìm trung bình của danh sách đó. Trung bình là tổng của các phần tử chia cho số lượng các phần tử.

    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75

    Sử dụng Sum ()

    Trong Python, chúng ta có thể tìm thấy trung bình của một danh sách bằng cách sử dụng hàm sum () và len ().sum() and len() function.

    • SUM (): Sử dụng hàm SUM () Chúng ta có thể nhận được tổng của danh sách. : Using sum() function we can get the sum of the list.
    • hàm Len (): Len () được sử dụng để có độ dài hoặc số lượng phần tử trong danh sách. : len() function is used to get the length or the number of elements in a list.

    Python3

    =0 =1

    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    4
    sum =  46
    average =  5.75
    
    18=
    sum =  46
    average =  5.75
    
    9

    =9= ____1010____1022212

    sum =  46
    average =  5.75
    
    9__12

    sum =  46
    average =  5.75
    
    18=
    sum =  46
    average =  5.75
    
    20

    Average of the list = 35.75
    5
    Average of the list = 35.75
    6
    sum =  46
    average =  5.75
    
    23
    sum =  46
    average =  5.75
    
    2
    sum =  46
    average =  5.75
    
    25
    sum =  46
    average =  5.75
    
    26
    sum =  46
    average =  5.75
    
    7
    sum =  46
    average =  5.75
    
    28

    Output:

    Average of the list = 35.75

    Đầu ra

    Điều kiện tiên quyết: hàm sum (), hàm len (), hàm tròn (), giảm (), lambda và mean (). Đưa ra một danh sách các số, nhiệm vụ là tìm trung bình của danh sách đó. Trung bình là tổng của các phần tử chia cho số lượng các phần tử.

    Hướng dẫn how do you find the sum and average in python? - làm thế nào để bạn tìm thấy tổng và trung bình trong python?

    Python3

    Sử dụng Sum ()

    =0 =1

    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    4
    sum =  46
    average =  5.75
    
    18=
    sum =  46
    average =  5.75
    
    9

    =9= ____1010____1022212

    sum =  46
    average =  5.75
    
    9__12

    sum =  46
    average =  5.75
    
    18=
    sum =  46
    average =  5.75
    
    20

    Average of the list = 35.75
    5
    Average of the list = 35.75
    6
    sum =  46
    average =  5.75
    
    23
    sum =  46
    average =  5.75
    
    2
    sum =  46
    average =  5.75
    
    25
    sum =  46
    average =  5.75
    
    26
    sum =  46
    average =  5.75
    
    7
    sum =  46
    average =  5.75
    
    28

    Output:

    Average of the list = 35.75

    Đầu ra

    Điều kiện tiên quyết: hàm sum (), hàm len (), hàm tròn (), giảm (), lambda và mean (). Đưa ra một danh sách các số, nhiệm vụ là tìm trung bình của danh sách đó. Trung bình là tổng của các phần tử chia cho số lượng các phần tử.

    Python3

    Sử dụng Sum ()

    =0 =1

    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    4
    sum =  46
    average =  5.75
    
    18=
    sum =  46
    average =  5.75
    
    9

    =9= ____1010____1022212

    sum =  46
    average =  5.75
    
    9__12

    sum =  46
    average =  5.75
    
    18=
    sum =  46
    average =  5.75
    
    20

    Average of the list = 35.75
    5
    Average of the list = 35.75
    6
    sum =  46
    average =  5.75
    
    23
    sum =  46
    average =  5.75
    
    2
    sum =  46
    average =  5.75
    
    25
    sum =  46
    average =  5.75
    
    26
    sum =  46
    average =  5.75
    
    7
    sum =  46
    average =  5.75
    
    28

    Output:

    Average of the list = 35.75

    Bằng danh sách lặp lại

    Lặp lại danh sách bằng cách sử dụng cho vòng lặp và thực hiện các hoạt động trên từng yếu tố của danh sách.

    Python3

    =0 =1

    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    4
    sum =  46
    average =  5.75
    
    18=
    sum =  46
    average =  5.75
    
    9

    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    4
    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    0
    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    1
    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    2
    sum =  46
    average =  5.75
    
    25
    Average of the list = 35.75
    6
    Average of the list = 35.75
    3
    sum =  46
    average =  5.75
    
    28

    sum =  46
    average =  5.75
    
    29
    sum =  46
    average =  5.75
    
    18
    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    6=
    sum =  46
    average =  5.75
    
    33

    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    4
    sum =  46
    average =  5.75
    
    18=
    sum =  46
    average =  5.75
    
    37
    Average of the list = 35.75
    2
    Average of the list = 35.75
    3=8

    Input : [4, 5, 1, 2, 9, 7, 10, 8]
    Output : Average of the list = 5.75
    Explanation:
    Sum of the elements is 4+5+1+2+9+7+10+8 = 46
    and total number of elements is 8.
    So average is 46 / 8 = 5.75
    
    Input : [15, 9, 55, 41, 35, 20, 62, 49]
    Output : Average of the list = 35.75
    Explanation:
    Sum of the elements is 15+9+55+41+35+20+62+49 = 286
    and total number of elements is 8.
    So average is 46 / 8 = 35.75
    4=3
    sum =  46
    average =  5.75
    
    43

    =9= ____1010____1022212

    sum =  46
    average =  5.75
    
    9__12

    sum =  46
    average =  5.75
    
    18=
    sum =  46
    average =  5.75
    
    20

    Average of the list = 35.75
    5
    Average of the list = 35.75
    6
    sum =  46
    average =  5.75
    
    23
    sum =  46
    average =  5.75
    
    2
    sum =  46
    average =  5.75
    
    25
    sum =  46
    average =  5.75
    
    26
    sum =  46
    average =  5.75
    
    7
    sum =  46
    average =  5.75
    
    28

    Đầu ra

    Average of the list = 35.75