Python Data Types

انواع داده در پایتون

مفهوم نوع داده در برنامه‌نویسی بسیار مهم است. متغیرها می‌توانند انواع داده‌های متفاوت را در خود ذخیره کنند و هر نوع داده متفاوت کار متفاوتی را انجام می‌دهد. در پایتون به صورت پیش‌فرض انواع داده‌های زیر وجود دارد:

متنی str
عددی int float complex
دنباله‌ list tuple range
تصویر کردن (Mapping) dict
مجموعه‌ set frozenset
بولین bool
دودویی bytes bytearray memoryview
None NoneType

پیدا کردن نوع داده

برای دانستن نوع داده یک متغیر می‌توان از تابع type() استفاده کرد.


x = 5
print(type(x))

خروجی:

قرار دادن نوع داده

در پایتون به محض اختصاص دادن یک مقدار به متغیر، نوع آن متغیر قرار داده می‌شود.

نوع داده مثال
str "x = "Hello World
int 20 = x
float 20.5 = x
complex x = 1j
list x = ["apple", "banana", "cherry"]
tuple x = ("apple", "banana", "cherry")
range x = range(6)
dict x = {"name" : "John", "age" : 36}
set x = {"apple", "banana", "cherry"}
frozenset x = frozenset({"apple", "banana", "cherry"})
bool x = True
bytes "x = b"Hello
bytearray x = bytearray(5)
memoryview x = memoryview(bytes(5))
NoneType x = None

قرار دادن نوع داده به صورت خاص

اگر می‌خواهید که خودتان نوع داده را تعیین کنید، از توابع سازنده زیر استفاده کنید.

نوع داده مثال
str x = str("Hello World")
int x = int(20)
float x = float(20.5)
complex x = complex(1j)
list x = list(("apple", "banana", "cherry"))
tuple x = tuple(("apple", "banana", "cherry"))
range x = range(6)
dict x = dict(name = "John", age = 36)
set x = set(("apple", "banana", "cherry"))
frozenset x = frozenset(("apple", "banana", "cherry"))
bool x = bool(5)
bytes x = bytes(5)
bytearray x = bytearray(5)
memoryview x = memoryview(bytes(5))