Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃 Add typing hints examples in "Python" notebook #17

Open
clstaudt opened this issue Mar 11, 2024 · 0 comments
Open

馃 Add typing hints examples in "Python" notebook #17

clstaudt opened this issue Mar 11, 2024 · 0 comments

Comments

@clstaudt
Copy link
Collaborator

clstaudt commented Mar 11, 2024

Issue Description

The notebook currently lacks a section on type hints, which are a significant feature of modern Python programming for static type checking and code clarity.

Proposed Change

  • Add a new section dedicated to type hints, explaining their purpose and how they can be used in Python code.
  • Provide practical code examples that demonstrate how to add type hints to functions and variables.
  • Link to the official Python documentation for a more in-depth explanation.

Example Implementation

## Type Hints in Python

Type hints are a feature in Python that allow you to specify the expected data types of variables, function arguments, and return values. While type hints are not enforced at runtime, they provide developers with a clear contract of what a function expects and returns. They are especially helpful for larger codebases, static type checking, and improving IDE support with autocompletion and linting.

Here's how you can add type hints to your Python code:

### Basic Type Hints

```python
# Specifying the data type of a variable
age: int = 25

# Adding type hints to function parameters and return type
def greet(name: str) -> str:
    return f"Hello, {name}!"

Advanced Type Hints with Collections

from typing import List, Dict

# Specifying a list of integers
numbers: List[int] = [1, 2, 3, 4, 5]

# Defining a dictionary with string keys and float values
prices: Dict[str, float] = {'apple': 0.5, 'banana': 0.75}

Functions with Type Hints

from typing import Tuple

# A function that returns a tuple of a string and an integer
def get_data() -> Tuple[str, int]:
    return "data", 42

More examples

from typing import Set, Tuple

# A set of unique strings
usernames: Set[str] = {'user1', 'user2', 'admin'}

# A tuple with mixed data types
person_info: Tuple[str, int, bool] = ('Alice', 30, True)
``

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant