Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

Python Function Annotations (Complete Guide for Beginners)

In Python, function annotations are a way to add extra information (metadata) about function parameters and return values. They do NOT affect how the code runs, but they help developers understand the expected data types and purpose of a function.

Function annotations are mainly used for:

  • Code readability
  • Documentation
  • Type hinting (modern Python practice)

🔹 What are Function Annotations in Python?

Function annotations allow you to specify:

  • Expected data type of parameters
  • Expected return type

👉 They are optional and purely informational.


🔹 Syntax of Function Annotations

def function_name(param1: type, param2: type) -> return_type:
pass

🔹 Simple Example of Function Annotations

def add(a: int, b: int) -> int:
return a + b

print(add(5, 3))

🔸 Output:

8

🔍 Explanation:

  • a: int → parameter annotation
  • b: int → parameter annotation
  • -> int → return type annotation

🔹 Function Annotations with String Example

def greet(name: str) -> str:
return "Hello " + name

print(greet("Alex"))

🔸 Output:

Hello Alex

🔹 Multiple Parameter Annotations

def student(name: str, age: int, score: float) -> str:
return f"{name} is {age} years old with score {score}"

print(student("Sophy", 20, 85.5))

🔸 Output:

Sophy is 20 years old with score 85.5

🔹 Function Annotations Do NOT Enforce Types

Python does NOT restrict data types at runtime.

def add(a: int, b: int) -> int:
return a + b

print(add("10", "20"))

🔸 Output:

1020

🔍 Explanation:

  • Python does not enforce types
  • It still concatenates strings

🔹 Checking Annotations Using annotations

You can view annotations using:

def multiply(a: int, b: int) -> int:
return a * b

print(multiply.__annotations__)

🔸 Output:

{'a': <class 'int'>, 'b': <class 'int'>, 'return': <class 'int'>}

🔹 Function Annotation with Default Values

def greet(name: str = "Guest") -> str:
return "Hello " + name

print(greet())
print(greet("John"))

🔹 Real-Life Example: Calculator Function

def calculate(a: float, b: float, operation: str) -> float:
if operation == "add":
return a + b
elif operation == "multiply":
return a * b
return 0

print(calculate(5.5, 2.5, "add"))

🔸 Output:

8.0

🔹 Real-Life Example: User Registration

def register(username: str, age: int, country: str) -> str:
return f"{username} registered from {country}"

print(register("Alex", 25, "Cambodia"))

🔹 Why Use Function Annotations?

✔ Improves code readability
✔ Helps teamwork in large projects
✔ Useful for documentation
✔ Supports type checking tools (like mypy)
✔ Makes code more professional


🔹 Function Annotations vs Type Hints

Feature              Function AnnotationsType Hints
Purpose              Metadata              Type checking
Enforced             ❌ No             ❌ No (but tools can check)
Usage        def f(x: int)              Same syntax
Python version3.0+              3.5+ standard

🔹 Key Points to Remember

  • Annotations are optional
  • They do NOT affect execution
  • Used for documentation and clarity
  • Can be accessed via __annotations__

🚀 Conclusion

Python function annotations are a powerful feature for making your code more readable and maintainable. Although they do not enforce rules at runtime, they are extremely useful for developers working in large projects or teams.

They help you write:

  • Cleaner code
  • Better documentation
  • More professional Python programs

 



Post a Comment

0 Comments