How to Add Two Numbers in Python- Python Programming

Introduction

Adding two numbers is one of the most fundamental operations in programming, often used to illustrate the basic syntax and structure of a programming language. This simple task helps us understand how different languages handle variables, functions, and output. Below, we’ll explore the basic code for adding two numbers in various programming languages and break down the core components of each example to make the concept clear.

 

Code 

 

 

# Python code to add two numbers
def add_numbers(a, b):
    return a + b

# Example usage
num1 = 5
num2 = 10
result = add_numbers(num1, num2)
print("Sum:", result)
Python
  • EXPLANATION

  • Define the Function

    • In Python, functions are defined using the def keyword. Here’s how you define a function to add two numbers:
      python
      def add_numbers(a, b): return a + b
      • def indicates the start of a function definition.
      • add_numbers is the name of the function.
      • (a, b) are parameters that the function will accept.
      • return a + b computes the sum of a and b and returns the result.
  • Set Up Input Values

    • To use the function, you need to specify the values you want to add. In the example:
      python
      num1 = 5 num2 = 10
      • num1 and num2 are variables that store the numbers you want to add. Here, num1 is set to 5, and num2 is set to 10.
  • Call the Function

    • After defining the function and setting the values, you call the function to get the result:
      python
      result = add_numbers(num1, num2)
      • add_numbers(num1, num2) executes the function with num1 and num2 as arguments.
      • The returned value (sum of 5 and 10) is stored in the variable result.
  • Print the Result

    • To display the result, use the print function:
      python
      print("Sum:", result)
      • print outputs the string "Sum:" followed by the value of result (which is 15).
  • Run the Code

    • To see the output, save the code in a file, for example, add.py, and run it using:
      sh
      python add.py
      • This command executes the Python script and displays the output in the terminal or command prompt.
  • Understand the Output

    • When you run the script, the output will be:
      makefile
      Sum: 15
      • This confirms that the function correctly added the numbers 5 and 10, displaying the sum as 15.

 

Usage: Save the code in a file named add.py. Run it using the command:

sh

python add.py

 

OUTPUT

SEE THE OUTPUT OF THE ABOVE CODE 

 

 

Sum: 15
Python

SEE COMPLETELY COMPILED CODE

PREES PLAY BUTTON TO RUN THE PROGRAM 

Conclusion

The Python code for adding two numbers showcases the language’s simplicity and effectiveness. By defining a function, setting input values, calling the function, and printing the result, you can perform basic arithmetic operations easily. Python’s clear syntax makes it a great choice for learning programming fundamentals. For more information on Python functions and basic operations, you can visit the official Python documentation.

COMPILE CODE : CODE COMPILER 

Leave a Reply

Your email address will not be published. Required fields are marked *

Resize text
Scroll to Top