# [Python] TIL 02

jiyoungΒ·2021λ 11μ 27μΌ
0

πΎμ¬λΌμ΄μ±
-μ²μλΆν°λ λκΉμ§ νλ κ²½μ° κ³΅λ°±μΌλ‘ λΉμλ μκ΄ μλ€.
-μ²μλΆν° μλλ μΈλ±μ€κ° 0λΆν°μ΄μ§λ§, λμμλΆν° μλλ μΈλ±μ€κ° -1λΆν°μ΄λ€.

jumin = "990120-1234567"

print("μ±λ³: " + jumin[7])
print("μ°: " + jumin[0:2]) # 0λΆν° 2 μ§μ κΉμ§ (0,1)
print("μ: " + jumin[2:4])
print("μΌ: " + jumin[4:6])
print("μλμμΌ: " + jumin[:6]) # μ²μλΆν° 6 μ§μ κΉμ§
print("λ€ 7μλ¦¬: " + jumin[7:]) # 7λΆν° λκΉμ§
print("λ€ 7μλ¦¬ (λ€μμλΆν°): " + jumin[-7:])
#λ§¨ λ€μμ 7λ²μ§ΈλΆν° λκΉμ§

πΎλ¬Έμμ΄ μ²λ¦¬ ν¨μ
-findμμλ μνλ κ°μ΄ λ³μμ μμ λ -1μ΄ μΆλ ₯λλ€. λ°λ©΄, indexμμλ μνλ κ°μ΄ μμ λ μ€λ₯λ₯Ό μΆλ ₯νλ©΄μ νλ‘κ·Έλ¨μ μ’λ£νλ€.

python = "Python is Amazing"
print(python.lower()) # λ¬Έμ₯μ μλ¬Έμλ‘ λ°κΏμ€
print(python.upper()) # λ¬Έμ₯μ λλ¬Έμλ‘ λ°κΏμ€
print(python[0].isupper()) # ν΄λΉ λ¬Έμμ΄μ΄ λλ¬ΈμμΈμ§ νλ³ν΄μ€ (boolνμ)
print(len(python)) # λ¬Έμμ΄μ κΈΈμ΄λ₯Ό μλ €μ€
print(python.replace("Python", "Java")) # λ¬Έμμ΄ μμ μλ κ°λ€μ λ°κΏ μ μμ

index = python.index("n") # μ΄λ€ λ¬Έμκ° μ΄λ μμΉμ μλμ§ μλ €μ€
print(index)
index = python.index("n", index + 1) # ν΄λΉ μΈλ±μ€λΆν° μμΉ νμμ μμν¨
print(index)

print(python.find("n")) # ν΄λΉ λ¬Έμκ° ν¬ν¨λ μμΉλ₯Ό μλ €μ€
print(python.find("Java")) # μνλ κ°μ΄ λ³μμ ν¬ν¨λμ§ μμ κ²½μ° -1μ΄ μΆλ ₯λ¨

print(python.count("n")) # nμ΄ λ³μμμ μ΄ λͺλ² λ±μ₯νλμ§ κ°―μλ₯Ό μΈμ΄μ€

πΎλ¬Έμμ΄ ν¬λ§·
-%sλ‘λ§ μ°λ©΄ μ μνμ΄λ  λ¬Έμνμ΄λ  μκ΄μμ΄ κ°μΆλ ₯μ΄ κ°λ₯νλ€.

# λ°©λ²1
print("λλ %dμ΄μλλ€." % 20)
print("λλ %sμ μ’μν΄μ" % "νμ΄μ¬")
print("Appleμ %cλ‘ μμν΄μ." % "A") # νκΈμλ§ λ°κ² λ€!

print("λλ %sμ΄μλλ€." % 20)
print("λλ %sμκ³Ό %sμμ μ’μν΄μ" % ("νλ", "λΉ¨κ°"))

print("λλ {}μ΄μλλ€.".format(20))
print("λλ {}μκ³Ό {}μμ μ’μν΄μ".format("νλ", "λΉ¨κ°"))
print("λλ {0}μκ³Ό {1}μμ μ’μν΄μ".format("νλ", "λΉ¨κ°"))
# format λ€μ μλ κ°λ€μ 0λ²μ§Έ 1λ²μ§Έλ‘ μκ°ν΄μ κ°κ° μλ²μ λ§κ² μ€νμ΄ λ¨
print("λλ {1}μκ³Ό {0}μμ μ’μν΄μ".format("νλ", "λΉ¨κ°"))

age = 22
color = "κ²μ"
print(f"λλ {age}μ΄μ΄λ©°, {color}μμ μ’μν΄μ")

πΎλ¦¬μ€νΈ

subway = ["μ μ¬μ", "μ‘°μΈνΈ","λ°λͺμ"]
print(subway)

print(subway.index("μ‘°μΈνΈ"))
#μ‘°μΈνΈκ° λͺλ²μ§Έ μΉΈμ νλκ°?

subway.append("νν") #ννκ° λ€μ μΉΈμ ν
print(subway)

subway.insert(1,"μ νλ") # μ μ¬μ μ‘°μΈνΈ μ¬μ΄μ νμλ΄
print(subway)

print(subway.pop()) # λ§¨ λ€μμλΆν° νλͺμ© κΊΌλ
print(subway)

print(subway.count("μ μ¬μ")) # κ°μ μ΄λ¦μ μ¬λμ΄ λͺλͺ μλμ§ νμΈ

-λ¦¬μ€νΈλ μ λ ¬λ κ°λ₯νλ€.

num_list = [5,2,4,3,1]
num_list.sort() # μμλλ‘ μ λ ¬ν΄μ€
print(num_list)

num_list.reverse() # μμ λ€μ§κΈ° κ°λ₯
print(num_list)

num_list.clear() # λ¦¬μ€νΈ λͺ¨λ μ§μ°κΈ°
print(num_list)

ex) mix_list = ["μ‘°μΈνΈ", 20, True]
-λ¦¬μ€νΈ νμ₯μ΄ κ°λ₯νλ€.
ex) num_list.extend(mix_list) -> [5,2,4,3,1,'μ‘°μΈνΈ', 20, True]

πΎμ¬μ  μλ£ν

fruit = {3:"μλ°", 100:"λΈκΈ°"} # μ μνμ ν΅ν΄ ν€ μ μΈ
# μ¬μ  μλ£ν κ°μ Έμ€λ λ²1
print(fruit[3])
print(fruit[100])

print(fruit.get(3))

fruit[5]μ²λΌ 5λΌλ ν€κ° μλ€λ©΄ Traceback μλ¬κ° λ¨λ©΄μ μ’λ£λλ€.
νμ§λ§ fruit.get(5)λ‘ μμ±νλ©΄ Noneμ΄λΌκ³  λ¨κ³  λ€μ λ¬Έμ₯μ μ€ννλ€.
fruit.get(5,"μ¬μ© κ°λ₯")μ΄λΌκ³  μμ±νλ©΄ 5λΌλ κ°μ κ°μ Έμ€λ €κ³  μλνκ³ , μμΌλ©΄ 5λ₯Ό κ°μ Έμ€κ³  μμΌλ©΄ 'μ¬μ© κ°λ₯'μ΄λΌκ³  μΆλ ₯λλ€.

μ¬μ  μλ£νμ κ°μ΄ μλμ§ μλμ§ νμΈν  μ μλ€.

print(3 in fruit) # 3μ΄λΌλ ν€κ° fruit μμ μλκ°? # True
print(5 in fruit) # False

μ μ μλ£ν λΏλ§ μλλΌ string μλ£νμΌλ‘λ ν  μ μλ€.

fruit = {"A-3":"μλ°", "B-100":"λΈκΈ°"}

print(fruit["A-3"])
print(fruit["B-100"])

fruit["C-50"] = "λ°€" # μλ‘μ΄ ν€ μΆκ°
fruit["A-3"] = "κ³ κ΅¬λ§" # κΈ°μ‘΄ ν€κ° update
print(fruit)

del fruit["A-3"] # ν€ μ­μ 
print(fruit)

print(fruit.keys()) # keyλ€λ§ μΆλ ₯
print(fruit.values()) # valueλ€λ§ μΆλ ₯
print(fruit.items()) # key, value μμΌλ‘ μΆλ ₯

fruit.clear() # λͺ¨λ  ν€ μ­μ 
print(fruit)

πΎνν
-ννμ add κΈ°λ₯ μ κ³΅νμ§ X
-λ°λΌμ κ°μ μΆκ°νκ±°λ λ³κ²½μ λΆκ°νλ€.
-κ³ μ λ κ°μ λν΄μλ§ μΈ μ μκ³ , νλ²μ κ° μ μΈμ΄ κ°λ₯νλ€.
(name, age, hobby) = ("μ΄μ§μ",20,"μ½λ©")