This is down to changes that were made for new versions of ZPE.

Jamie Balfour BSc

Full stack developer

Mathematics is an important part of programming. In fact, programming is a mathematical concept. This article will discuss how Python mathematics works.

There are a collection of mathematical operators that perform different tasks. The expressions used to represent such operators are shown in the following table, along with an example of each.

Name | Description | Operator Code | Example | Result |
---|---|---|---|---|

Addition | Simply used to add two numbers together. | `+` |
`5 + 10` |
15 |

Subtract | Used to subtract the second number from the first | `-` |
`10 - 5` |
5 |

Multiply | Used to multiply two numbers | `*` |
`7 * 4` |
28 |

Divide | Used to divide the first number by the second number and return a floating point number | `/` |
`50 / 7` |
7.14... |

Raise | Raise a number to another number in the same way as
the mathematical representation x^{y} |
`**` |
`2**7` |
128 |

Modulus | Used to get the remainder of a division as an integer. | `%` |
`5 % 4` |
1 |

These operations can be used to return a result as an integer or floating point number.

A boolean operation can be used to return a boolean value (`True`

or `False`

).
The table below specifies all of these available in Python:

Name | Description | Operator Code | Example (of truth) |
---|---|---|---|

Greater than | True if the first expression is greater than the second expression | > | 7 > 4 |

Less than | True if the first expression is less than the second expression | < | 7 < 16 |

Greater than or equal to | True if the first expression is greater than or equal to the second expression | >= | 17 >= 16 |

Less than or equal to | True if the first expression is less than or equal to the second expression | <= | 16 <= 16 |

Equal to | True if both expressions are equal | == | 16 == 16 |

Not equal | True if both expressions are not equal | != | 16 != 7 |

The results of these operations can be placed in a boolean variable as shown in the following sample:

Python

a = 5 b = 9 c = a + b - 7 result = c > a print(result)

The above sample should return `True`

.

Boolean logic also uses other operators. These can also be represented in Python and are also known as bitwise operators. Formal logic and specification defines the way these work and Python contains expressions that form statements like the following which can be constructed in Python:

x = (a ∧ b) ⊕ (c ∨ d)

The above expression will evaluate true when *a* and *b* are both false and either *c* or *d *are
true or when *a* and *b* are both true and both *c* and *d* are false due to the ⊕ operator.

The following table shows these operators.

Name | Description | Logical symbol | Operator code | Example (of truth) |
---|---|---|---|---|

And | Logical conjunction, true when both expressions are true. |
∧ | `And` |
`True And True` |

Or | Logical disjunction, true when either expressions are true. |
∨ | `Or` |
`True Or False` |

Not | Logical negation, returns true when given a false expression. |
¬ | `!` |
`!(False)` |

The following is a truth table that represents each of the operators shown above for some P and some Q.

P | Q | ¬P | P∧Q | P∨Q |
---|---|---|---|---|

T | T | F | T | T |

T | F | F | F | T |

F | T | T | F | T |

F | F | T | F | F |

Most languages have a shorthand method for increasing a value or variable by another value that saves writing the whole assignment and mathematical expression.

For instance, consider the following:

Python

x = 10 print (x) x = x + 1 print (x) x = x - 1 print (x)

The method of increasing the value of `x`

is
to reassign the value with the value plus one. What if there
were a simpler, more syntactically concise method of doing
this?

Meet incrementing and decrementing values. In reference to a value or a variable, incrementing and decrementing are very important as in many programming languages they are able to produce small performance benefits.

Python increments with `x += 1`

and decrements with `x -= 1`

. Many other
languages use `++`

and `--`

syntaxes but most of these
languages also opt to include this syntax for these purposes.

Python

x = 10 print (x) x += 1 print (x) x -= 1 print (x)

Site news

jamiebalfour.scot

My new domain is jamiebalfour.scot, something I've waited a long time for.

Website update

Push notifications are back by popular demand.

The Right to Repair

Join the campaign to support the Right to Repair and make the world of electronics fairer for the consumer.

ZPE 1.8.4

ZPE 1.8.4 (Evershed) is now available to download. 1.8.4 brings minor fixes. See the changelog for more information.

jamiebalfour.scot: dark mode

Check out my websites new dark mode! If you are using a modern browser you can use this by changing your operating system to dark mode.

Old versions of ZPE

Older versions of ZPE can no longer connect to the ZULE repository.

This is down to changes that were made for new versions of ZPE.

This is down to changes that were made for new versions of ZPE.

New effects for BalfSlider

Several new effects have been added to BalfSlider this month. The next version of BalfSlider is also underwork.

Quick links

@mgs_mrjbalfour

S1s and S2s really enjoyed the lesson on cybercrime using the lesson on How to Rob A Bank on… https://t.co/MxjDYE2JFu 3 months ago |

S2s doing really well in hardware test! Also really enjoying doing tests electronically rather than on paper.… https://t.co/ritdfcW6Mw 3 months ago |

S1 and S2 pupils listing input and output devices and giving answers such as keyboard and mouse but there were also… https://t.co/dGxDkYqFxo 3 months ago |

Get in touch

Back to top

Site miscellanea

Active projects

© Copyright Jamie B Balfour

Provide feedback on this page

Celebrating 10 years of my website!

15th May 2020

15th May 2020

Celebrating 10 years of my website!

15th May 2020

15th May 2020

Celebrating 10 years of my website!

15th May 2020

15th May 2020