파이썬의 철학

  • 암시하는 것보다는 명시하는 것이 낫다. (Explicit is better than implicit.)

  • 복잡한 것보다는 단순한 것이 낫다. (Simple is better than complex.)

  • 난해한 것보다는 복잡한 것이 더 낫다. (Complex is better than complicated)

Python3 주요 변경 사항

  • 모든 변수는 객체(object)로 처리됨

  • 연산식에서 자료형이 float 아니어도 결과가 float이면 float 자료형으로 리턴

    >>> 3/2
    1.5
  • print 문의 Fuction 형태로 바뀜

    >>> print("Hi", "Python")
    Hi Python
    
    >>> a = 1234
    >>> b = 5678
    >>> print ("A:", a, "B:", b)
    A: 1234 B: 5678
    
    >>> print("A: %d B: %d" % (a,b))
    A: 1234 B: 5678
    >>> print("A: %5d B: %5d" % (a,b))
    A:  1234 B:  5678
    >>> print("A: %05d B: %05d" % (a,b))
    A: 01234 B: 05678
  • dict (사전)

    >>> d = {'a':1, 'b':2, 'c':3, 'd':4}
    
    >>> print(d)
    {'a': 1, 'b': 2, 'c': 3, 'd': 4}
    
    >>> d.keys()
    dict_keys(['a', 'b', 'c', 'd'])
    
    >>> d.values()
    dict_values([1, 2, 3, 4])
    
    >>> d.items()
    dict_items([('a', 1), ('b', 2), ('c', 3), ('d', 4)])
    
    >>> iter(d.items())
    <dict_itemiterator object at 0x7f5e45f3edb8>
  • 모든 문자열을 unicode인 str로 통일

    >>> print(type('hello'))
    <class 'str'>
    >>> print(type(u'hello'))
    <class 'str'>
  • long을 int로 통일

    >>> a = 2**100
    >>> print(a)
    1267650600228229401496703205376
    >>> type(a)
    <class 'int'>
  • .pyc 파일 생성

    #  __pycache__ 디렉토리에  magic tag 를 넣은 .pyc 파일 생성
    
    # ls
    module_test.py
    
    # ls __pycache__/
    module_test.cpython-36.pyc
    Note
    python3 부터는 .py 없는 .pyc 는 사용할 수 없음

'파이썬 (Python)' 카테고리의 다른 글

Telegram API Python에서 활용  (0) 2021.03.14