Skip to content

Basic Math for Coding — Arithmetic, Logic, and Problem-Solving With Numbers

DodaTech Updated 2026-06-22 5 min read

In this tutorial, you'll learn about Basic Math for Coding. We cover key concepts, practical examples, and best practices.

Learn basic math concepts for coding: variables, arithmetic, Boolean logic, comparisons, and patterns. No advanced math required, just beginner-friendly examples.

What You'll Learn

By the end of this tutorial, you will understand the math you actually need for coding: arithmetic operations, comparison operators, Boolean logic, and how to use math in programs.

Why It Matters

Coding is built on math. Every program you write uses numbers, comparisons, and logic. You do not need calculus or advanced algebra, but you do need these fundamentals.

Real-World Use

Durga Antivirus Pro uses Boolean logic to decide whether a file is safe. It checks multiple conditions: "Is the file signed? Is it from a trusted source? Does it match known malware patterns?" All of these are yes-or-no math questions.

Your Learning Path

flowchart LR
  A[Problem-Solving Skills] --> B[Basic Math for Coding]
  B --> C[First Python Program]
  C --> D[Coding for Absolute Beginners]
  D --> E[How to Debug]
  B --> F{You Are Here}
  style F fill:#f90,color:#fff

Arithmetic in Code

Programming uses the same arithmetic you learned in school, plus a few extras.

Operation Symbol Example Result
Addition + 5 + 3 8
Subtraction - 10 - 4 6
Multiplication * 6 * 7 42
Division / 15 / 4 3.75
Integer division // 15 // 4 3 (discards remainder)
Modulo (remainder) % 15 % 4 3 (the remainder)
Exponent ** 2 ** 3 8

Writing Arithmetic in Code

# Basic arithmetic in Python
price = 25
tax_rate = 0.08
total = price + (price * tax_rate)

print(f"Item price: ${price}")
print(f"Tax ({tax_rate*100}%): ${price * tax_rate}")
print(f"Total: ${total}")

Expected output:

Item price: $25
Tax (8.0%): $2.0
Total: $27.0

Comparison Operators

Comparisons return True or False. They are the foundation of decision-making in code.

Operator Meaning Example Result
== Equal to 5 == 5 True
!= Not equal to 5 != 3 True
> Greater than 10 > 5 True
< Less than 3 < 7 True
>= Greater or equal 5 >= 5 True
<= Less or equal 4 <= 3 False

Using Comparisons in Code

age = 18
if age >= 18:
    print("You can vote.")
else:
    print("Too young to vote.")

Expected output:

You can vote.

Boolean Logic

Boolean logic works with True and False values. It uses three main operators:

Operator What It Does Example Result
and Both must be True (5 > 3) and (2 < 4) True
or At least one is True (5 > 3) or (10 < 4) True
not Reverses the value not (5 > 3) False

Boolean Logic in Practice

# A simple antivirus check
has_signature = True
is_known_source = True
matches_malware = False

if has_signature and is_known_source and not matches_malware:
    print("File is safe.")
else:
    print("File may be dangerous.")

Expected output:

File is safe.

Working With Variables

A variable is a named container for a value. Think of it as a labeled box.

count = 10          # Integer
price = 19.99       # Float (decimal)
name = "Alice"      # String (text)
is_active = True    # Boolean (True/False)

# You can do math with variables
total_items = count + 5
discount = price * 0.10

Counting and Loops

Loops use math to repeat actions a specific number of times.

# Count from 1 to 5
for i in range(1, 6):
    print(f"Count: {i}")

Expected output:

Count: 1
Count: 2
Count: 3
Count: 4
Count: 5

Accumulating Values

# Sum the numbers from 1 to 100
total = 0
for i in range(1, 101):
    total = total + i
print(f"Sum of 1 to 100: {total}")

Expected output:

Sum of 1 to 100: 5050

Finding Patterns With Math

Math helps you detect patterns in data, which is essential for programming.

# Check if a number is even or odd
def check_number(n):
    if n % 2 == 0:
        print(f"{n} is even")
    else:
        print(f"{n} is odd")

check_number(7)
check_number(10)

Expected output:

7 is odd
10 is even

Common Mistakes Beginners Make

1. Confusing = and ==

= assigns a value. == checks if two values are equal. Using the wrong one causes bugs.

2. Integer Division Surprises

In Python 3, 15 / 4 gives 3.75. In some languages, dividing integers truncates to 3. Know your language's behavior.

3. Order of Operations

Multiplication and division happen before addition and subtraction. Use parentheses to be explicit: (price + tax) * discount.

4. Off-by-One Errors

range(1, 6) gives numbers 1 through 5, not 1 through 6. The end value is exclusive.

5. Dividing by Zero

Division by zero crashes programs. Always check that the divisor is not zero before dividing.

6. Mixing Types

Adding a number and a string causes an error: "5" + 3 fails. Convert types explicitly.

7. Using Floating Point for Money

Floating point numbers have rounding errors. For money calculations, use integers (cents) or decimal types.

Practice Questions

1. What is the difference between = and == in code? = assigns a value to a variable. == compares two values and returns True or False.

2. What does the modulo operator % do? It returns the remainder after division. 15 % 4 equals 3 because 15 divided by 4 is 3 with a remainder of 3.

3. What is the result of (5 > 3) and not (2 > 4)? True. (5 > 3) is True. (2 > 4) is False. not False is True. True and True is True.

4. Why does range(1, 6) produce five numbers instead of six? In most programming languages, range start is inclusive and end is exclusive. The sequence is 1, 2, 3, 4, 5.

5. Challenge: Write a program that checks if a number is a multiple of both 3 and 5. Test it with 15 (should be True) and 10 (should be False). Hint: use the modulo operator and Boolean and.

Try It Yourself

Open a Python environment (or an online compiler) and write a program that calculates the total cost of three items with tax. Define the prices as variables, calculate the subtotal, apply a tax rate, and print the final total. Then modify the program to apply a 10% discount if the subtotal is over $50.

Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro