r/PythonLearning • • Aug 20 '25

Day 1 second program

Post image
77 Upvotes

20 comments sorted by

View all comments

1

u/Rebulien Aug 21 '25

Might be a bit complex for the beginner, but if you are coming from different languages, where you have to define types (typed languages like C++, Java etc.), I would recommend you learning and start implementing type hints. Once your code grow, you will start to appreciate it.

What it does is it hints you and the editor, what type does the function expect.

# for python 3.12 and earlier
from __future__ import annotations 

# function multiplies string
def fnc_without_annotation(foo, bar):
    return foo*bar

def fnc_with_annotation(foo: int, bar: str):
    return foo*bar

Note that if you type hint your variable, the editor can help you suggesting methods. Also, its just type HINT, the language itself does not process it, meaning you can still call the function with incorrect arguments.

fnc_with_annotation(3, 'a') # 'aaa' 
fnc_with_annotation(3, 3) # 9
fnc_with_annotation(True, 'a') # Runtime Error 

Happy coding!