Python (5/14 20~57/57)

๊น€์†Œ์€ยท2025๋…„ 5์›” 15์ผ

Python

๋ชฉ๋ก ๋ณด๊ธฐ
1/1

https://wikidocs.net/78952

๐Ÿ“š ๋ฌธ์ž์—ด ํฌ๋งทํŒ…

name = "So-Eun"
num= 2
print("%s has %d cats." % (name, num))

Python 3 ~ : .format method from String Class

name = "So-Eun"
num = 2
print("{} has {} cats.".format(name, num))

Python 3.6 ~ : f-string

name = "So-Eun"
num = 2
print(f"{name} has {num} cats.")
a = "hello"
b = ["hello"]

์ฝ”๋“œ๋ฅผ ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•œ ๊ฒฝ์šฐ,
"hello"๋ผ๋Š” ๋ฌธ์ž์—ด ๊ฐ์ฒด๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ์— ํ• ๋‹น๋จ.
์ด ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด ๊ฐ์ฒด๋ฅผ a, b[0] ๋‘ ๋ณ€์ˆ˜๊ฐ€ ๋ฐ”์ธ๋”ฉํ•˜๊ฒŒ ๋จ.
๋”ฐ๋ผ์„œ id(a)์™€ id(b[0])๋Š” ๊ฐ’์ด ๊ฐ™์Œ.

a = 888
b = 888

a == b	# True, comparing the value.
a is b	# False, comparing the memory address.

c = 256
d = 256

c is d	# True because integers less than 257 use only one memory address to not waist the memory.

๐Ÿ“š ์ „ํ†ต์ ์ธ for๋ฌธ์„ list comprehension์œผ๋กœ ๊ฐ„์†Œํ™”ํ•˜๋Š” ๋ฒ•(1์ฐจ์› ์ดํ•˜๋กœ๋งŒ ๊ถŒ์žฅ)

์ „ํ†ต์  for๋ฌธ

odd_num = []

for i in range(10):
	if i % 2 == 0:
    	odd_num.append(i)

list comprehension

odd_num = [i for i in range(10) if i % 2 == 0]	# [0, 2, 4, 6, 8]

๐Ÿ“š Tuple Packing & Unpacking

Packing

a = 1, 2

a == (1, 2)

Unpacking 1๏ธโƒฃ

data = (1, 3, 5)
a1, a2, a3 = data

a1 == 1
a2 == 3
a3 == 5

Unpacking 2๏ธโƒฃ

data = (3, 1, 4, 1, 5, 9, 2)
first, *middle, last = data

first == 3
middle == [1, 4, 1, 5, 9] # list
last == 2

Unpacking 3๏ธโƒฃ

def sum(a, b, c, d)
	return a+b+c+d
    
data = (102, 30, 0, 77)
result = sum(*data)	# ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ data ํŠœํ”Œ ์–ธํŒจํ‚น.

๐Ÿ“š Zip & Dictionary

zip

zip(key_list , value_list) == [(key1, value1), ...]

for k, v in zip(key_list, value_list)
	print(k, v)

dictionary

dict(zip(ket_list, value_list))
== dict( [(key1, value1), ...] )
== {key1 : value1, ...}

dict_.setdefault(_key, value_) returns value

dict1 = {}

res = dict1.setdefault('key1', 0)
print(res)
print(dict1)
0
{ 'key1' : 0 }

res = dict1.setdefault('key1', 77) # key1์ด ๊ธฐ์กดํ•˜๋ฏ€๋กœ ๊ฐ’ ๋ณ€๊ฒฝ ์—†์ด ๊ธฐ์กด ๊ฐ’์„ ๋ฆฌํ„ดํ•จ.
print(res)
print(dict1)
0
{ 'key1' : 0}

๐Ÿ“š ์ค‘๋ณต๋œ ๊ฐ’์„ ํฌํ•จํ•˜๋Š” List

list1 = ['aa', 'bb', 'bb', 'cc', 'cc', 'cc']

for key in set(list1):	# list1์— ์ค‘๋ณต๋œ ๊ฐ’ ์ œ๊ฑฐ ํ›„ ์ง‘ํ•ฉ ํ˜•ํƒœ๋กœ ๋ฆฌํ„ด, {'aa', 'bb', 'cc'}
	cnt = list1.count(key)
    print(k, cnt)
{ 'aa' : 1, 'bb' : 2, 'cc' : 3}

๐Ÿ“š Dictionary Comprehension:

keys = ['a', 'b', 'c']
values = [1, 2, 3]

dict1 = { k:v for k, v in zip(keys, values) }	# dict(zip(keys, values))
print(dict1)

{ 'a' : 1, 'b' : 2, 'c' : 3 }

dict2 = { k:v*v for k, v in zip(keys, values) if v > 1 }
print(dict2)

{ 'b' : 4, 'c' : 9 }

๐Ÿ“š namedtuple : ํด๋ž˜์Šค์™€ ๋”•์…”๋„ˆ๋ฆฌ์˜ ํŠน์„ฑ์„ ๋ชจ๋‘ ํฌํ•จํ•œ๋‹ค.

from collecrtions import namedtuple

Book = namedtuple('Book', ['title', 'author'])	# Book: ํด๋ž˜์Šค ๊ฐ์ฒด์™€ ๋Œ€์‘, ['title', 'author']: ํŠœํ”Œ๊ณผ ๋Œ€์‘, 'title'/'author': ๊ฐ ๋”•์…”๋„ˆ๋ฆฌ์˜ key/value์™€ ๋Œ€์‘
myBook1 = Book('์›์น™', '๋ ˆ์ด ๋‹ฌ๋ฆฌ์˜ค')

myBook1 == Book( title = '์›์น™', author = '๋ ˆ์ด ๋‹ฌ๋ฆฌ์˜ค' )

print(myBook1.title, myBook2.author)

( '์›์น™', '๋ ˆ์ด ๋‹ฌ๋ฆฌ์˜ค' )

def print_book(title, author)
	print(title, price)

print_book(*myBook1)	# unpacking tuple to list

['์›์น™', '๋ ˆ์ด ๋‹ฌ๋ฆฌ์˜ค']

๐Ÿ“š ASCII Code

48 : 0
49 : 1
...
57 : 9
...
65 : A
66 : B
...
90 : Z
...
97 : a
98 : b
...
122 : z

profile
๊ฐœ๋ฐœ์ž

0๊ฐœ์˜ ๋Œ“๊ธ€