#compsci #python 
# Properties
## Combinations
You can mix numeric types, eg:
```python
40+3.14 # 43.14
```

The rule is simple: Python first converts operands **up** to the type of the most complicated operand, and then performs the math on same-type operands.

Python ranks the complexity of numeric types like so: (from the simplest to the most complicated)
1. Integers
2. Floating-point numbers
3. Complex numbers

## Operator overloading and polymorphism
All Python operators may be overloaded (i.e. implemented) by [[Classes in Python|classes]] to work on objects you create. 
Furthermore, Python itself automatically overloads some operators: i.e. the + operators which sums numbers and concatenates sequences.
This property is called [[Polymorphism in Python|polymorphism]]

## Display formats
Due to the limitations of floating-point hardware, Python can display numbers differently, using **str()** and **repr()**:
```python
num='Hello World'
repr(num) # 'Hello World'
str(num) # Hello World
```
repr() produces results that look as though they were code; str() converts to a typically more user-friendly format if available. Some objects have both. 
As seen in this example, these functions can be applied to other Python data types.

## Chained comparisons
Python allows the user to chain their comparisons like this:
```python
A<B<C # similar to 'A<B and B<C'
```

## Integer precision
There **is** no soft limit for integers in Python. The only limit at all is your computer's memory.

## Hex, Oct, and Bin
Literals:
	- Octal: 0o1, 0o20, etc
	- Hex: 0x01, 0x10, etc
	- Bin: 0b1, 0b101, etc

```python
print(0o20) # 16
```

You can convert integers to other bases' digit strings using the functions:
- **oct()**
- **hex()**
- **bin()**

## Bitwise operations
Bitwise shift:
```python
x=1 # 0001
x<<2 # 0100 = 4
```
Bitwise AND and OR, XOR:
```python
x=1
x | 2 # 0011 = 3
x & 1 # 0001 = 1
x ^ 3 # 0010 = 2
```

## Floating point precision
In order to specify how precise you want a floating-point number to be, use the **Decimal** function from the **decimal** module:
```python
from decimal import Decimal
print(Decimal('0.1')+Decimal('0.1')+Decimal('0.1')-Decimal('0.3')) # Decimal('0.0'). 
```
In regular Python the answer would be close to zero, but not zero due to [[Представление чисел в компьютере|the hardware limitations]]

You even can set a global precision value with the module:
```python
import decimal
decimal.getcontext().prec = 4 # 4 digits after the decimal point
decimal.Decimal(1)/decimal.Decimal(7) # 0.1429
```

## Fractions
To create a fraction in Python, use the **fractions** module:
```python
from fractions import Fraction
x=Fraction(1,3) # Numerator, denominator
```
You can also represent floating-point numbers as fractions using the **.as_integer_ratio()** method:
```python
(2.5).as_integer_ratio() # (5, 2) - num, den
```
