Skip to main content
  • Python is available now. All other compilers are coming soon.

    Python
  • Popular Tutorials

    • Getting Started With Python
    • Python if Statement
    • while Loop in Python
    • Python Lists
    • Dictionaries in Python
    All Python Tutorials

    Reference Materials

    Built-in FunctionsList MethodsDictionary MethodsString Methods
    View all

Online Compilers

  • Python Compiler
  • Python Playground
  • Python Projects
  • Exercises

Tutorials

  • Learn Python
  • Python for Beginners
  • Variables and Types
  • Python Strings
  • Python Lists
  • Python If Else
  • Python For Loops
  • Python Functions
  • Python Classes

Examples

  • Python Examples
  • Hello World
  • Add Two Numbers
  • Even or Odd
  • Factorial
  • Fibonacci
  • Prime Check
  • Palindrome Check

Company

  • About
  • Contact
  • Privacy Policy
  • Terms of Use
  • Disclaimer

© 2026 ILoveCompiler. All rights reserved.

  1. Home
  2. Python Examples
  3. Add Two Numbers

Variables · Python example

Add Two Numbers

Store two integers in variables and print their sum with an f-string.

Open the compilerAll examples
beginnervariablesintegersf-strings

This example stores two numbers in variables, adds them, and formats the result into a readable sentence.

example.py
first = 12
second = 30
total = first + second

print(f"{first} + {second} = {total}")

How the code works

  • first is assigned 12 and second is assigned 30.
  • total receives the result of first + second.
  • The f-string evaluates the three variable names and inserts their values.

Key ideas

  • Variables are names that refer to values.
  • The + operator adds two integers.
  • An f-string lets you place expressions inside text using curly braces.

Try changing it

  • Read the two numbers with input() and convert them with int().
  • Use float() to add decimal values.
  • Print the result without an f-string to compare the output.

Related examples

beginner

Variables and Data Types

Create strings, integers, floats, and booleans, then inspect each type.

beginner

Celsius to Fahrenheit

Convert a temperature with a formula and formatted output.

Previous exampleHello World in PythonNext exampleVariables and Data Types

Expected output

12 + 30 = 42

Output is compared after trimming trailing whitespace on each line. Run the code to see it produced by CPython in your browser.

Practice next

Apply the same idea with automatic tests or browse a guided project.

ExercisesProjects