From f7dc61226c88c3f877d473cb71ca4142a5e7cad5 Mon Sep 17 00:00:00 2001
From: Pradnya Gaitonde <116059908+PradnyaGaitonde@users.noreply.github.com>
Date: Wed, 5 Jun 2024 00:33:22 +0530
Subject: [PATCH 1/2] Create Type_Hinting.md
Added Content on Type hinting in python
---
contrib/advanced-python/Type_Hinting.md | 106 ++++++++++++++++++++++++
1 file changed, 106 insertions(+)
create mode 100644 contrib/advanced-python/Type_Hinting.md
diff --git a/contrib/advanced-python/Type_Hinting.md b/contrib/advanced-python/Type_Hinting.md
new file mode 100644
index 0000000..fcf1e1c
--- /dev/null
+++ b/contrib/advanced-python/Type_Hinting.md
@@ -0,0 +1,106 @@
+# Introduction to Type Hinting in Python
+Type hinting is a feature in Python that allows you to specify the expected data types of variables, function arguments, and return values. It was introduced
+in Python 3.5 via PEP 484 and has since become a standard practice to improve code readability and facilitate static analysis tools.
+
+**Benefits of Type Hinting**
+
+1. Improved Readability: Type hints make it clear what type of data is expected, making the code easier to understand for others and your future self.
+2. Error Detection: Static analysis tools like MyPy can use type hints to detect type errors before runtime, reducing bugs and improving code quality.
+3.Better Tooling Support: Modern IDEs and editors can leverage type hints to provide better autocompletion, refactoring, and error checking features.
+4. Documentation: Type hints serve as a form of documentation, indicating the intended usage of functions and classes.
+
+**Syntax of Type Hinting**
+Type hints can be added to variables, function arguments, and return values using annotations.
+
+1. Variable Annotations:
+
+```bash
+age: int = 25
+name: str = "Alice"
+is_student: bool = True
+```
+
+2. Function Annotations:
+
+```bash
+def greet(name: str) -> str:
+ return f"Hello, {name}!"
+```
+
+3. Multiple Arguments and Return Types:
+
+```bash
+def add(a: int, b: int) -> int:
+ return a + b
+```
+
+4. Optional Types: Use the Optional type from the typing module for values that could be None.
+
+```bash
+from typing import Optional
+
+def get_user_name(user_id: int) -> Optional[str]:
+ # Function logic here
+ return None # Example return value
+```
+
+5. Union Types: Use the Union type when a variable can be of multiple types.
+
+```bash
+from typing import Union
+
+def get_value(key: str) -> Union[int, str]:
+ # Function logic here
+ return "value" # Example return value
+```
+
+6. List and Dictionary Types: Use the List and Dict types from the typing module for collections.
+
+```bash
+from typing import List, Dict
+
+def process_data(data: List[int]) -> Dict[str, int]:
+ # Function logic here
+ return {"sum": sum(data)} # Example return value
+```
+
+7. Type Aliases: Create type aliases for complex types to make the code more readable.
+
+```bash
+from typing import List, Tuple
+
+Coordinates = List[Tuple[int, int]]
+
+def draw_shape(points: Coordinates) -> None:
+ # Function logic here
+ pass
+```
+
+**Example of Type Hinting in a Class**
+Here is a more comprehensive example using type hints in a class:
+
+```bash
+from typing import List
+
+class Student:
+ def __init__(self, name: str, age: int, grades: List[int]) -> None:
+ self.name = name
+ self.age = age
+ self.grades = grades
+
+ def average_grade(self) -> float:
+ return sum(self.grades) / len(self.grades)
+
+ def add_grade(self, grade: int) -> None:
+ self.grades.append(grade)
+
+# Example usage
+student = Student("Alice", 20, [90, 85, 88])
+print(student.average_grade()) # Output: 87.66666666666667
+student.add_grade(92)
+print(student.average_grade()) # Output: 88.75
+```
+
+### Conclusion
+Type hinting in Python enhances code readability, facilitates error detection through static analysis, and improves tooling support. By adopting
+type hinting, you can write clearer and more maintainable code, reducing the likelihood of bugs and making your codebase easier to navigate for yourself and others.
From 98ad137d915cdb5039045aa0ee7f0c696c02ade5 Mon Sep 17 00:00:00 2001
From: Ashita Prasad
Date: Sat, 8 Jun 2024 11:34:42 +0530
Subject: [PATCH 2/2] Rename Type_Hinting.md to type-hinting.md
---
contrib/advanced-python/{Type_Hinting.md => type-hinting.md} | 0
1 file changed, 0 insertions(+), 0 deletions(-)
rename contrib/advanced-python/{Type_Hinting.md => type-hinting.md} (100%)
diff --git a/contrib/advanced-python/Type_Hinting.md b/contrib/advanced-python/type-hinting.md
similarity index 100%
rename from contrib/advanced-python/Type_Hinting.md
rename to contrib/advanced-python/type-hinting.md