Calculator with memory, supports + - / * sqrt()
Project description
Calculator: Simple Memory-Based Arithmetic Module
A tiny Python class that keeps a running memory and performs validated arithmetic operations.
This document explains the module, how to install/use it. It also includes a mini‑tutorial in English
Features
Calculator.memory: read only property exposing the current memory value.- Validated inputs: only
intorfloatare accepted; other types raiseTypeError. - Arithmetic:
add,subtract,multiply,divide. - Utilities:
sqrt(adds sqrt(n) to memory),memory_reset(resets to0.0). - Clear, Google style docstrings and helpful error messages.
Installation
This is a single file module. You can:
- drop
calculator.pyinto your project, or - install it as an editable local package if you later add
pyproject.toml/setup.cfg.
Python version: 3.9+ recommended.
Quick Start (Tutorial)
1) Import and construct
from calculator import Calculator
c = Calculator()
print(c.memory) # 0.0
2) Basic operations
c.add(10) # -> 10.0
c.subtract(3.5) # -> 6.5
c.multiply(2) # -> 13.0
c.divide(4) # -> 3.25
3) Square root accumulation
c.sqrt(9) # adds 3, -> 6.25
4) Reset
c.memory_reset() # -> 0.0
5) Defensive behavior (errors)
try:
c.add("3") # not allowed
except TypeError as e:
print(e) # Argument must be an int or float
try:
c.divide(0) # division by zero
except ZeroDivisionError as e:
print(e)
try:
c.sqrt(-1) # negative sqrt
except ValueError as e:
print(e) # Cannot take square root of a negative real number
# read-only property
try:
c.memory = 42
except AttributeError as e:
print(e) # memory is read-only
API Reference
Class: Calculator()
- Properties
memory: float: read only; returns the current memory value.
- Methods
add(n: float) -> float: Addsnto memory.subtract(n: float) -> float: Subtractsnfrom memory.multiply(n: float) -> float: Multiplies memory byn.divide(n: float) -> float: Divides memory byn. RaisesZeroDivisionErrorifn == 0.sqrt(n: float) -> float: Addssqrt(n)to memory. RaisesValueErrorifn < 0.memory_reset() -> float:Resets memory to0.0.
Validation rule: a private helper _to_float converts and validates inputs. Only int and float are accepted
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file simple_calculator_tc2-1.2.tar.gz.
File metadata
- Download URL: simple_calculator_tc2-1.2.tar.gz
- Upload date:
- Size: 2.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
35e6578296f4e77fbd610282f8913ef5ee32b297f1477004978021f53a9a6cee
|
|
| MD5 |
22dde7e3999cd62b69916743fb946459
|
|
| BLAKE2b-256 |
b59521d4c9c246170ef68d80f3c238b9c9c91dc5b5151a0c8f383e543a49bedb
|
File details
Details for the file simple_calculator_tc2-1.2-py3-none-any.whl.
File metadata
- Download URL: simple_calculator_tc2-1.2-py3-none-any.whl
- Upload date:
- Size: 2.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c9865767458cb2d6a18f2030cf38f3a42a9bd223fcd50412a2402cdfcb8fd7e0
|
|
| MD5 |
021aea29237ce340a0bddb24ee0e585c
|
|
| BLAKE2b-256 |
c0dca47d937af3ed7cf94b005b19bbd2f0b7ea0be2eefe06877ebbc93a9caaeb
|