Python (Scripting) - Addition and Increment (+)

by
Jeremy Canfield |
Updated: November 06 2023
| Python (Scripting) articles
Here is the most basic example of how to add two numbers in Python.
#!/usr/bin/python
print(1 + 1)
It is much more common to store the result in a variable.
#!/usr/bin/python
result = (1 + 1)
print(result)
Which should return the following.
2
Increment
Here is an example of how to increment a value.
#!/usr/bin/python3
value = 0
print(f"value = {value}")
value += 1
print(f"value = {value}")
Which should produce the following.
value = 0
value = 1
An increment is often used as part of a while loop.
#!/usr/bin/python3
attempt = 0
while attempt < 5:
print(f"{attempt} is less than 5")
attempt += 1
Should produce the following output.
0 is less than 5
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
Decimal numbers
By default, decimal will be used.
#!/usr/bin/python
result = (1.1 + 1.1)
print(result)
Which should return the following.
2.2
Whole numbers
round can be used to round to whole numbers.
#!/usr/bin/python
result = round(1.1 + 1.1)
print(result)
Which should return the following.
2.0
Did you find this article helpful?
If so, consider buying me a coffee over at