26
loading...
This website collects cookies to deliver better user experience
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.mypy
.int_typed:int = 4
float_typed:float = 1.2
string_typed:str = "hello"
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 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.
from typing import List
int_typed_list:List[int] = []
TypeScript
we have any
keyword if we want dynamic arrayAny
even exists here toofrom 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)
from typing import Dict
x: Dict[str, int] = {'followers': 1110}
x['abc'] = 123
x.keys()
x.values()
def get_avg(num1:int, num2:int) -> float:
return (num1+num2)/2
get_avg(12,9)
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)