+91 81900 98289

Add Two Numbers

This Add Two Numbers lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.

The + operator adds numbers. Text that looks like a number is still text until int or float converts it.

Adding two strings joins them. That is a different operation.

Example 1: Start here

int("3.5") fails. Use float when the text may contain a decimal point.

a = int("20") # text read as a whole number b = int("4") # text read as a whole number print(a + b)

Example 2: Add a list of fees

sum is the direct way to add every number.

print(sum([25000, 18000, 12000])) # add the numbers together

Example 3: Start from zero

The loop version shows the running total.

total = 0 # a whole number for fee in [25000, 18000]: # repeat the indented lines once per value total += fee # update the stored value print(total)

Example 4: Skip the missing ones

Ignore None so a missing fee does not break the addition.

fees = [25000, None, 18000] # a list; the values stay in this order total = sum(fee for fee in fees if fee is not None) # the total print(total)
int("3.5") fails. Use float when the text may contain a decimal point.
Call Now 81900 98289