Work/Python

Python 곡뢀!

lagu 2021. 2. 24. 10:30
728x90
λ°˜μ‘ν˜•

πŸ˜ƒ Web IDE : https://repl.it/

πŸ˜ƒ Web Documentation : https://docs.python.org/3.5/library/index.html

Naming Rules

Data Types

  • πŸ‘‰ string a_string = "Like this"
    • "이름 %s" % "내이름" , "λ‚˜μ΄ %s" % "25"
    • "이름 %s, λ‚˜μ΄ %s" % ("내이름", 25)
    • "이름 : %(name)s, λ‚˜μ΄ %(age)s" % {"name":"내이름", "age":25}
    • Formating : %d → 10μ§„μˆ˜, %o → 8μ§„μˆ˜, %x → 16μ§„μˆ˜, %f → λΆ€λ™μ†Œμˆ˜μ , %d → μ •μˆ˜, %% → %둜 ν‘œμ‹œ, %5s → μš°μΈ‘μ •λ ¬ 5만큼, %-5s → μ’ŒμΈ‘μ •λ ¬ 5만큼, %0.2f → 3.14, %2.2f → μš°μΈ‘μ •λ ¬ 2만큼 & μ†Œμˆ˜ 자릿수 2만큼, $02.2f → λΉ„μ–΄μžˆλŠ” μžλ¦¬μˆ˜λŠ” 0으둜 채움
    • "이름: {}, λ‚˜μ΄{}".format("내이름",25} → 순차적으둜 적용, "이름:{0}".format("내이름")
    • "{0:f}.format()" → μ†Œμˆ˜μ  6자리, "{0:2f}.format()" → μ†Œμˆ˜μ  2자리
    • "{name}, {age}".format(name="내이름",age=20)
  • πŸ‘‰ int a_number = 3
  • πŸ‘‰ float a_float = 3.12
  • πŸ‘‰ boolean a_boolean = False
  • πŸ‘‰ null a_none = None
  • πŸ‘‰ list a_list = [1, 2, 3]
    • β­• a_list[1] = 3 → a_list[1, 3, 3] β­• a_list = ["hi", 21]
  • πŸ‘‰ tuple a_tuple = ("hi",20)
    • 🚫 a_tuple[1] = 21, β­• a_tuple = ("hi", 21)
  • πŸ‘‰ set a_set = {1, 2, 3, 3} → a_set = {1, 2, 3}
    • 🚫 a_set[0], ☝️ a_set |= {2, 4} → ν•©μ§‘ν•©μœΌλ‘œ μ„ μ–Έ κ°€λŠ₯ β­• a_set = ["hi", 21]
    • πŸš«μˆœμ„œκ°€ μ—†μ–΄μ„œ 인덱슀 μ—†μŒ, πŸš«λ°μ΄ν„° 쀑볡 ν—ˆμš©X
  • πŸ‘‰ Dictionary a_dictionary = { 1: 1, 2: 2}
    • a_dictionary[1] → 1, β­• a_dictionary[2] = 3, a_dictionary[4] = 4 → μƒˆλ‘­κ²Œ μΆ”κ°€ ☝️ a_dictionary[4]와 a_dictionary['4']λŠ” 닀름!
    • ☝️ ν‚€λ₯Ό μ΄μš©ν•΄ κ°’ 읽을 수 있음, ν•­λͺ© μΆ”κ°€μ‹œ 동일킀 | μ—†μŒ: μƒˆλ‘œμš΄ ν•­λͺ© μΆ”κ°€, 있음 : μ €μž₯된 ν•­λͺ© λ³€κ²½
  • πŸ‘‰ Del del() : μ‚¬μš©ν•˜μ§€ μ•ŠλŠ” λ³€μˆ˜ 제거
  • var1 = 10, var2 = 10, var1 is var2 → True

Queue

Function

  • ν•¨μˆ˜λ₯Ό λ§Œλ“ λ‹€!

  • λͺ¨λ“ˆλ‘œ λ˜μ–΄ μžˆλŠ” ν•¨μˆ˜λ₯Ό λΆˆλŸ¬μ˜¨λ‹€!

  • 본인이 λ§Œλ“  것도 λͺ¨λ“ˆμ²˜λŸΌ λΆˆλŸ¬μ˜¨λ‹€!

 

Class

  • 클래슀의 상속 ꡬ쑰 μ„ μ–Έ

Map, lambda

  • lambda둜 map에 λŒ€ν•΄ μ μš©ν•  수 μžˆλ‹€.

  • zip, unpack

    • zip(name), *name

Boolean Operations

  • x or y
  • x and y
  • not x

Slice (:)

  • command[0] → 0번째 index
  • command[1:2] → 1번째 ~ 2번째 index
  • command[:2] → 0 ~ 2번째 κΉŒμ§€
  • command[2:] → 2 ~ 끝 κΉŒμ§€
  • command μ΅œλŒ€κ°€ 4라면 command[0] command[1] command[2] command[3] command[-4] command[-3] command[-2] command[-1]

Bisect

  • Array bisection algorithm, Binary search!

import bisect mylist = [1, 2, 3, 7, 9, 11, 33] print(bisect.bisect(mylist, 7))

4

λ¬Έμžμ—΄ μ •λ ¬ν•˜κΈ°

s, n = input().strip().split(' ') n = int(n) print(f'{s:<{n}}') print(f'{s:^{n}}') print(f'{s:>{n}}')

s.ljust(n) # 쒌츑 μ •λ ¬ s.center(n) # κ°€μš΄λ° μ •λ ¬ s.rjust(n) # 우츑 μ •λ ¬

Comparisons

  • is : object identity

  • is not : negated object identity

     

Snake case

  • super_long_variable : python μ‚¬μš©ν• λ•Œ 약속, λͺ¨λ‘ μ†Œλ¬Έμž

Garbage Collection

  • python의 κ°€λΉ„μ§€ μˆ˜μ§‘κΈ°λŠ” 총 3μ„ΈλŒ€, 0(young)~2(old)μ„ΈλŒ€
  • κ°€λΉ„μ§€ μ»¬λ ‰μ…˜μ΄ μ„±λŠ₯에 영ν–₯을 μ£ΌλŠ” 이유 : κ°€λΉ„μ§€ μ»¬λ ‰μ…˜μ„ μˆ˜ν–‰ν•˜λ €λ©΄ ν”„λ‘œκ·Έλž¨μ΄ μ™„μ „νžˆ μ€‘μ§€ν•΄μ•Όν•˜κ³ , 객체가 λ§Žμ„ 수둝 μ‹œκ°„μ΄ μ˜€λž˜κ±Έλ¦¬λ‹ˆ λ©ˆμΆ°μžˆλŠ” μ‹œκ°„λ„ 였래걸림.
  • 레퍼런슀 μΉ΄μš΄νŒ…(reference_counting) : 주둜 μ‚¬μš©!
    • ☝️ 객체가 참쑰될 λ•Œλ§ˆλ‹€ 증가, ν•΄μ œλ  λ•Œ κ°μ†Œ, 0 이면 λ©”λͺ¨λ¦¬ ν• λ‹Ή ν•΄μ œ sys.getrefcount(a) λ₯Ό 톡해 μ°Έμ‘° 횟수 확인 κ°€λŠ₯, 단 sys ν• λ‹Ήμ‹œμ—λ„ 증가!
  • μ„ΈλŒ€λ³„ κ°€λΉ„μ§€ μ»¬λ ‰μ…˜(generational_garbage_collection) : 보쑰둜 μ‚¬μš©!
    • ☝️ 객체가 μˆœν™˜ μ°Έμ‘° ν•˜κ±°λ‚˜, μ„œλ‘œλ₯Ό μ°Έμ‘°ν•˜λŠ” κ°μ²΄μ—μ„œ ν•΄λ‹Ή 객체에 μ ‘κ·Ό ν•  수 없을 경우 레퍼런슀 μΉ΄μš΄νŒ…λ§ŒμœΌλ‘œλŠ” ν•œκ³„κ°€ μžˆλ‹€.
    • ✌️ Generation Rules : 0μ„ΈλŒ€μ—μ„œ μ‹œμž‘, 객체가 μ‚΄μ•„ λ‚¨μœΌλ©΄(ν˜„μž¬ μ„ΈλŒ€μ˜ μž„κ³„κ°’ μˆ˜μΉ˜κ°€ μ΅œλŒ€) λ‹€μŒ μ„ΈλŒ€λ‘œ 이동, 0μ„ΈλŒ€μ—μ„œ 더 자주 κ°€λΉ„μ§€ μ»¬λ ‰μ…˜ν•¨.
    • 🀟 κ°€λΉ„μ§€ μ»¬λ ‰ν„°μ˜ λ™μž‘μ„ λ³€κ²½ ν•  수 μžˆλ‹€! μž„κ³„κ°’ λ³€κ²½, μˆ˜λ™μœΌλ‘œ trigger, κ°€λΉ„μ§€ μ»¬λ ‰μ…˜ λΉ„ν™œμ„±ν™” ν•  수 μžˆλ‹€.
  • Manual
    • Time-based(μ‹œκ°„ 기반) : κ³ μ •λœ μ‹œκ°„ κ°„κ²©λ§ˆλ‹€ 호좜 Event-based(이벀트 기반) : νŠΉμ • 이벀트 λ°œμƒμ‹œ 호좜, μ‘μš© ν”„λ‘œκ·Έλž¨ μ’…λ£Œ ν˜Ήμ€ 쀑단 μƒνƒœμΌ λ•Œ

List comprehension의 if 문

  • for λ¬Έκ³Ό if문을 ν•œλ²ˆμ— - List comprehension의 if λ¬Έ

mylist = [3, 2, 6, 7] answer = [i**2 for i in mylist if i %2 == 0]

Reference!

πŸ‘‰ python

πŸ‘‰ garbage collection

728x90
λ°˜μ‘ν˜•