Environment Setting
- Python 3.9.5
- IDE Pycharm, Jupyter Notebook, Github Codespace
π Introduction to Variable
λ³μλ?
κ°μ μ μ₯νλ κ³΅κ° = μ°μ°μλ‘ λμ
μ°μ°
νμ΄μ¬ λ³μμ νΉμ§
λͺ¨λ λ³μλ Memory Addressλ₯Ό κ°λ¦¬ν¨λ€. μ¦, λͺ¨λ κ²μ ν¬μΈν°λ€!
λ³μλͺ
μΌμ’
μ μ΄λ¦ν, μ μΈν λ³μμ νΉμ 곡κ°μ΄ μκΈ°λ κ°λ
μ΄ μλλ€!
νμν λλ§λ€ 곡κ°μ λ§λ€κ³ λ³μλͺ
μ λΆμ!
a = 15
b = 4
a+b
#19
a = 15
b = a
b
#15
π How to name Variables
- μνλ²³, μ«μ, μΈλμ€μ½μ΄(_)λ‘ μ μΈ
- μ«μλ‘ λ³μλͺ μ μμν μ μμ.
- data = 0, _al=2, _gg='adfaf'
- λ³μλͺ
μ κ·Έ λ³μμ νΉμ§μ΄ μ μ΄μμκ² μλͺ
νμ.
- κ°λ μ±μ΄ μ μΌ μ€μνλ€.
- name = 'Hojun Cho
- λ³μλͺ
μ λμλ¬Έμλ₯Ό ꡬλΆνκ³ μμ½μ΄λ₯Ό μ¬μ©ν μ μλ€.
- abcμ ABCλ λ€λ₯΄λ€.
- for, if, else, True λ±μ μμ½μ΄λ λ³μλͺ μΌλ‘ μ¬μ©ν μ μλ€.
π Assinging Variables
- CμΈμ΄μ λ¬λ¦¬ λμ μ°μ°μ΄ λ±ν λ°ν κ°μ κ°μ§λ κ² μλλ€.
- μ°μν΄μ λμ μ΄ κ°λ₯νλ€.
- := μ°μ°μΌλ‘ λμ κ³Ό λμμ λ°νμ΄ κ°λ₯νλ€.
# Cμ λ¬λ¦¬ λμ
μ°μ°μ΄ λ±ν λ°ν κ°μ κ°μ§λ κ²μ΄ μλλ€.
(a=2)==2 #Syntax Error
# Input In [3]
# (a=2)==2
# ^
# SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
# μ°μν΄μ λμ
μ΄ κ°λ₯νλ€. (Cμ κ°μ΄ λ€μμλΆν° λμ
)
A = B = 2
print(A)
print(B)
# 2
# 2
# := μ°μ°μΌλ‘ λμ
κ³Ό λμμ λ°νμ΄ κ°λ₯νλ€. (Python 3.9 μ΄μ)
(c := 2) == 2
# True
π Primitive Data Types
- Primitive Data Type(μμ μλ£ν) : κ°μ₯ κΈ°λ³Έμ΄ λλ μλ£ν
- int(μ μν)
- overflowκ° λ°μνμ§ μμ
- λͺ μμ μΈ short typeμ΄ μμ
- float(μ€μν)
- λΆλμμμ , Double precision
- complex(볡μμ)
- νμλΆμ νμλΆλ₯Ό λλ μ νν
- νμλΆλ jλλ Jλ‘ νν
- string(λ¬Έμμ΄)
- λ°μ΄νλ‘ νν
- Unicode
- ν° λ°μ΄ν, μμ λ°μ΄ν μ°¨μ΄ μμ
- bool(λ
Όλ¦¬)
- True / False κ° λ°μ μμ
- None(None)
- None νμ
- μΌμ’ μ null
- None κ° λ°μ μμ
a = 15 # μ μ print(a) b = 1.5 # μ€μ print(b) c = 1+5j # 볡μμ print(c) d = "text" # λ¬Έμμ΄ print(d) e = True # Boolean, λμλ¬Έμμ μ£Όμ print(e) f = None # None, λμλ¬Έμμ μ£Όμ print(f) # 15 # 1.5 # (1+5j) # text # True # None
- int(μ μν)
π Feature of Arithmetic & Bit Operator
- In-Place μ°μ°
- a += 1μ In-Place μ°μ°μ΄λ€.
- κΈ°μ‘΄ κ°μ²΄λ₯Ό μμ μλνκ³ , λΆκ°λ₯ν μ μλ‘μ΄ κ°μ²΄λ₯Ό μμ±νλ€.
- Out-Place μ°μ°
- a = a+1μ Out-Place μ°μ°μ΄λ€.
- λͺ μμ μΌλ‘ μλ‘μ΄ κ°μ²΄λ₯Ό μμ±νλ€.
- a++ μ°μ°μ νμ΄μ¬μ μ‘΄μ¬νμ§ μλλ€.
# Arithmetic Operator
print(50+12) # Addition
print(30-46) # Substract
print(2*6) # Multifly
print(2**3) # Exponentiation
print(3/2) # Division
print(13//5) # Quotient
print(13%5) # Remainder
# 62
# -16
# 12
# 8
# 1.5
# 2
# 3
# In-Place μ°μ°
a = 5
a = a+1
a
# 6
a+=1
a
#7
# Out-Place μ°μ°
a = 7
a = a^4
a
# 3
a^=4
a
# 7
π Condition Operatiors
- X is Y : Xμ Yμ μ£Όμκ° κ°λ€.
- x is not Y : xμ yμ μ£Όμκ° λ€λ₯΄λ€.
a = 3
print(a<4)
print(a>2)
print(2<a and a<4)
print(2<a<4)
print(not a > 3)
# True
# True
# True
# True
# True
π Features of Primitive Data Types
- μμμλ£νλ€μ λΆλ³νμ (Immutable Type)μ΄λ€.
- μμμλ£νκ³Ό Tupleμ μ μΈν λ€λ₯Έ λͺ¨λ νμ΄μ¬ κ°μ²΄λ κ°λ³νμ (Mutable Type)μ΄λ€.
# νμ΄μ¬μμ λμ
μ μμΉμ μΌλ‘ λ©λͺ¨λ¦¬ μ£Όμ λ³΅μ¬ (μ¦, κ°μ 볡μ¬νμ§ μκ³ κ°μ μ£Όμλ₯Ό 곡μ )
# λΆλ³νμ κ²½μ° μμ μ΄ νμν κ²½μ°μ μλ‘μ΄ κ°μ²΄λ₯Ό μμ±
a = 10
b = a
a += 1
a, b, a is b
# (11, 10, False)
a = [1,2,3]
b = a
a += [4]
a, b, a is b
# ([1, 2, 3, 4], [1, 2, 3, 4], True)
a = a+[5]
a, b, a is b
# ([1, 2, 3, 4, 5], [1, 2, 3, 4], False)
π Dynamic Typing
- μ½λ μ€νμ§μ μμ λ°μ΄ν°μ νμ μ κ²°μ νλ€.
π Type Conversion
- TypeμΌλ‘ λͺ μμ ν λ³νμ΄ κ°λ₯νλ€.
- type([variable])μΌλ‘ ν΄λΉ λ³μμ νμ νμΈμ΄ κ°λ₯νλ€.
- isinstance([variable], [type])μΌλ‘ λ³μκ° μ§μ νμ μΈμ§ νμΈ κ°λ₯νλ€.
# Type Checking
a = 123
b = 12.3
c = '12.3'
isA = isinstance(a, float)
isB = isinstance(b, float)
isC = isinstance(c, str)
print(type(a))
print(type(b))
print(type(c))
print('-'*15)
print(isA)
print(isB)
print(isC)
# <class 'int'>
# <class 'float'>
# <class 'str'>
# ---------------
# False
# True
# True
'κ°λ° > Python' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
Advanced Python #5 [Notion : 1/4] (0) | 2023.04.30 |
---|---|
Advanced Python #4 [Notion : 1/3] (0) | 2023.04.30 |
Advanced Python #3 [Notion : 1/3] (0) | 2023.04.30 |
Advanced Python #2 [Notion : 1/3] (0) | 2023.04.30 |