Python 곡λΆ!
π 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
- https://swexpertacademy.com/
- https://nomadcoders.co/
- http://pythonstudy.xyz/python/article/511-νμ΄μ¬-μ½λ©-μ€νμΌ
π garbage collection