This shows you the differences between two versions of the page.
ic:resurse:python [2023/10/02 00:08] razvan.smadu [Python3 Crash Course] |
ic:resurse:python [2023/10/02 00:10] (current) razvan.smadu [Python3 Crash Course] |
||
---|---|---|---|
Line 264: | Line 264: | ||
Exemple: | Exemple: | ||
<code python> | <code python> | ||
- | from typing import List, Sequence, Tuple, TypeVar | + | from typing import Dict, List, Sequence, Tuple, TypeVar |
my_string: str = "hello world" # A variable storing a string | my_string: str = "hello world" # A variable storing a string | ||
my_number: int = 10 # A variable storing an integer | my_number: int = 10 # A variable storing an integer | ||
- | |||
# A function returning nothing | # A function returning nothing | ||
def main() -> None: | def main() -> None: | ||
return # Returns nothing (i.e., None) | return # Returns nothing (i.e., None) | ||
- | |||
# A function that takes some arguments, and returns a tuple | # A function that takes some arguments, and returns a tuple | ||
def compute(a: int, b: float, c: List[str]) -> Tuple[int, float]: | def compute(a: int, b: float, c: List[str]) -> Tuple[int, float]: | ||
return a, b # This is a tuple of `a` and `b` | return a, b # This is a tuple of `a` and `b` | ||
- | |||
T = TypeVar('T') # Declare type variable | T = TypeVar('T') # Declare type variable | ||
Line 285: | Line 281: | ||
def first(l: Sequence[T]) -> T: # A generic function | def first(l: Sequence[T]) -> T: # A generic function | ||
return l[0] | return l[0] | ||
+ | |||
+ | # Type alias | ||
+ | MyCustomType = Dict[str, List[Tuple[int, int]]] | ||
</code> | </code> | ||