Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k 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!

Browse Categories

...