Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Python by (12.7k points)

I'm truly new to programming (two days old). I've as of late run into an issue with adding up two numbers from a list. I've figured out how to come up with this program: 

list_nums = ["17", "3"]           

num1 = list_nums[0]                            

num2 = list_nums[1]                      

sum = (num1) + (num2)                        

print(sum)

The issue is that as opposed to adding up num1 with num2 (17+3=20), Python joins the two numbers (for example "173"). what would I be able to do to add up the numbers, rather than joining them?

1 Answer

0 votes
by (26.4k points)

Look at the below code:

list_nums = [17, 3]           

num1 = list_nums[0]                            

num2 = list_nums[1]                      

sum = (num1) + (num2)                        

print(sum)

In your code, 17 and 3 are strings. Just remove the doube-quotes, then will become an integer.

If want to become an expert in Python? Kindly, Join the python course fast!

Related questions

0 votes
1 answer
0 votes
1 answer
asked Feb 26, 2021 in Python by laddulakshana (12.7k points)
0 votes
1 answer
asked Dec 17, 2020 in Python by laddulakshana (12.7k points)

Browse Categories

...