Home Operators
Post
Cancel

Operators

Init

  • python에서는 7개 그룹의 연산자가 있음
    • Arithmetic operators
    • Assignment operators
    • Comparison operators
    • Logical operators
    • Identity operators
    • Membership operators
    • Bitwise operators

Arithmetic Operators

  • 산술 연산.
OperatorNameExample
+Addition2 + 3 = 5
-Subtraction3 - 2 = 1
*Multiplication2 * 3 = 6
/Division5 / 2 = 2.5
%Modulus7 % 2 = 1
**Exponentiation2 ** 3 = 8
//Floor division10 // 3 = 3

Comparison Operators

  • 비교연산
OperatorNameExample
==Equal to2 == 2
!=Not equal to2 != 3
<Less than2 < 3
>Greater than3 > 2
<=Less than or equal to2 <= 3
>=Greater than or equal to3 >= 2

Logical Operators

  • 논리 연산
OperatorNameExample
andLogical ANDx > 5 and x < 10
orLogical ORx < 5 or x > 10
notLogical NOTnot(x > 5 and x < 10)

Assignment Operators

  • 할당연산
OperatorExampleSame as
=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

Identity Operators

  • 항등연산
  • object 비교.
  • 같은 object가 아닌
    같은 memory에 있는
    object인지 비교.
OperatorExampleResult
isx is yReturns True
if the same object
is notx is not yReturns True
if not the same object

Membership Operators

  • 멤버쉡 연산
  • object에 시퀀스가 존재하는지 테스트.
  • OperatorExampleName
    inx in yReturns True
    if y have x
    not inx not in yReturns True
    if y have not x
  • 아무튼 좀 애매한부분을 더 봤는데

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    l2 = [1, 2, 3]
    l3 = [1, 2]
    item = 3
      
    print("l1 in l2 ?", (l1 in l2))
      
    print("item in l1 ?", (item in l1))
      
    print("l3 in l1 ?", (l3 in l1))
    
    1
    2
    3
    
    l1 in l2 ? False
    item in l1 ? True
    l3 in l1 ? False
    

    해서, 집합기호로 따지면 관계.
    부분집합이 아닌 원소.

Bitwise Operators

  • 비트연산
OperatorNameExample
&ANDx & y
|ORx | y
^XORx ^ y
~NOT~x
«Zero fill left shiftx « y
»Signed right shiftx » y

Operator Precedence

  • 앞서 썼던 연산자 포함 연산자 우선순위.
    나머지는 앞으로 오며가며 볼꺼니까.
  • 위쪽으로 우선순위가 높고
    같은 상자안에서는 순서상관없이 같음.
  • 구문이 명시적이지 않으면
    연산자는 binary
OperatorDescription
(expressions…),[expressions…], 
{key: value…}, {expressions…}
Binding, parenthesized expression, list display,
dictionary display, set display
x[index], x[index:index],
 x(arguments…), x.attribute
Subscription, slicing,
call, attribute reference
await xAwait expression
**Exponentiation
+x, -x, ~xPositive, negative, bitwise NOT
*, @, /, //, %Multiplication, matrix multiplication,
division, floor division, remainder
+, -Addition and subtraction
«, »Shifts
&Bitwise AND
^Bitwise XOR
|Bitwise OR
in, not in, is, is not, 
<, <=, >, >=, !=, ==
Comparisons, membership , identity
not xBoolean NOT
andBoolean AND
orBoolean OR
if – elseConditional expression
lambdaLambda expression
:=Assignment expression
This post is licensed under CC BY 4.0 by the author.

Python 4 - Variable & Data Type 2

Type Inheritance

Comments powered by Disqus.