You are coding the wrong way in Python if you aren't using these two libraries
Tags: python, beginners, todayilearned, codenewbie
Published: 2021-06-27 14:48:49

![https://media.giphy.com/media/4OBq5v6J4pgJuZ2Cnj/giphy.gif](https://media.giphy.com/media/4OBq5v6J4pgJuZ2Cnj/giphy.gif)
_Hello amazing developers_

Table of Contents
- Motivation
- Introduction
- Typing
  - Variables
  - Lists
  - Dictionary
  - Functions
  - Classes
- Conclusion
- Resources

## Motivation

What makes people love (sometimes hate 😛) TypeScript more than JavaScript?

It's the typing, the type safety. They know that there will be a safety net beneath them that catches many bugs and shouts at us if we do mistakes. 

It's not just that it's the auto-suggestion that matters too, it would feel amazing right when you get the best auto completes and the methods for the variable.

Now what if I say we can you could kinda get it in python. It feels great right.

![https://media.giphy.com/media/aWPGuTlDqq2yc/giphy.gif](https://media.giphy.com/media/aWPGuTlDqq2yc/giphy.gif)

Now since we know python is an interpreted language and it doesn't have complier for itself so we can't entirely replicate typescript but at least try to use types wherever possible.

TL;DR on how typescript works, it takes the entire entire script code complies it into a JavaScript file, the complier does all the type checking.

**Python is a dynamically typed language**

## Introduction

From python 3.5 we have this amazing library which is build inside python, it's the  `typing` library. We can't cover entire library but applying Pareto principle (80-20 rule) I will try to cover a few important parts of the library.

We use it along with a power type checking library `mypy`.

We will be sliding over the following topics in brief

- Basic Variables
- Lists
- Dictionary
- Functions
- Classes

![https://media.giphy.com/media/cAhGmJPBqsXAlDTYHi/giphy.gif](https://media.giphy.com/media/cAhGmJPBqsXAlDTYHi/giphy.gif)

Let's start 

IDE Setup

Please install the following extensions for VSCode

[MyPy](https://marketplace.visualstudio.com/items?itemName=matangover.mypy)

[Error Lens](https://marketplace.visualstudio.com/items?itemName=usernamehw.errorlens) (Optional)

## Typing

### Variables

- Integer
- Float
- String

```python
int_typed:int = 4

float_typed:float = 1.2

string_typed:str = "hello"
```

Let us see what happens if we try to assign them a different value.

![Int Error](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u4rjz7dvmg9s3vtx23c6.png)

### Lists

To know more about Lists you have to know about `Sequence` 

Sequence

> In Python, sequence is the generic term for an ordered set. There are several types of sequences in Python, the following three are the most important.

Lists

> Lists are the most versatile sequence type. The elements of a list can be any object, and lists are mutable - they can be changed. Elements can be reassigned or removed, and new elements can be inserted.

```python
from typing import List
int_typed_list:List[int] = []
```
![List](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kuzvqcmwhbl6c34sg1og.png)
_Shouts for type errors_


But but but but in `TypeScript` we have `any` keyword if we want dynamic array

Yes the golden `Any` even exists here too

```python
from typing import Any, List

int_typed:int = 4

float_typed:float = 1.2

string_typed:str = "hello"

int_typed_list:List[int] = []

int_typed_list.append(int_typed)

any_typed_list: List[Any] = []
any_typed_list.append(int_typed)
any_typed_list.append(string_typed)
any_typed_list.append(float_typed)
any_typed_list.append(int_typed_list)
```
![Power of any](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sz4sqqgzhn68en4oq472.png)
_No shouting 🤫_


### Dictionary

For this section *Picture speaks louder than words*

```python
from typing import Dict

x: Dict[str, int] = {'followers': 1110} 

x['abc'] = 123

x.keys()

x.values()
```

![Overall](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bzf36nnr10i3ohw0p8zq.png)
_It reads the keys if we define in code and help us in auto complete_

![Keys](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/18l3x81x3k50nm2b8syd.png)
_Now we know that `keys()` returns a list of `str`_


![Values](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rcs2gw6m3atd8yu0csaq.png)
_Now we know that `values()` returns a list of `int`_


### Functions

My all time favourite definition of function is depicted in the picture below.

![Function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h5hr8nplxionk7ojmu8t.png)

```python
def get_avg(num1:int, num2:int) -> float:
    return (num1+num2)/2

get_avg(12,9)
```

![Function shouts](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4hhzz1zcdgsv4lj1r1xa.png)
_Function shouts_

### Classes

We may need a solution when we require custom classes to hold and use our data. Then we could similar to this

```python
import math as m

class Vector:
    def __init__(self, x:float,y:float) -> None:
        self.x = x
        self.y = y
    
    def calculate_magnitude(self) -> float:
        return m.sqrt(m.pow(self.x,2)+ m.pow(self.y,2))

v = Vector(1,1)
```
![Vector Class](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v68cydwksox7ya2t8dja.png)

You get the beautiful autocomplete again with details 💫

## Conclusion

![With great power comes great responsibility](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/djulqav3533qk13c5021.png)

As said in the picture, with great power comes great responsibility.
Python dynamic types gives us very easy to get started with learning python but at the same time increases the chances to create bugs unintentionally.

The typing gets even better in Python 3.9 and I hope it gets better and better as we progress.

Small changes can great a huge impact in the code, so please start using types, MyPy and feel safe.

I kept it brief but please do let me know if diving deeper helps, feedback is really appreciated.

## Resources

[MyPY](https://mypy.readthedocs.io/en/stable/index.html)

[Typing](https://docs.python.org/3/library/typing.html)

P.S My [Twitter](https://twitter.com/gillarohith) DMs are always open if you want to discuss collaboration opportunities or request on writing for a topic 

![https://media1.tenor.com/images/40317e3f3b28d57c89edceb1d75832c2/tenor.gif?itemid=16119732](https://media1.tenor.com/images/40317e3f3b28d57c89edceb1d75832c2/tenor.gif?itemid=16119732)

Thanks
Rohith Gilla