TIL20.python

Jaeyeon·2021년 2월 19일
0
post-thumbnail

파이썬에서 가변객체와 불변객체는 무엇이며, 어떠한 자료형이 있는가?

  • 불변객체 - 객체에 할당된 값을 수정할 수 없다.
    ex: int, str, float, tuple
  • 가변객체 - 객체에 할당된 값을 수정할 수 있다.
    ex: list, dictionary

BTS혹은 레드벨벳의 멤버정보를 딕셔너리로 구현하라.

rv = {}
rv['wendy'] =28
rv['irene'] = 31
rv['joy'] = 26
rv['seulgi'] = 28
rv['yeri'] = 23
print(rv)
{'wendy':28, 'irene':31, 'joy':26, 'seulgi':28, 'yeri':23}

스트링, 리스트, 딕셔너리를 반복문으로 돌면서 인자를 출력하는 함수를 작성하라.

>> a = 'hello'
>> b = [1, 2, 3, 4]
>> c = {'wendy': 28, 'irene': 31, 'joy': 26, 'seulgi': 28, 'yeri': 23}
>> def enu(v):
	for i in v:
		print(i)
>> enu(a)
h
e
l
l
o
>> enu(b)
1
2
3
4
>> enu(c)
wendy
irene
joy
seulgi
yeri
>> enu(c.keys())
wendy
irene
joy
seulgi
yeri
>> enu(c.values())
28
31
26
28
23
>> enu(c.items())
('wendy', 28)
('irene', 31)
('joy', 26)
('seulgi', 28)
('yeri', 23)

for in 반복문을 작성해보고, break, continue의 쓰임새도 알아보아라.

>> count = 0
>> while count <10:
    count += 1
    if count <4:
        continue
    print('횟수:', count)
    if count ==8:
        break    
횟수: 4        
횟수: 5
횟수: 6
횟수: 7
횟수: 8

if와 else를 이용해 조건문을 작성해보아라.

>>for i in range(11):
	if i % 2 == 0:
    	print("{} is 짝수".format(i))
    else:
    	print("{} is 홀수".format(i))
>> 0 is 짝수
   1 is 홀수
   2 is 짝수
   3 is 홀수
   4 is 짝수
   5 is 홀수
   6 is 짝수
   7 is 홀수
   8 is 짝수
   9 is 홀수
   10 is 짝수

list method 중 append, pop, sort 을 활용한 함수를 작성해보아라.

>> rv = ['wendy','irene','seulgi','joy']
>> rv.append('yeri')
>> rv
['wendy', 'irene', 'seulgi', 'joy', 'yeri']
>> rv.pop()
'yeri'
>> rv.pop(0)
'wendy'
>> rv
['irene', 'seulgi', 'joy']
>> rv.sort()
>> rv
['irene', 'joy', 'seulgi']

파이썬 용어정리

profile
생각하는 개발자 되기

0개의 댓글