A learning repository for Python type hints and the typing module.
This repo contains examples and explanations of common Python typing features like List, Tuple, Set, Dict, Union, Optional, Any, Callable, and more.
- Basic Python type hints (
int,str,float, etc.) - Typing module examples:
List,Tuple,Set,DictUnion,Optional,Any,Callable
- Real-world examples:
- Counting letters in a string
- Using
Callablefor function parameters - Combining
Union,Optional, andAny
- Safe dictionary access using
.get(),.keys(),.values(),.items() - List comprehensions and filtering examples
- Step-by-step explanations for beginners
from typing import List, Dict
def count_letters(word: str) -> Dict[str, int]:
counts: Dict[str, int] = {}
for letter in word:
counts[letter] = counts.get(letter, 0) + 1
return counts
print(count_letters("banana"))
# Output: {'b': 1, 'a': 3, 'n': 2}