Post

TIL - 24/02/26

2024/02/26

  • Python - TIL
    • String
      • Raw String
        • r'' 사용하면 이스케이프 시퀀스 없이 사용 가능 근데 \ 뒤에 오는 문자가 무조건 문자열 취급 당해서 r'\'이런식으로 쓰면 마지막'이 문자 취급, 그래서 문자열이 안끝나고 SyntaxError
        • 따옴표 3개 쓰면 중간에 아무 따옴표나 쓰기 가능
      • swapcase: 대소문자 동시 변경
      • 문자열 대 소문자 비교 ==, !=로 가능
    • 논리 연산자
      • not, and, or 순으로 젹용
      • short-circuit evaluation
        • 첫 번째 값으로 결과 확실하면 뒤는 안 봄
        • 논리 연산자는 마지막으로 단락 평가한 값을 그대로 반환
        • chain 요소 평가 시 한 번만 평가
    • 객체 비교: is
      • 값 비교에 사용하는거 아님
1
2
3
4
5
6
7
8
9
10
11
12
>>> True and 'Python'
'Python'

>>> 'Python' and True
True
>>> 'Python' and False
False

>>> False and 'Python'
False
>>> 0 and 'Python'    # 0은 False이므로 and 연산자는 두 번째 값을 평가하지 않음
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
>>> 2 < "2"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'int' and 'str'
>>> 3 < 2 < "2"
False

>>> 3 < 2 < (1//0)
False

>>> def foo():
...     print("I'm foo")
...     return 1
...
>>> 0 < foo() < 2
I'm foo
True
>>> (0 < foo()) and (foo() < 2)
I'm foo
I'm foo
True
This post is licensed under CC BY 4.0 by the author.