PYTHOPIACHANNEL Telegram 194
📌 معرفی کتابخونه typing در پایتون

شاید برات پیش اومده باشه که وقتی کدی می‌نویسی، بعد از یه مدت یادت میره یه تابع دقیقا چه ورودی می‌گرفت یا خروجیش چی بود 🤔
توی اینجور مواقع کتابخونه‌ی typing به دادت می‌رسه!

کتابخونه typing چیه؟
یه کتابخونه داخلی (built-in) توی پایتونه که برای type hinting یا همون "اشاره به نوع داده‌ها" استفاده میشه.
باهاش می‌تونی به پایتون و بقیه برنامه‌نویس‌ها بفهمونی:
🔹 ورودی یه تابع چه نوعیه
🔹 خروجی تابع چی برمی‌گردونه
🔹 یا یه متغیر چه نوع داده‌ایه


یکی از پرکاربردترین‌ها: List

با List می‌گی که لیستت دقیقا چه نوع داده‌ای رو نگه می‌داره.

🔹 مثال ۱: لیست از عدد صحیح
from typing import List

def sum_numbers(numbers: List[int]) -> int:
return sum(numbers)

print(sum_numbers([1, 2, 3])) # 6


🔹 مثال ۲: لیست از رشته‌ها
def join_words(words: List[str]) -> str:
return " ".join(words)

print(join_words(["world", "hi"]))
#hi world


🔹 مثال ۳: لیست توی لیست
def matrix_size(matrix: List[List[int]]) -> str:
return f"{len(matrix)} x {len(matrix[0])}"

print(matrix_size([[1, 2], [3, 4], [5, 6]])) # "3 x 2"


⚡️ نکته مهم:
از پایتون ۳.۹ به بعد لازم نیست از typing.List استفاده کنی، می‌تونی راحت‌تر بنویسی:

def sum_numbers(numbers: list[int]) -> int:
return sum(numbers)


کتابخونه‌ی typing فقط برای List نیست، کلی ابزار دیگه هم داره مثل Dict, Tuple, Optional, Union و... که همه‌شون برای خواناتر و قابل‌اعتمادتر شدن کدت به کار میان

@pythopiachannel
8🔥4👏1



tgoop.com/pythopiachannel/194
Create:
Last Update:

📌 معرفی کتابخونه typing در پایتون

شاید برات پیش اومده باشه که وقتی کدی می‌نویسی، بعد از یه مدت یادت میره یه تابع دقیقا چه ورودی می‌گرفت یا خروجیش چی بود 🤔
توی اینجور مواقع کتابخونه‌ی typing به دادت می‌رسه!

کتابخونه typing چیه؟
یه کتابخونه داخلی (built-in) توی پایتونه که برای type hinting یا همون "اشاره به نوع داده‌ها" استفاده میشه.
باهاش می‌تونی به پایتون و بقیه برنامه‌نویس‌ها بفهمونی:
🔹 ورودی یه تابع چه نوعیه
🔹 خروجی تابع چی برمی‌گردونه
🔹 یا یه متغیر چه نوع داده‌ایه


یکی از پرکاربردترین‌ها: List

با List می‌گی که لیستت دقیقا چه نوع داده‌ای رو نگه می‌داره.

🔹 مثال ۱: لیست از عدد صحیح
from typing import List

def sum_numbers(numbers: List[int]) -> int:
return sum(numbers)

print(sum_numbers([1, 2, 3])) # 6


🔹 مثال ۲: لیست از رشته‌ها
def join_words(words: List[str]) -> str:
return " ".join(words)

print(join_words(["world", "hi"]))
#hi world


🔹 مثال ۳: لیست توی لیست
def matrix_size(matrix: List[List[int]]) -> str:
return f"{len(matrix)} x {len(matrix[0])}"

print(matrix_size([[1, 2], [3, 4], [5, 6]])) # "3 x 2"


⚡️ نکته مهم:
از پایتون ۳.۹ به بعد لازم نیست از typing.List استفاده کنی، می‌تونی راحت‌تر بنویسی:

def sum_numbers(numbers: list[int]) -> int:
return sum(numbers)


کتابخونه‌ی typing فقط برای List نیست، کلی ابزار دیگه هم داره مثل Dict, Tuple, Optional, Union و... که همه‌شون برای خواناتر و قابل‌اعتمادتر شدن کدت به کار میان

@pythopiachannel

BY Pythopia


Share with your friend now:
tgoop.com/pythopiachannel/194

View MORE
Open in Telegram


Telegram News

Date: |

There have been several contributions to the group with members posting voice notes of screaming, yelling, groaning, and wailing in different rhythms and pitches. Calling out the “degenerate” community or the crypto obsessives that engage in high-risk trading, Co-founder of NFT renting protocol Rentable World emiliano.eth shared this group on his Twitter. He wrote: “hey degen, are you stressed? Just let it out all out. Voice only tg channel for screaming”. The visual aspect of channels is very critical. In fact, design is the first thing that a potential subscriber pays attention to, even though unconsciously. Unlimited number of subscribers per channel Among the requests, the Brazilian electoral Court wanted to know if they could obtain data on the origins of malicious content posted on the platform. According to the TSE, this would enable the authorities to track false content and identify the user responsible for publishing it in the first place. 1What is Telegram Channels?
from us


Telegram Pythopia
FROM American