+{"cells":[{"cell_type":"markdown","metadata":{},"source":["In Python, the `->` symbol is used in function annotations, specifically to indicate the return type of a function. This is part of the type hinting system introduced in PEP 484, which allows developers to specify the expected types of variables and function return values. These annotations do not enforce type checking at runtime but can be used by third-party tools, linters, and integrated development environments (IDEs) to provide better code analysis and suggestions.\n","\n","Here's an example of how the `->` symbol is used in Python:\n","\n","### Example 1: Basic Function Annotation"]},{"cell_type":"code","execution_count":9,"metadata":{},"outputs":[],"source":["def greet(name: str) -> str:\n"," return f\"Hello, {name}!\"\n"]},{"cell_type":"markdown","metadata":{},"source":["In this example:\n","\n","- `name: str` indicates that the `name` parameter should be of type `str` (string).\n","- `-> str` indicates that the function `greet` is expected to return a `str` (string)."]},{"cell_type":"markdown","metadata":{},"source":["### Example 2: Function with Multiple Parameters"]},{"cell_type":"code","execution_count":11,"metadata":{},"outputs":[],"source":["def add(a: int, b: int) -> int:\n"," return a + b\n"]},{"cell_type":"markdown","metadata":{},"source":["In this example:\n","\n","- `a: int` and `b: int` indicate that the parameters `a` and `b` should both be of type `int` (integer).\n","- `-> int` indicates that the function `add` is expected to return an `int` (integer)."]},{"cell_type":"markdown","metadata":{},"source":["### Example 3: Function Returning a List"]},{"cell_type":"code","execution_count":12,"metadata":{},"outputs":[],"source":["def get_even_numbers(n: int) -> list[int]:\n"," return [i for i in range(n) if i % 2 == 0]\n"]},{"cell_type":"markdown","metadata":{},"source":["In this example:\n","\n","- `n: int` indicates that the parameter `n` should be of type `int`.\n","- `-> list[int]` indicates that the function `get_even_numbers` is expected to return a list of integers."]},{"cell_type":"markdown","metadata":{},"source":["### Example 4: Function with Optional Return Type"]},{"cell_type":"code","execution_count":13,"metadata":{},"outputs":[],"source":["from typing import Optional\n","\n","def find_divisor(n: int) -> Optional[int]:\n"," for i in range(2, n):\n"," if n % i == 0:\n"," return i\n"," return None\n"]},{"cell_type":"markdown","metadata":{},"source":["In this example:\n","\n","- `n: int` indicates that the parameter `n` should be of type `int`.\n","- `-> Optional[int]` indicates that the function `find_divisor` may return an `int` or `None` if no divisor is found."]},{"cell_type":"markdown","metadata":{},"source":["### Example 5: Function with Custom Type"]},{"cell_type":"code","execution_count":14,"metadata":{},"outputs":[],"source":["from typing import List, Dict\n","\n","def process_data(data: List[Dict[str, int]]) -> Dict[str, int]:\n"," result = {}\n"," for item in data:\n"," for key, value in item.items():\n"," if key in result:\n"," result[key] += value\n"," else:\n"," result[key] = value\n"," return result"]},{"cell_type":"markdown","metadata":{},"source":["In this example:\n","\n","- `data: List[Dict[str, int]]` indicates that the parameter `data` should be a list of dictionaries, where each dictionary has keys of type `str` and values of type `int`.\n","- `-> Dict[str, int]` indicates that the function `process_data` is expected to return a dictionary with keys of type `str` and values of type `int`.\n","\n","### Conclusion\n","\n","The `->` symbol in Python is a part of the function annotation syntax used for type hinting. It allows developers to specify the expected return type of a function, which can improve code readability, maintenance, and tooling support. Remember that these annotations are not enforced at runtime and are primarily used for static analysis."]}],"metadata":{"colab":{"authorship_tag":"ABX9TyPqWG88n3GHNIr4gnaZsrlz","provenance":[]},"kernelspec":{"display_name":"Python 3","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.11.2"}},"nbformat":4,"nbformat_minor":0}
0 commit comments