اپراتورهای پایتون
اپراتورهای پایتون
از اپراتورها برای انجام عملیات بر روی متغیرها و مقادیر استفاده می شود.
در مثال زیر ، ما از +عملگر برای جمع کردن دو مقدار استفاده می کنیم:
مثال
print(10 + 5)
پایتون اپراتورها را در گروه های زیر تقسیم می کند:
- عملگرهای حسابی
- اپراتورهای واگذاری
- اپراتورهای مقایسه
- عملگرهای منطقی
- اپراتورهای هویت
- اپراتورهای عضویت
- عملگرهای بیتی
اپراتورهای حساب پایتون
عملگرهای حساب با مقادیر عددی برای انجام عملیات رایج ریاضی استفاده می شوند:
| Operator | Name | Example | |
|---|---|---|---|
| + | Addition | x + y | |
| - | Subtraction | x - y | |
| * | Multiplication | x * y | |
| / | Division | x / y | |
| % | Modulus | x % y | |
| ** | Exponentiation | x ** y | |
| // | Floor division | x // y |
اپراتورهای واگذاری پایتون
عملگرهای انتساب برای اختصاص مقادیر به متغیرها استفاده می شوند:
| Operator | Example | Same As | Try it |
|---|---|---|---|
| = | x = 5 | x = 5 | |
| += | x += 3 | x = x + 3 | |
| -= | x -= 3 | x = x - 3 | |
| *= | x *= 3 | x = x * 3 | |
| /= | x /= 3 | x = x / 3 | |
| %= | x %= 3 | x = x % 3 | |
| //= | x //= 3 | x = x // 3 | |
| **= | x **= 3 | x = x ** 3 | |
| &= | x &= 3 | x = x & 3 | |
| |= | x |= 3 | x = x | 3 | |
| ^= | x ^= 3 | x = x ^ 3 | |
| >>= | x >>= 3 | x = x >> 3 | |
| <<= | x <<= 3 | x = x << 3 |
اپراتورهای مقایسه پایتون
برای مقایسه دو مقدار از عملگرهای مقایسه استفاده می شود:
| Operator | Name | Example | |
|---|---|---|---|
| == | Equal | x == y | |
| != | Not equal | x != y | |
| > | Greater than | x > y | |
| < | Less than | x < y | |
| >= | Greater than or equal to | x >= y | |
| <= | Less than or equal to | x <= y |
اپراتورهای منطقی پایتون
از عملگرهای منطقی برای ترکیب عبارات شرطی استفاده می شود:
| Operator | Description | Example | Try it |
|---|---|---|---|
| and | Returns True if both statements are true | x < 5 and x < 10 | Try it » |
| or | Returns True if one of the statements is true | x < 5 or x < 4 | Try it » |
| not | Reverse the result, returns False if the result is true | not(x < 5 and x < 10) | Try it » |
اپراتورهای هویت پایتون
از عملگرهای هویت برای مقایسه اشیا استفاده می شود ، نه اگر برابر باشند ، اما اگر در واقع همان شی با محل حافظه مشابه باشند:
| Operator | Description | Example | Try it |
|---|---|---|---|
| is | Returns True if both variables are the same object | x is y | Try it » |
| is not | Returns True if both variables are not the same object | x is not y | Try it » |
اپراتورهای عضویت پایتون
از اپراتورهای عضویت برای آزمایش اینکه توالی در یک شی ارائه شده استفاده می شود:
| Operator | Description | Example | Try it |
|---|---|---|---|
| in | Returns True if a sequence with the specified value is present in the object | x in y | Try it » |
| not in | Returns True if a sequence with the specified value is not present in the object | x not in y | Try it » |
اپراتورهای پایتون Bitwise
از عملگرهای بیتی برای مقایسه اعداد (باینری) استفاده می شود:
| Operator | Name | Description |
|---|---|---|
| & | AND | Sets each bit to 1 if both bits are 1 |
| | | OR | Sets each bit to 1 if one of two bits is 1 |
| ^ | XOR | Sets each bit to 1 if only one of two bits is 1 |
| ~ | NOT | Inverts all the bits |
| << | Zero fill left shift | Shift left by pushing zeros in from the right and let the leftmost bits fall off |
| >> | Signed right shift | Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off |
+ نوشته شده در شنبه بیست و دوم خرداد ۱۴۰۰ ساعت 15:20 توسط علی رضا نقش
|