프리렉 - FREELEC
http://freelec.co.kr/book/catalogue_view.asp?UID=134
이강성저
t = (1, 2, 3, 4)
s = t[:2] + t[3:]
s
t [|, |, |, |]
| | | |
1, 2, 3, 4
| | |
s [|, |, |]
슬라이싱은 객체의 레퍼런스를 복사한다. 따라서 1, 2, 4 객체에 대한 레퍼런스만 복사를 한다. 따라서 세 객체의 레퍼런스를 저장할 만큼의 메모리만 증가한다.
Sun, Mon, Tue, Wed, Thu, Fri, Sat = range(7)
print(Sun, Mon)
weekday = input('요일 하나를 입력하세요:') # 파이썬3에서는 raw_input 대신 input으로
weekday # 문자열
# 방법 1
globals()[weekday] # 전역 변수에서 'Wed' 이름 찾기
# 방법 2 - 12장에 설명 나옴
import sys
mod = sys.modules[__name__] # 현재 모듈 알아내기
getattr(mod, weekday) # 모듈 내의 값 얻어내기
#1)
a, b = 1, 2
print ('a={}, b={}'.format(a, b))
#2)
a, b = ['green', 'blue']
print ('a={}, b={}'.format(a, b))
#3)
a, b = 'XY'
print ('a={}, b={}'.format(a, b))
#4)
a, b = range(1,5,2) # [1, 3]
print ('a={}, b={}'.format(a, b))
#5)
(a, b), c = "XY", "Z"
print ('a={}, b={}, c={}'.format(a, b, c))
#6)
(a, b), c = [1, 2], 'this'
print ('a={}, b={}, c={}'.format(a, b, c))
#7)
a, *b = 1,2,3,4,5
print ('a={}, b={}'.format(a, b))
#8)
*a, b = 1,2,3,4,5
print ('a={}, b={}'.format(a, b))
#9)
a, *b, c = 1,2,3,4,5
print ('a={}, b={}, c={}'.format(a, b, c))
#10)
a, *b = 'X'
print ('a={}, b={}'.format(a, b))
#11)
*a, b = 'X'
print ('a={}, b={}'.format(a, b))
#12)
a, *b, c = 'XY'
print ('a={}, b={}, c={}'.format(a, b, c))
#13)
a, *b, c = 'X...Y'
print ('a={}, b={}, c={}'.format(a, b, c))
#14)
a, b, *c = 1, 2, 3
print ('a={}, b={}, c={}'.format(a, b, c))
#15)
a, b, c, *d = 1, 2, 3
print ('a={}, b={}, c={}, d={}'.format(a, b, c, d))
#16)
*a, b = [1]
print ('a={}, b={}'.format(a, b))
#17)
*a, b = (1,)
print ('a={}, b={}'.format(a, b))
#18)
(a, b), *c = 'XY', 2, 3
print ('a={}, b={}, c={}'.format(a, b, c))
(a, b), c = 1, 2, 3 # 좌변의 값은 두 개, 우 변은 세 개, 에러 발생
(a, b), c = (1, 2), 3 # 이건 가능
*(a, b) = 1, 2 # 확장된 언패킹은 * 이 없는 변수와 함께 사용되어야 한다
*(a, b), c = 1, 2, 3
a, b, c
*(a, b), c = (1, 2), (3, 4), (5, 6)
a, b, c
*a = 1, 2 # 확장된 언패킹은 * 이 없는 변수와 함께 사용되어야 한다
def vargs(*args):
print(args)
vargs(1)
vargs(1, 3, 5, 7)
def addall(*args):
return sum(args)
addall(1)
addall(1,2,3,4)
sum(1,2,3,4) # sum 함수는 시퀀스 자료형을 받는다
sum( [1,2,3,4,] )