연산자 | 우선순위 | 설명 | 결합순서 |
---|---|---|---|
+, - | 1 | 단항 연산자 | - |
** | 2 | 지수 연산자 | 왼쪽 <- 오른쪽 |
*,/,%,// | 3 | 곱하기,나누기,나머지,몫 | 왼쪽 -> 오른쪽 |
+, - | 4 | 더하기,빼기 | 왼쪽 -> 오른쪽 |
print(2 ** 3)
print(5 % 2)
print(-5 % 2)
8 1 1
print(3 + 5)
print(3 + 5.0) # 정수 + 실수의 결과는 실수
8 8.0
a = 5 / 2.0
b = 5 / 2
print(a) # 정수 / 실수의 결과
print(b) # 정수 / 정수의 결과 <-- python 3 부터는 /의 결과는 언제나 실수
print(type(a), type(b))
2.5 2.5 <class 'float'> <class 'float'>
a = 5 / 3
b = 5 % 3
print(a, b)
print(divmod(5,3))
# 1 2
# (1, 2)
1.6666666666666667 2 (1, 2)
print(5 // 3) # Floor Division
print(5 // 3.0) # Floor Division - Floor Division은 '정수 // 정수'와 '정수 // 실수'의 결과 다름
1 1.0
print(-7/4) # -7을 4로 나눈다
print(-(7/4))
-1.75 -1.75
print(2 + 3 * 4)
print((2 + 3) * 4)
14 20
print(4 / 2 * 2)
4.0
print(2 ** 3 ** 2)
print((2 ** 3) ** 2)
512 64
print(6 == 9)
print(6 != 9)
print(1 > 3)
print(4 <= 5)
a = 5
b = 10
print(a < b)
False True False True True
a = 5
b = 10
print(0 < a < b)
True
a = 5
b = 10
print(0 < a and a < b)
True
print('abcd' > 'abd')
print((1, 2, 4) < (2, 1, 0))
print([1, 3, 2] < [1, 2, 3])
False True False
print({0:0, 1:1} > {2:2, 3:3})
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-21-6c72580154ad> in <module>() ----> 1 print({0:0, 1:1} > {2:2, 3:3}) TypeError: '>' not supported between instances of 'dict' and 'dict'
print({0:0, 1:1} == {2:2, 3:3})
print({0:0, 1:1} == {0:0, 1:1})
False True
print(9999999999999999999999 < 'abc')
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-24-1ad077f7dbdc> in <module>() ----> 1 print(9999999999999999999999 < 'abc') TypeError: '<' not supported between instances of 'int' and 'str'
print({3:2} < [1,2,3] < (1,2,3))
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-25-b7a2598f4f7e> in <module>() ----> 1 print({3:2} < [1,2,3] < (1,2,3)) TypeError: '<' not supported between instances of 'dict' and 'list'
L = [1,2,3, 'abc', 'a', 'z', (1,2,3), [1,2,3], {1:2}, ['abc']]
L.sort()
print(L)
# [1, 2, 3, {1: 2}, [1, 2, 3], ['abc'], 'a', 'abc', 'z', (1, 2, 3)]
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-26-d35459624c4c> in <module>() 1 L = [1,2,3, 'abc', 'a', 'z', (1,2,3), [1,2,3], {1:2}, ['abc']] ----> 2 L.sort() 3 print(L) 4 # [1, 2, 3, {1: 2}, [1, 2, 3], ['abc'], 'a', 'abc', 'z', (1, 2, 3)] TypeError: '<' not supported between instances of 'str' and 'int'
x = [1,2,3]
y = [1,2,3]
z = y
print(x == y)
print(x == z)
print(x is y)
print(x is z)
print(y is z)
True True False False True
print(x[0] == y[0])
print(x[0] is y[0])
True True
a = 20
b = 30
print(a > 10 and b < 50)
print(a > 10 or b < 50)
True True
print(True + 1)
print(False + 1)
print(False * 75)
print(True * 75)
2 1 0 75
print(bool(0)) # 정수 0은 거짓
print(bool(1))
print(bool(100))
print(bool(-100))
print()
print(bool(0.0)) # 실수 0.0은 거짓
print(bool(0.1))
print(bool(0.00000000000000000000000000000000000000000001))
False True True True False True True
print(bool('abc'))
print(bool('')) # 빈 문자열은 거짓
print()
print(bool([])) # 공 리스트는 거짓
print(bool([1,2,3]))
print()
print(bool(())) # 공 튜플은 거짓
print(bool((1,2,3)))
print()
print(bool({})) # 공 사전은 거짓
print(bool({1:2}))
print()
print(bool(None)) # None 객체는 거짓
True False False True False True False True False
print(1 and 2)
print(1 and 0)
print()
print(0 or 2)
print(1 or 0)
print()
print([] or 1) # [] 거짓
print([] or ()) # [], () 거짓
print([] and 1) # [] 거짓이므로 1은 참조할 필요 없음
2 0 2 1 1 () []
print([[]] or 1) # [[]] 참으로 간주
print([{}] or 1) # [{}] 참으로 간주
print('' or 1) # 빈 문자열('')은 거짓
[[]] [{}] 1
print(not(True))
print(not(1 and 2))
print(not('' or 1))
False False False
참고 문헌: 파이썬(열혈강의)(개정판 VER.2), 이강성, FreeLec, 2005년 8월 29일