Python Operators

Python Operators – Python Операторууд #

Operators are used to perform operations on variables and values.

Операторуудыг хувьсагч болон утгуудад үйлдэл хийхэд ашигладаг.

In the example below, we use the + operator to add together two values:

Доорх жишээнд бид хоёр утгыг нэмэхийн тулд + операторыг ашиглана:

Example – Жишээ #

print(10 + 5)

Python divides the operators in the following groups:

Python операторуудыг дараах бүлгүүдэд хуваадаг:

  • Arithmetic operators

Арифметик операторууд

  • Assignment operators

Томилолтын операторууд

  • Comparison operators

Харьцуулалтын операторууд

  • Logical operators

Логик операторууд

  • Identity operators

Тэнцэтгэл операторууд

  • Membership operators

Бүрэлдэхүүн операторууд

  • Bitwise operators

Бит операторууд

Python Arithmetic Operators – Python Арифметик Операторууд #

Arithmetic operators are used with numeric values to perform common mathematical operations:

Арифметик операторууд нь тоон утгуудыг ашиглан ерөнхий математик үйлдлүүдийг гүйцэтгэхэд ашиглагддаг:

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  

Python Assignment Operators – Python Томилолтын Операторууд #

Assignment operators are used to assign values to variables:

Томилолтын Операторууд нь хувьсагчдад утгуудыг хуваарилахад ашиглагддаг:

Operator

Оператор

Example

Жишээ

Same As

Адилхан

 
= 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  

Python Comparison Operators – Python Харицуулалтын Оператор #

Comparison operators are used to compare two values:

Comparison операторууд нь хоёр утгыг харьцуулахад ашиглагддаг:

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  

Python Logical Operators – Python Логик Операторууд #

Logical operators are used to combine conditional statements:

Logical операторууд нь нөхцлийг нэгтгэхэд ашиглагддаг:

Operator

Оператор

Description

Тайлбар

Example

Жишээ

 
and 

Returns True if both statements are true

Хоёр нөхцөл хоёул үнэн байвал Үнэн(True) буцана

x < 5 and  x < 10  
or

Returns True if one of the statements is true

 Нэг нөхцөл үнэн байвал Үнэн(True) буцаана

x < 5 or x < 4  
not

Reverse the result, returns False if the result is true

Үр дүнгийн эсрэг, үнэн байвал Худал(False) буцаана

not(x < 5 and x < 10)  

Python Identity Operators – Python Тэнцэтгэл Операторууд #

Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location:

Identity операторууд нь объектуудыг харьцуулж, тэдгээр нь ижил объект эсэхийг шалгахад ашиглагддаг:

Operator

Оператор

Description

Тайлбар

Example

Жишээ

 
is 

Returns True if both variables are the same object

Хоёр хувьсагч ижил объект бол Үнэн(True) буцаана

x is y  
is not

Returns True if both variables are not the same object

Хоёр хувьсагч ижил объект биш бол Үнэн(True) буцаана

x is not y

Python Membership Operators – Python Бүрэлдэхүүн операторууд #

Membership operators are used to test if a sequence is presented in an object:

Membership операторууд нь дараалал объект дотор байгаа эсэхийг шалгахад ашиглагддаг:

Operator

Оператор

Description

Тайлбар

Example

 Жишээ

 
in 

Returns True if a sequence with the specified value is present in the object

Тусгай утга объект дотор байгаа бол Үнэн(True) буцаана

x in y  
not in

Returns True if a sequence with the specified value is not present in the object

Тусгай утга объект дотор байхгүй бол Үнэн(True) буцаана

x not in y

Python Bitwise Operators – Python Бит Операторууд #

Bitwise operators are used to compare (binary) numbers:

Bitwise операторууд нь (binary буюу хоёртийн) тоонуудыг харьцуулахад ашиглагддаг:

Operator

Оператор

Name

Нэр

Description

Тайлбар

Example

Жишээ

 
AND

Sets each bit to 1 if both bits are 1

Хоёр бит хоёул 1 бол 1 болно

x & y  
| OR

Sets each bit to 1 if one of two bits is 1

Хоёр битний нэг нь 1 бол 1 болно

x | y  
^ XOR

Sets each bit to 1 if only one of two bits is 1

Хоёр битний нэг нь 1, нөгөө нь 0 бол 1 болно

x ^ y  
~ NOT Inverts all the bits ~x  
<<

Zero fill left shift

Зүүн шилжүүлэх

Shift left by pushing zeros in from the right and let the leftmost bits fall off

Баруун талд 0 нэмнэ, зүүн талын битүүдийг унагаана(хасна)

x << 2  
>> Signed right shift

Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off

Зүүн талд зүүн талын битүүдийг хуулж нэмнэ, баруун талын битүүдийг унагаана

x >> 2

Operator Precedence – Анхдагч Оператор #

Operator precedence describes the order in which operations are performed.

Operator precedence нь үйлдлүүдийг ямар дарааллаар гүйцэтгэхийг тодорхойлно.

Example – Жишээ #

Parentheses has the highest precedence, meaning that expressions inside parentheses must be evaluated first:

Хаалт хамгийн өндөр анхдагч байдаг, хаалтан доторх илэрхийллийг эхлээд боддог:

print((6 + 3) - (6 + 3))

Example – Жишээ #

Multiplication * has higher precedence than addition +, and therefor multiplications are evaluated before additions:

Үржүүлэх * нь нэмэх +-ээс өндөр зэрэглэлтэй, тиймээс үржүүлэлтүүдийг эхэлж бодно:

print(100 + 5 * 3)

The precedence order is described in the table below, starting with the highest precedence at the top:

Зэрэглэлийн дараалал доорх хүснэгтэд өндөр зэрэглэлтэйгээс эхэлж бичигдсэн байна:

Operator

Оператор

Description

Тайлбар

 
()

Parentheses

Хаалт

 
**

Exponentiation

зэрэгт дэвшүүлэх

 
+x  -x  ~x

Unary plus, unary minus, and bitwise NOT

Нэгтгэж нэмэх, нэгтгэж хасах, бит БИШ

 
*  /  //  %

Multiplication, division, floor division, and modulus

Үржүүлэх, хуваах, шалгалт, үлдэгдэл

 
+  -

Addition and subtraction

Нэмэх ба хасах

 
<<  >>

Bitwise left and right shifts

Битээр зүүн ба баруун шилжүүлэх

 
&

Bitwise AND

 
^ Bitwise XOR  
| Bitwise OR  
==  !=  >  >=  <  <=  is  is not  in  not in 

Comparisons, identity, and membership operators

Харьцуулалт, Тэнцэтгэл, Бүрэлдэхүүн операторууд

 
not

Logical NOT

 
and AND  
or OR  

If two operators have the same precedence, the expression is evaluated from left to right.

Хоёр оператор адил зэрэглэлтэй бол илэрхийллийг зүүнээс баруун руу бодно.

Example – Жишээ #

Addition + and subtraction - has the same precedence, and therefor we evaluate the expression from left to right:

Нэмэх + болон хасах - адил зэрэглэлтэй, тиймээс бид илэрхийллийг зүүнээс баруун руу бодно:

print(5 + 4 - 7 + 3)

Powered by BetterDocs

Leave a Reply