# DIY Desktop Calculator with CircuitPython 

## Overview

In this project, you'll build your own Desktop Calculator with CircuitPython. Along the way you'll also learn about the alternative "RPN" notation for calculating, how a keyboard matrix works (and how to create your own!), and more!

![](https://cdn-learn.adafruit.com/assets/assets/000/096/176/medium800/projects_PXL_20201021_162405210.jpg?1603297544)

### What is "RPN", anyway?

Most of us are accustomed to standard mathematical notation, like 2+3\*7 or (2+3)\*7. Because most operations (like "+") appear between the two things operated on (like 2 and 3), this is called "infix" notation. Two alternatives to "infix" are "postfix" and "prefix". Postfix notation is also called RPN, which stands for "Reverse Polish Notation".

Possibly because it was used in some classic HP calculators, many people have a fondness for RPN or even feel that it works better than standard notation.

In standard infix notation, you have to know the "precedence rule", and understand that when given 2+3\*7 you first compute 3\*7=21 and then compute 2+21=23. When the rule does not reflect your intent, you add parentheses:&nbsp; (2+3)\*7. In this case you evaluate 2+3=5 first, then 5\*7=35. Mnemonic devices such as PEDMAS help us remember the rules.

In postfix notation, there are no parentheses and no rules of precedence. Instead, there what is called the "stack". When you enter a number, it is placed on the stack.&nbsp; When you enter an operator such as "+" that operates on two values, the "top" two values are taken off the stack, the operation is performed, and the result is put back on the stack.

If you'd like to know more about RPN, I found that the short paper "[Order of Operations and RPN](https://digitalcommons.unl.edu/cgi/viewcontent.cgi?article=1045&context=mathmidexppap)" provides helpful background.

To interpret "2 3 7 \* +", start with the empty stack and place each number on the stack as you encounter it. When you encounter "\*", your stack will contain "2", "3", and "7" in that order. This makes the values that "+" will operate on "3" and "7", so they are removed and replaced with "21". Now the stack contains "2" and "21" in that order. When you encounter "+", you take "2" and "21" off and put "23" on. That's the same as "2 + 3 \* 7" in standard notation.

Operations don't have to appear at the end only, they can appear in the middle. Consider "2 3 + 7 \*". When you encounter "+", the stack has "2" and "3", so replace it with "5". Next, 7 gets placed on the stack. When you encouter "\*", the stack has "5" and "7" so replace them with "35". That's the same as "(2 + 3) \* 7" in standard notation.

![](https://cdn-learn.adafruit.com/assets/assets/000/096/178/medium800/projects_IMG_20200910_124534%281%29.jpg?1603297790)

### What is Decimal Arithmetic?

This calculator uses a decimal arithmetic library ported from standard Python3. It's available in the community bundle under the name jepler\_udecimal. Decimal is "designed with people in mind, and necessarily has a paramount guiding principle–computers must provide an arithmetic that works in the same way as the arithmetic that people learn at school."

In this library, "0.1 + 0.2" is exactly equal to "0.3". Even better, the precision isn't limited to just 6 digits. In Python code, you can select any number of digits (even in the hundreds!). For this project, so that numbers fit on the display, numbers up to 14 digits are displayed.

This stands in contrast with how floats work in CircuitPython. CircuitPython flating point numbers only have about 6 to 7 digits of decimal precision, and values like "0.1" are not exact values.

## Parts
Featured
### Adafruit SHARP Memory Display Breakout - 2.7" 400x240 Monochrome

[Adafruit SHARP Memory Display Breakout - 2.7" 400x240 Monochrome](https://www.adafruit.com/product/4694)
The **Adafruit** &nbsp; **2.7" 400x240 SHARP Memory Display Breakout** &nbsp;is a chonky cross between an eInk (e-paper) display and an LCD. It has the ultra-low power usage of eInk and the fast-refresh rates of an LCD. This model has a gray background, and the...

In Stock
[Buy Now](https://www.adafruit.com/product/4694)
[Related Guides to the Product](https://learn.adafruit.com/products/4694/guides)
![Adafruit SHARP Memory Display Breakout connected to a half-sized breadboard and a microcontroller. The breakout board plays a black and white animation video of black figures playing violins and a keyboard.](https://cdn-shop.adafruit.com/product-videos/640x480/4694-04.jpg)

Featured
### Adafruit Feather nRF52840 Express

[Adafruit Feather nRF52840 Express](https://www.adafruit.com/product/4062)
The **Adafruit Feather nRF52840 Express** is the new Feather family member with Bluetooth® Low Energy and _native USB support_ featuring the nRF52840!&nbsp; It's our take on an 'all-in-one' Arduino-compatible + Bluetooth® Low Energy with built-in USB...

In Stock
[Buy Now](https://www.adafruit.com/product/4062)
[Related Guides to the Product](https://learn.adafruit.com/products/4062/guides)
![Angled shot of a Adafruit Feather nRF52840 Express. ](https://cdn-shop.adafruit.com/640x480/4062-02.jpg)

Featured
### Colorful 12mm Square Tactile Button Switch Assortment - 15 pack

[Colorful 12mm Square Tactile Button Switch Assortment - 15 pack](https://www.adafruit.com/product/1010)
Little clicky switches are standard input "buttons" on electronic projects. These work best in a PCB but [can be used on a solderless breadboard as shown in this tutorial](http://www.ladyada.net/learn/arduino/lesson5.html). The pins are normally open (disconnected) and...

In Stock
[Buy Now](https://www.adafruit.com/product/1010)
[Related Guides to the Product](https://learn.adafruit.com/products/1010/guides)
![Angled  shot of 15 colorful square tactile button switches in green, yellow, red, blue, and white.](https://cdn-shop.adafruit.com/640x480/1010-04.jpg)

Featured
### Bakelite Universal Perfboard Plates - Pack of 10

[Bakelite Universal Perfboard Plates - Pack of 10](https://www.adafruit.com/product/2670)
Make your next project as you imagine it with&nbsp;prototyping perfboards that can easily be cut with scissors like these **Bakelite Universal Perfboard Plates**!

We wanted a perfboard&nbsp;that was both customizable and durable and <a...></a...>

In Stock
[Buy Now](https://www.adafruit.com/product/2670)
[Related Guides to the Product](https://learn.adafruit.com/products/2670/guides)
![Angled shot of single Bakelite Universal Perfboard Plate.](https://cdn-shop.adafruit.com/640x480/2670-00.jpg)

### Break-away 0.1" 36-pin strip right-angle male header (10 pack)

[Break-away 0.1" 36-pin strip right-angle male header (10 pack)](https://www.adafruit.com/product/1540)
Breakaway header is like the duct tape of electronics. Its great for connecting things together, soldering to perf-boards, fits into any breadboard, etc. We go through these guys real fast, and thought that given how handy they are, we'd offer them in a pack of ten!  
  
Each pack...

In Stock
[Buy Now](https://www.adafruit.com/product/1540)
[Related Guides to the Product](https://learn.adafruit.com/products/1540/guides)
![Break-away 0.1 inch 36-pin strip right-angle male header](https://cdn-shop.adafruit.com/640x480/1540-00.jpg)

### Premium Female/Female Jumper Wires - 20 x 3" (75mm)

[Premium Female/Female Jumper Wires - 20 x 3" (75mm)](https://www.adafruit.com/product/1951)
These female-female premium jumper wires are handy for making wire harnesses or jumpering between headers on PCB's. They're&nbsp;3" (75mm) long and come in a 'strip' of 20 (2 pieces of each of ten rainbow colors). They have 0.1" female header contacts on either end...

In Stock
[Buy Now](https://www.adafruit.com/product/1951)
[Related Guides to the Product](https://learn.adafruit.com/products/1951/guides)
![Angled shot of Premium Female/Female Jumper Wires - 20 x 3 (75mm)](https://cdn-shop.adafruit.com/640x480/1951-01.jpg)

### White Nylon Machine Screw and Stand-off Set – M2.5 Thread

[White Nylon Machine Screw and Stand-off Set – M2.5 Thread](https://www.adafruit.com/product/3658)
Totaling 420 pieces, this **White Nylon&nbsp;M2.5 Screw Set** &nbsp;is a must-have smörgåsbord for your workstation.&nbsp;You'll have more than enough screws, nuts, and hex standoffs to construct&nbsp;your maker projects&nbsp;for days on end! M2.5 screws fit nearly...

In Stock
[Buy Now](https://www.adafruit.com/product/3658)
[Related Guides to the Product](https://learn.adafruit.com/products/3658/guides)
![White Nylon Screw and Stand-off Set with M2.5 Threads, kit box](https://cdn-shop.adafruit.com/640x480/3658-00.jpg)

### Hook-up Wire Spool Set - 22AWG Solid Core - 6 x 25 ft

[Hook-up Wire Spool Set - 22AWG Solid Core - 6 x 25 ft](https://www.adafruit.com/product/1311)
Perfect for bread-boarding, free wiring, etc. This box contains 6 spools of solid-core wire. The wire is easy to solder to and when bent it keeps its shape pretty well. We like to have a few spools of this stuff around which is why this set is quite nice! We suggest picking up wire strippers...

In Stock
[Buy Now](https://www.adafruit.com/product/1311)
[Related Guides to the Product](https://learn.adafruit.com/products/1311/guides)
![Hook-up Wire Spool Set in box with 6 colorful wires coming out](https://cdn-shop.adafruit.com/640x480/1311-04.jpg)

### Solder Wire - 60/40 Rosin Core - 0.5mm/0.02" diameter - 50 grams

[Solder Wire - 60/40 Rosin Core - 0.5mm/0.02" diameter - 50 grams](https://www.adafruit.com/product/1886)
If you want to make a kit you'll need some solder. This 0.1 lb (about 50 grams) spool is a good amount, not too much (like 1 lb spools) and not too little (like those little 'pocket clip' vials).  
  
This spool of solder contains approximately 0.6mm / .02" diameter in...

Out of Stock
[Buy Now](https://www.adafruit.com/product/1886)
[Related Guides to the Product](https://learn.adafruit.com/products/1886/guides)
![Solder Wire - 60/40 Rosin Core - 0.5mm/0.02" diameter.](https://cdn-shop.adafruit.com/640x480/1886-01.jpg)

## Tools
### Flush diagonal cutters

[Flush diagonal cutters](https://www.adafruit.com/product/152)
These are the best diagonal cutters, large super-comfortable grip to use and have strong nippers for perfect trimming of wires and leads. I've used my pair every day for years.

In Stock
[Buy Now](https://www.adafruit.com/product/152)
[Related Guides to the Product](https://learn.adafruit.com/products/152/guides)
![Red and black diagonal flush cutters](https://cdn-shop.adafruit.com/640x480/152-02.jpg)

- [Next Page](https://learn.adafruit.com/diy-rpn-desktop-calculator-with-circuitpython/soldering-the-keyboard-matrix.md)

## Guide Products

### Adafruit SHARP Memory Display Breakout - 2.7" 400x240 Monochrome

[Adafruit SHARP Memory Display Breakout - 2.7" 400x240 Monochrome](https://www.adafruit.com/product/4694)
The **Adafruit** &nbsp; **2.7" 400x240 SHARP Memory Display Breakout** &nbsp;is a chonky cross between an eInk (e-paper) display and an LCD. It has the ultra-low power usage of eInk and the fast-refresh rates of an LCD. This model has a gray background, and the...

In Stock
[Buy Now](https://www.adafruit.com/product/4694)
[Related Guides to the Product](https://learn.adafruit.com/products/4694/guides)
### Adafruit Feather nRF52840 Express

[Adafruit Feather nRF52840 Express](https://www.adafruit.com/product/4062)
The **Adafruit Feather nRF52840 Express** is the new Feather family member with Bluetooth® Low Energy and _native USB support_ featuring the nRF52840!&nbsp; It's our take on an 'all-in-one' Arduino-compatible + Bluetooth® Low Energy with built-in USB...

In Stock
[Buy Now](https://www.adafruit.com/product/4062)
[Related Guides to the Product](https://learn.adafruit.com/products/4062/guides)
### Colorful 12mm Square Tactile Button Switch Assortment - 15 pack

[Colorful 12mm Square Tactile Button Switch Assortment - 15 pack](https://www.adafruit.com/product/1010)
Little clicky switches are standard input "buttons" on electronic projects. These work best in a PCB but [can be used on a solderless breadboard as shown in this tutorial](http://www.ladyada.net/learn/arduino/lesson5.html). The pins are normally open (disconnected) and...

In Stock
[Buy Now](https://www.adafruit.com/product/1010)
[Related Guides to the Product](https://learn.adafruit.com/products/1010/guides)
### Bakelite Universal Perfboard Plates - Pack of 10

[Bakelite Universal Perfboard Plates - Pack of 10](https://www.adafruit.com/product/2670)
Make your next project as you imagine it with&nbsp;prototyping perfboards that can easily be cut with scissors like these **Bakelite Universal Perfboard Plates**!

We wanted a perfboard&nbsp;that was both customizable and durable and <a...></a...>

In Stock
[Buy Now](https://www.adafruit.com/product/2670)
[Related Guides to the Product](https://learn.adafruit.com/products/2670/guides)
### Break-away 0.1" 36-pin strip right-angle male header (10 pack)

[Break-away 0.1" 36-pin strip right-angle male header (10 pack)](https://www.adafruit.com/product/1540)
Breakaway header is like the duct tape of electronics. Its great for connecting things together, soldering to perf-boards, fits into any breadboard, etc. We go through these guys real fast, and thought that given how handy they are, we'd offer them in a pack of ten!  
  
Each pack...

In Stock
[Buy Now](https://www.adafruit.com/product/1540)
[Related Guides to the Product](https://learn.adafruit.com/products/1540/guides)
### Premium Female/Female Jumper Wires - 20 x 3" (75mm)

[Premium Female/Female Jumper Wires - 20 x 3" (75mm)](https://www.adafruit.com/product/1951)
These female-female premium jumper wires are handy for making wire harnesses or jumpering between headers on PCB's. They're&nbsp;3" (75mm) long and come in a 'strip' of 20 (2 pieces of each of ten rainbow colors). They have 0.1" female header contacts on either end...

In Stock
[Buy Now](https://www.adafruit.com/product/1951)
[Related Guides to the Product](https://learn.adafruit.com/products/1951/guides)
### White Nylon Machine Screw and Stand-off Set – M2.5 Thread

[White Nylon Machine Screw and Stand-off Set – M2.5 Thread](https://www.adafruit.com/product/3658)
Totaling 420 pieces, this **White Nylon&nbsp;M2.5 Screw Set** &nbsp;is a must-have smörgåsbord for your workstation.&nbsp;You'll have more than enough screws, nuts, and hex standoffs to construct&nbsp;your maker projects&nbsp;for days on end! M2.5 screws fit nearly...

In Stock
[Buy Now](https://www.adafruit.com/product/3658)
[Related Guides to the Product](https://learn.adafruit.com/products/3658/guides)
### Hook-up Wire Spool Set - 22AWG Solid Core - 6 x 25 ft

[Hook-up Wire Spool Set - 22AWG Solid Core - 6 x 25 ft](https://www.adafruit.com/product/1311)
Perfect for bread-boarding, free wiring, etc. This box contains 6 spools of solid-core wire. The wire is easy to solder to and when bent it keeps its shape pretty well. We like to have a few spools of this stuff around which is why this set is quite nice! We suggest picking up wire strippers...

In Stock
[Buy Now](https://www.adafruit.com/product/1311)
[Related Guides to the Product](https://learn.adafruit.com/products/1311/guides)

## Related Guides

- [Introducing the Adafruit nRF52840 Feather](https://learn.adafruit.com/introducing-the-adafruit-nrf52840-feather.md)
- [Deco Two-Key Feather Macro Pad](https://learn.adafruit.com/deco-two-key-keypad-macropad-circuitpython-feather.md)
- [Breadboards for Beginners](https://learn.adafruit.com/breadboards-for-beginners.md)
- [Analog Knobs on Raspberry Pi 400 with CYBERDECK Hat](https://learn.adafruit.com/analog-knobs-on-raspberrypi-400-with-cyberdeck-hat.md)
- [NYE Circuit Playground Drop](https://learn.adafruit.com/nye-circuit-playground-drop.md)
- [Desk Calculator with CircuitPython](https://learn.adafruit.com/desk-calculator-with-circuitpython.md)
- [Adafruit QT Py Activity Timer and Hydration Reminder](https://learn.adafruit.com/qt-py-activity-timer-and-hydration-reminder.md)
- [Fisher-Price USB Foot Pedal](https://learn.adafruit.com/fisher-price-usb-foot-pedal.md)
- [No-Solder Faux Neon Bike Lights](https://learn.adafruit.com/no-solder-faux-neon-bike-lights.md)
- [JOY Controller for Adafruit Feather](https://learn.adafruit.com/joy-controller-feather.md)
- [PB Gherkin 30% keyboard with KMK, CircuitPython, & KB2040](https://learn.adafruit.com/pb-gherkhin-30-keyboard-with-kmk-circuitpython-kb2040.md)
- [Control Electronics with your Brain using NextMind](https://learn.adafruit.com/control-electronics-with-your-brain-using-nextmind.md)
- [Zelda: Breath of the Wild – 3D Printed Bladesaw](https://learn.adafruit.com/bladesaw.md)
- [USB C Dock](https://learn.adafruit.com/usb-c-dock.md)
- [MUNNY Glowing Friend with Bluetooth Control!](https://learn.adafruit.com/munny-lamp.md)
- [Cranky Adabot Solder Dispenser](https://learn.adafruit.com/cranky-adabot-solder-dispenser.md)
