[Python] 02. VARIABLE

Min's Study NoteΒ·2023λ…„ 11μ›” 3일

Python

λͺ©λ‘ 보기
2/21

02. VARIABLE

πŸ’‘λ³€μˆ˜μ˜ λͺ…λͺ…κ·œμΉ™

  • λ³€μˆ˜ μ΄λ¦„μ—λŠ” λ‹€μŒ 문자만 μ‚¬μš© κ°€λŠ₯ν•˜λ‹€: μ†Œλ¬Έμž(a - z), λŒ€λ¬Έμž(A - Z), 숫자(0 - 9), 언더바(_)
  • λ³€μˆ˜λͺ…은 λŒ€μ†Œλ¬Έμžλ₯Ό κ΅¬λΆ„ν•œλ‹€: apple, Apple, aPPleλŠ” λͺ¨λ‘ λ‹€λ₯Έ λ³€μˆ˜
  • λ³€μˆ˜λͺ…은 숫자둜 μ‹œμž‘ν•  수 μ—†λ‹€. μ†Œλ¬Έμž, λŒ€λ¬Έμž, μ–Έλ”λ°”λ‘œλ§Œ μ‹œμž‘ν•  수 μžˆλ‹€.
  • μ˜ˆμ•½μ–΄λŠ” λ³€μˆ˜λͺ…μœΌλ‘œ μ‚¬μš©ν•  수 μ—†λ‹€.

πŸ”³ λ³€μˆ˜μ˜ μ„ μ–Έκ³Ό μ΄ˆκΈ°ν™”(Assignment) => 동적 ν• λ‹Ή

a = 10
b = True
c = 3.14
d = "Hello"
e = 'c'
f = {"name", "age", "email"}
print(type(a), type(b), type(c), type(d), type(e), type(f), sep='\n')
# <class 'int'>
# <class 'bool'>
# <class 'float'>
# <class 'str'>
# <class 'str'>
# <class 'set'>

del f   #λ³€μˆ˜ μ‚­μ œ

a = 1; b = 2
a,b = b,a   #λ³€μˆ˜ κ΅ν™˜
print("λ³€μˆ˜ κ΅ν™˜ : a = %d, b = %d" % (a,b))
#λ³€μˆ˜ κ΅ν™˜ : a = 2, b = 1

a = (a = b + c)   #error -> 할당문이 할당문을 포함할 수 μ—†λ‹€.

πŸ”³ 1. type Number(int, float)

a = 10
b = 12.345
c = 0b0101   #2μ§„μˆ˜
d = 0o12   #8μ§„μˆ˜
e = 0x2d   #16μ§„μˆ˜
f = 123e2   #μ§€μˆ˜ ν‘œκΈ°λ²•
g = 123E-2   #μ§€μˆ˜ ν‘œκΈ°λ²•
print(a,b,c,d,e,f,g)
#10 12.345 5 10 45 12300.0 1.23

πŸ”³ λ‚œμˆ˜ 생성

h = random.random()*100   #0.0<=x<1.0
i = random.uniform(1, 100)
rand = int(random.random()*100)+1	#1~100 μž„μ˜μ˜ 숫자
print(h)
print(int(i))   #λ‚œμˆ˜λ₯Ό μ •μˆ˜λ‘œ λ³€ν™˜

πŸ”³ μ˜ˆμ™Έ 처리

try:
  x = float("a0.12")
except:
  print("μˆ«μžκ°€ μ•„λ‹Œ λ¬Έμžμ—΄μ΄ ν¬ν•¨λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€.")
print(x)

πŸ”³ λ³΅μ†Œμˆ˜ μžλ£Œν˜•(complex)

y = 10 + complex(2)
print(y)
print(type(y))		#<class 'complex'>

πŸ”³ 2. type boolean :: bool

power = False   #True, False
power = bool("")    #False
power = bool(None)    #False
power = bool(0)    #False
power = bool(-10)    #True
power = bool("LMH")    #True
print(power)
print(type(power))    #<class 'bool'>

πŸ”³ 3. type string :: str

s = "Hello Python"
print(type(s))    #<class 'str'>

s1 = str(12345)
print(type(s1))    #<class 'str'>
print(s1+1)   #error

πŸ”³ 4. type character :: chr

c = chr(48)
c = 'A'
c1 = 'hello' + 'world'
print(c)   #A
print(type(c))    #<class 'str'>
print(c1)   #helloworld
print(type(c1))    #<class 'str'>

(μ°Έκ³ ) λ³€μˆ˜

0개의 λŒ“κΈ€