انواع داده های داخلی
در برنامه نویسی ، نوع داده مفهوم مهمی است.
متغیرها می توانند داده های مختلفی را ذخیره کنند و انواع مختلفی نیز می توانند کارهای مختلفی انجام دهند.
Python انواع داده های زیر را دارد که بصورت پیش فرض در این دسته ها ساخته شده است:
نوع متن:
str | Text Type |
int, float, complex | Numeric Types |
list, tuple, range | Sequence Types |
dict | Mapping Type |
set, frozenset | Set Types |
bool | Boolean Type |
bytes, bytearray, memoryview | Binary Types |
دریافت نوع داده
با استفاده از type()
می توانید نوع داده های هر شی را بدست آورید:
Translate from: English۴۷/۵۰۰۰مثال
نوع داده متغیر x را چاپ کنید:
x = 5
print(type(x))
تنظیم نوع داده
در پایتون ، وقتی مقدار را به متغیر اختصاص می دهید ، نوع داده تنظیم می شود:
Data Type | Example |
str | x = “Hello World” |
int | x = 20 |
float | x = 20.5 |
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)) |
نوع داده خاص را تنظیم کنید
اگر می خواهید نوع داده را مشخص کنید ، می توانید از توابع سازنده زیر استفاده کنید:
Data Type | Example |
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)) |