The addition operator (+) is widely used in programming languages, including Python, to add two or more values together. In its simplest form, Python allows for easy addition of numbers by directly entering the values into the command line. For example, you can add two numbers by simply typing them in the Python command line and using the “+” operator between them, like this: 2+3 which will return 5. Additionally, Python supports addition of multiple values at once, using the “+” operator multiple times. For example, 2+3+4 will return 9. This makes it easy for developers to work with numbers and perform mathematical operations in Python.
$python
Python 2.7.10 (default, Jul 15 2017, 17:16:57)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 4 + 5
9
Here python adds two numbers and prints their sum as shown above. Let us see a few other ways to add two numbers.
Adding two numbers and passing them directly to the print function.
# add two numbers 10 and 5 and print the result.
print(10 + 5) # prints 15 as output.
Python offers several ways to add numbers and print the result. One way is to simply add the numbers and pass them directly to the print function, as shown in the example above. Another way is to use a function to add the numbers and return the result. This allows for the flexibility to call the same function multiple times with different numbers to get their sum. For example:
# addtwo.py
# function to add two numbers.
def add(one, two):
return one + two
# add 5 and 10 and print the result.
print(add(5, 10))
# Output
$python addtwo.py
15
Above ‘addtwo.py’ prints the result of the addition of both numbers.
Addition of two floating-point numbers
Python also allows for the easy addition of floating-point numbers, which are numbers with decimal points. The process is similar to adding integers. You can simply use the “+” operator between the two numbers you want to add and print the result. For example:
# add two floating numbers and print.
print(4.5 + 5.5) # prints 10.0
This code snippet adds the two floating-point numbers 4.5 and 5.5 and returns the sum as a floating-point value of 10.0. This feature makes it easy for developers to perform mathematical operations with decimal numbers in Python. And this can be used in various cases such as for financial calculations or scientific computations.
We also can add two strings and boolean values
In Python, the addition (+) operator is not limited to only numbers, but it can also be used for strings and boolean values. For example, when used on string objects, it acts as a concatenation operator and combines the two strings into one. The following code shows an example of this:
# add two strings and print.
print('Hello' + ' World!') # prints 'Hello World!'.
As you can see, the code above concatenates the strings ‘Hello’ and ‘ World!’ into one string ‘Hello World!’.
Additionally, the addition operator can also be used with boolean values in Python. The boolean values True and False are represented as 1 and 0, respectively. When you add these two boolean values, it returns the integer value of 1 as shown in the example below:
# add two boolean values and print.
print(True + False) # prints '1'.
Therefore, the addition operator in Python can be used for various types of values such as integers, floating-point numbers, strings, and boolean values. Understanding how it behaves with different types of values is important for effective use of the operator in your code. Happy Learning!