Python is a powerful and versatile programming language, widely used in various fields such as data analysis, machine learning, web development, and scientific computing. One of the fundamental operations in any programming language is arithmetic, and Python facilitates this with its simple syntax and robust data types.

In this guide, we will explore how to add two numbers together in Python. Whether you're a beginner or an experienced programmer, understanding this basic operation is crucial to building more complex programs. So, let's dive right in!

Adding Numbers in Python
Python provides several ways to add numbers together. The most common method is using the plus operator (+). This operator can be used with both integers and floating-point numbers.

Here's a simple example demonstrating how to add two numbers using the plus operator:
```python # Example: Adding two integers num1 = 5 num2 = 3 result = num1 + num2 print(result) # Output: 8 ```
Integer Addition

In the example above, we're adding two integers (whole numbers). Python automatically performs integer addition when both operands are integers.
You can also perform binary addition using the plus operator. Binary numbers are represented in Python using the `0b` prefix:
```python # Example: Adding binary numbers binary_num1 = 0b101 binary_num2 = 0b110 result = binary_num1 + binary_num2 # Pythons automatic binary addition print(result) # Output: 1011 (decimal equivalent: 11) ```
Float Addition

Python also supports addition of floating-point numbers (numbers with decimal points). You can specify floating-point numbers in Python by appending a decimal point and the fractional part to an integer, like this: 3.14.
Here's an example of adding floating-point numbers in Python:
```python # Example: Adding floating-point numbers float_num1 = 2.5 float_num2 = 1.5 result = float_num1 + float_num2 print(result) # Output: 4.0 ```
Addition with User Input

In many Python programs, you'll want to add numbers provided by the user at runtime. Python provides the `input()` function to obtain user input from the standard input stream (usually the keyboard).
However, `input()` returns a string, so you'll need to convert it to a number (either `int` or `float`) using the `int()` or `float()` functions before performing addition. Here's an example:


















```python # Example: Adding numbers provided by the user num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: ")) result = num1 + num2 print(f"The sum of {num1} and {num2} is {result}") ```
Addition with Lists
In Python, the plus operator can also be used to add lists together. When adding lists, Python concatenates (appends) the elements of the second list to the end of the first list:
```python # Example: Adding lists list1 = [1, 2, 3] list2 = [4, 5, 6] result = list1 + list2 print(result) # Output: [1, 2, 3, 4, 5, 6] ```
It's important to note that adding lists with different data types will result in a `TypeError`. To avoid this, ensure that the lists you're adding together contain the same data types.
In addition to the plus operator, Python also provides functions in the `math` module for performing arithmetic operations, such as `add()`, which can be used to add numbers together. However, these functions are less commonly used than the plus operator.
That's it! You now know how to add numbers together in Python in various ways, from simple integers to user-provided input. Happy coding!