# Python as calculator

#### 3.1.1. Numbers

Python, at its core, acts as a powerful calculator. Let's explore some fundamental concepts:

#### Basic Arithmetic Operations

Python supports common mathematical operations, making it similar to languages like Pascal or C. Here's a quick overview:

````python
```python
>>> 2 + 2
4
>>> 50 - 5 * 6
20
>>> (50 - 5 * 6) / 4
5.0
>>> 8 / 5  # Division always returns a floating-point number
1.6
````

* **Arithmetic Operations:** Use `+`, `-`, `*`, and `/` just like you would on a regular calculator.
    

#### Types of Numbers

Numbers in Python come in two main types: integers (`int`) and floating-point numbers (`float`).

````python
```python
>>> type(2)
<class 'int'>
>>> type(5.0)
<class 'float'>
````

* **Numeric Types:** Python distinguishes between whole numbers (integers) and those with decimals (floats).
    

#### Floor Division and Modulo

For more advanced calculations, Python provides floor division (`//`) and the modulo (`%`) operator:

````python
```python
>>> 17 / 3  # Classic division returns a float
5.666666666666667
>>> 17 // 3  # Floor division discards the fractional part
5
>>> 17 % 3  # The % operator returns the remainder of the division
2
>>> 5 * 3 + 2  # Result * divisor + remainder
17
````

* **Floor Division and Modulo:** Useful for obtaining both quotient and remainder in division.
    

#### Exponentiation

Python makes it easy to calculate powers using the `**` operator:

````python
```python
>>> 5 ** 2  # 5 squared
25
>>> 2 ** 7  # 2 to the power of 7
128
````

* **Exponentiation:** Use `**` to raise a number to a certain power.
    

#### Variables and Assignments

In Python, you can store values in variables for later use:

````python
```python
>>> width = 20
>>> height = 5 * 9
>>> width * height
900
````

* **Variables and Assignments:** Use variables to store and manipulate values.
    

#### Error Handling

If you try to use a variable that hasn't been defined, Python raises a `NameError`:

````python
```python
>>> n  # Try to access an undefined variable
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'n' is not defined
````

* **Error Handling:** Ensure variables are defined before using them.
    

#### Floating-Point Operations

Python fully supports floating-point operations:

````python
```python
>>> 3 * 3.75 / 1.5
7.5
>>> 7.0 / 2
3.5
````

* **Floating-Point Operations:** Decimal numbers are crucial for precise calculations.
    

#### Special Variable \_

In interactive mode, the last printed expression is stored in the special variable `_`:

````python
```python
>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_, 2)
113.06
````

* **Special Variable** `_`: Holds the value of the last printed expression, making it handy for continued calculations.
    

#### Additional Numeric Types

Python goes beyond basic numbers, supporting Decimal, Fraction, and even complex numbers:

````python
```python
>>> complex_num = 3 + 5j
>>> type(complex_num)
<class 'complex'>
````

* **Additional Numeric Types:** Python offers advanced numeric types for specific use cases.
    

Understanding these foundational concepts will empower you to perform a wide range of calculations and set the stage for more advanced Python programming. Dive in, experiment, and enjoy your Python journey!
