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

از اپراتورها برای انجام عملیات بر روی متغیرها و مقادیر استفاده می شود.

در مثال زیر ، ما از +عملگر برای جمع کردن دو مقدار استفاده می کنیم:

مثال

print(10 + 5)

اجرای مثال »

پایتون اپراتورها را در گروه های زیر تقسیم می کند:

  • عملگرهای حسابی
  • اپراتورهای واگذاری
  • اپراتورهای مقایسه
  • عملگرهای منطقی
  • اپراتورهای هویت
  • اپراتورهای عضویت
  • عملگرهای بیتی

اپراتورهای حساب پایتون

عملگرهای حساب با مقادیر عددی برای انجام عملیات رایج ریاضی استفاده می شوند:

OperatorNameExample 
+Additionx + y 
-Subtractionx - y 
*Multiplicationx * y 
/Divisionx / y 
%Modulusx % y 
**Exponentiationx ** y 
//Floor divisionx // y 

اپراتورهای واگذاری پایتون

عملگرهای انتساب برای اختصاص مقادیر به متغیرها استفاده می شوند:

OperatorExampleSame AsTry it
=x = 5x = 5 
+=x += 3x = x + 3 
-=x -= 3x = x - 3 
*=x *= 3x = x * 3 
/=x /= 3x = x / 3 
%=x %= 3x = x % 3 
//=x //= 3x = x // 3 
**=x **= 3x = x ** 3 
&=x &= 3x = x & 3 
|=x |= 3x = x | 3 
^=x ^= 3x = x ^ 3 
>>=x >>= 3x = x >> 3 
<<=x <<= 3x = x << 3 


اپراتورهای مقایسه پایتون

برای مقایسه دو مقدار از عملگرهای مقایسه استفاده می شود:

OperatorNameExample 
==Equalx == y 
!=Not equalx != y 
>Greater thanx > y 
<Less thanx < y 
>=Greater than or equal tox >= y 
<=Less than or equal tox <= y 

اپراتورهای منطقی پایتون

از عملگرهای منطقی برای ترکیب عبارات شرطی استفاده می شود:

OperatorDescriptionExampleTry it
and Returns True if both statements are truex < 5 and  x < 10Try it »
orReturns True if one of the statements is truex < 5 or x < 4Try it »
notReverse the result, returns False if the result is truenot(x < 5 and x < 10)Try it »

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

از عملگرهای هویت برای مقایسه اشیا استفاده می شود ، نه اگر برابر باشند ، اما اگر در واقع همان شی با محل حافظه مشابه باشند:

OperatorDescriptionExampleTry it
is Returns True if both variables are the same objectx is yTry it »
is notReturns True if both variables are not the same objectx is not yTry it »

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

از اپراتورهای عضویت برای آزمایش اینکه توالی در یک شی ارائه شده استفاده می شود:

OperatorDescriptionExampleTry it
in Returns True if a sequence with the specified value is present in the objectx in yTry it »
not inReturns True if a sequence with the specified value is not present in the objectx not in yTry it »

اپراتورهای پایتون Bitwise

از عملگرهای بیتی برای مقایسه اعداد (باینری) استفاده می شود:

OperatorNameDescription
ANDSets each bit to 1 if both bits are 1
|ORSets each bit to 1 if one of two bits is 1
 ^XORSets each bit to 1 if only one of two bits is 1
NOTInverts all the bits
<<Zero fill left shiftShift left by pushing zeros in from the right and let the leftmost bits fall off
>>Signed right shiftShift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off