> For the complete documentation index, see [llms.txt](https://openai.gitbook.io/code-cheatsheets/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://openai.gitbook.io/code-cheatsheets/all/python/2.7v3.5.md).

# 2.7v3.5

## Comparisons

#### Strings

|                    2 | 3                        |
| -------------------: | ------------------------ |
| Default 8-bit String | Default unicode          |
|  `u'Hi'` for unicode | `b'Hi'` for 8-bit string |

#### Division

2, truncates by default:

`3/2` => 1

`3.0//2` => 1.0

3, // for truncating

`3.0/2` => 1

`3//2` => 1

#### Iterators 3 vs Lists 2

Optimized native functions used iterators in 3 vs lists in 2

i.e `range`, `map`, `filter`, `dict.[ft]`, `zip`

#### Print

In 2 can do `print Hello`

In 3, print is a regular ft so must include () `print(Hello)`

**Raw Input in 2.7 s now input in 3**

## 2.7 Specific Stuff

```python
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
```

Import python 3 functionality(good for migration and changes fundamental python props), needs to be near top of code because imported differently and changes things about language

## 3.5 New Features

### Dictionary Comprehensions

`{k: v for k, v in stuff}`

### Iterable Unwrapping

```python
#Python 2
first, rest, last = seq[0], seq[1:-1], seq[-1]
#Python 3
first, *rest, last = seq
```

### Objects

python 3 has different objects or something look it up

### Formatting Print

```python
name = 'World'
program = 'Python'
print(f'Hello {name}! This is {program}')
```

### Typing Hinting

This doesn't make python a staticly typed language suddenly, but basically inline comments

```python
def greeting(name: str) -> str:
    return 'Hello ' + name
```

#### Type Alias

```python
from typing import List
Vector = List[float]

def scale(scalar: float, vector: Vector) -> Vector:
    return [scalar * num for num in vector]

# typechecks; a list of floats qualifies as a Vector.
new_vector = scale(2.0, [1.0, -4.2, 5.4])
```

#### Dataclass

Actually creates a \_\_str and \_\_init for you

```python
from dataclasses import dataclass

@dataclass
class Armor:
    armor: float
    description: str
    level: int = 1

    def power(self) -> float:
        return self.armor * self.level

armor = Armor(5.2, "Common armor.", 2)
armor.power()
# 10.4
print(armor)
```

### Actually Type Checking

Use pycharm or something with this (atom has a mypy plugin)

OR

`pip install mypy`

`mypy [file]`


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://openai.gitbook.io/code-cheatsheets/all/python/2.7v3.5.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
