Optional[dict]: ... # returns dict or None # Union type (Python 3.10+ shorthand: str | int) from typing import Union def parse(value: Union[str, int]) -> str: return str(value) # Python 3.10+ shorthand def parse310(value: str | int) -> str: return str(value) # List, Dict, Tuple from typing (3.9+ can use built-ins directly) from typing import List, Dict, Tuple def process(records: List[Dict[str, int]]) -> Tuple[int, int]: ... From Python 3.9, you can use built-in collection types directly in annotations: list[int], dict[str, float], tuple[int, str] — no import from typing needed. From 3.10, X | Y replaces Union[X, Y]. Running mypy --strict script.py treats all un-annotated parameters as errors, giving you full type safety."> Optional[dict]: ... # returns dict or None # Union type (Python 3.10+ shorthand: str | int) from typing import Union def parse(value: Union[str, int]) -> str: return str(value) # Python 3.10+ shorthand def parse310(value: str | int) -> str: return str(value) # List, Dict, Tuple from typing (3.9+ can use built-ins directly) from typing import List, Dict, Tuple def process(records: List[Dict[str, int]]) -> Tuple[int, int]: ... From Python 3.9, you can use built-in collection types directly in annotations: list[int], dict[str, float], tuple[int, str] — no import from typing needed. From 3.10, X | Y replaces Union[X, Y]. Running mypy --strict script.py treats all un-annotated parameters as errors, giving you full type safety." />

Prev Next

Python / Core Python Fundamentals Interview Questions

How do Python type hints work and how do you use them in function signatures?

Type hints (PEP 484, Python 3.5+) let you annotate variables, function parameters, and return values with expected types. They are completely ignored at runtime by the interpreter but can be checked statically by tools like mypy, pyright, and IDE analysers, catching type errors before code ever runs.

def calculate_discount(price: float, pct: float) -> float:
    """Return the discounted price."""
    return price * (1 - pct / 100)

# Variable annotations
name: str = 'Alice'
items: list[int] = []

# Optional — value may be the type or None
from typing import Optional
def find_user(uid: int) -> Optional[dict]:
    ...  # returns dict or None

# Union type (Python 3.10+ shorthand: str | int)
from typing import Union
def parse(value: Union[str, int]) -> str:
    return str(value)

# Python 3.10+ shorthand
def parse310(value: str | int) -> str:
    return str(value)

# List, Dict, Tuple from typing (3.9+ can use built-ins directly)
from typing import List, Dict, Tuple
def process(records: List[Dict[str, int]]) -> Tuple[int, int]:
    ...

From Python 3.9, you can use built-in collection types directly in annotations: list[int], dict[str, float], tuple[int, str] — no import from typing needed. From 3.10, X | Y replaces Union[X, Y]. Running mypy --strict script.py treats all un-annotated parameters as errors, giving you full type safety.

Do Python type hints change program behaviour at runtime?
What is the Python 3.10+ shorthand for Optional[str] (str or None)?

Invest now in Acorns!!! 🚀 Join Acorns and get your $5 bonus!

Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!

Earn passively and while sleeping

Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.

Invest now!!! Get Free equity stock (US, UK only)!

Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.

The Robinhood app makes it easy to trade stocks, crypto and more.


Webull! Receive free stock by signing up using the link: Webull signup.

More Related questions...

What is Python and what makes it popular for software development? How do variables work in Python, and what does dynamic typing mean? How do conditional statements work in Python? How does the for loop work in Python, and what is the role of range()? When should you use a while loop instead of a for loop in Python? What is a Python list and what operations are most commonly used? What is a tuple in Python and when should you choose it over a list? How do Python dictionaries work and what are the most important operations? What is a Python set and what makes it useful for membership testing? How do you define and call a function in Python? What is variable scope in Python and how does the LEGB rule work? How does exception handling work in Python using try/except? What are the different ways to format strings in Python, and which is preferred? What is list comprehension and how does it differ from a regular for loop? What are *args and **kwargs in Python function definitions? What is a lambda function in Python and when is it appropriate to use one? What is the mutable default argument trap in Python and how do you fix it? How do you use dictionary comprehension to transform data payloads in Python? How does slicing work in Python for lists, strings, and tuples? What is tuple unpacking and extended unpacking in Python? What do the pass, break, and continue statements do in Python loops? What is None in Python, and when should you use 'is' versus '=='? How do you work with nested data structures such as a list of dictionaries? What is a generator in Python and how does it differ from a list? What is a decorator in Python and how do you write one? How do you define a class in Python, and what is the role of __init__? How does inheritance work in Python and what is method resolution order (MRO)? How do you read from and write to files in Python? How do Python modules and imports work? Which Python built-in functions are most important to know for coding interviews? How does Python determine whether a custom object is truthy or falsy? What is the difference between a shallow copy and a deep copy in Python? Which Python string methods are most useful for cleaning and parsing data payloads? How do enumerate() and zip() make loops more Pythonic? What is the Python exception class hierarchy and how do you create custom exceptions? What is the walrus operator (:=) and when is it useful? How does Python's sort work, and what is the difference between sort() and sorted()? What are Python dataclasses and when should you use them instead of regular classes? What is a context manager in Python and how do you implement one? How do Python type hints work and how do you use them in function signatures? Can you create a tuple comprehension in Python, and what is a generator expression? How does recursion work in Python and what are its limitations? How do you parse and build JSON payloads in Python? Why should you use Python's logging module instead of print() in production code? What is PEP 8 and which conventions does it define for Python code?
Show more question and Answers...

Data Science Essentials Interview Questions

Comments & Discussions