Use app×
QUIZARD
QUIZARD
JEE MAIN 2026 Crash Course
NEET 2026 Crash Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
292 views
in Python by (178k points)
Python sum() Function

Please log in or register to answer this question.

1 Answer

0 votes
by (178k points)

The sum() function in Python is used to calculate the sum of all the values in an iterable object such as a list, tuple, or set.

The syntax of the sum() function is:

sum(iterable, start=0)
 

where,

  • iterable: specifies the iterable object (e.g., list, tuple, set) to be summed.
  • start: specifies the value to start the sum with. Default is 0.

Here are some examples of using the sum() function:

# sum of a list of numbers
my_list = [1, 2, 3, 4, 5]
result = sum(my_list)
print(result)  # output: 15

# sum of a tuple of numbers
my_tuple = (1, 2, 3, 4, 5)
result = sum(my_tuple)
print(result)  # output: 15

# sum of a set of numbers
my_set = {1, 2, 3, 4, 5}
result = sum(my_set)
print(result)  # output: 15

# sum of a list of numbers starting from 10
my_list = [1, 2, 3, 4, 5]
result = sum(my_list, 10)
print(result)  # output: 25
 

Note that the sum() function only works with numerical values (e.g., integers, floats). If you want to concatenate strings or combine lists, you should use other methods such as join() or extend().

Related questions

0 votes
1 answer
asked Aug 19, 2023 in Python by MAhmad (120 points)
0 votes
1 answer
asked Aug 18, 2023 in Python by MAhmad (120 points)
+1 vote
1 answer
asked Aug 18, 2023 in Python by MAhmad (120 points)
0 votes
1 answer
asked Aug 18, 2023 in Python by MAhmad (120 points)
0 votes
1 answer
asked Aug 18, 2023 in Python by MAhmad (120 points)

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...