본문 바로가기
ONLINE COURSES/PYTHON

부스트코스 || 평균구하기 // split // Dictionary

by jono 2021. 5. 23.

0. 파이썬으로 평균을 구하는 두가지 방법

stuff = list()
stuff.append('book')
stuff.append(99)
print(stuff)
stuff.append('cookeie')
print(stuff)


#way1
total = 0
count = 0
while True:
	inp = input("Enter a number:")
	if inp == 'done' : break
	value = float(inp)
	total = total + value
	count = count +1
average = total / count
print("average :", average)

#way2
numlist = list()
while True:
	inp = input("Enter a number:")
	if inp == 'done': break
	value = float(inp)
	numlist.append(value)
average = sum(numlist)/len(numlist)
print("average: ", average)

 

1. split

abc = "with three words"
stuff= abc.split()
print(stuff)
print(len(stuff))
print(stuff[0])

print(" ")
for w in stuff:
	print(w)       #한줄씩 띄워서 반환.

print(" ")
a = 'first;second;third'
thing = a.split()
print(thing)         # ()안에 아무것도 없음 == 공백을 기준으로 split적용.
print(len(thing))    # 나뉘지 않음 == len == 1

print(" ")
thing = a.split(';')     # ';'' 기준으로 split적용
print(thing)
print(len(thing))

 

2. Dictionary

- Basic concept of dictionary

purse = dict()
purse['money'] = 12         # money, candy, tissue == 'key''
purse['candy'] = 3          # number for each keys == 'value'
purse['tissue'] = 6
print(purse)
print(purse['candy'])
purse['candy'] = purse['candy'] + 3
print(purse)


- Two ways to count the number of keys in dictionary

print("way1_using: in")
count = dict()
names = ['a', 'b', 'c', 'd']
for name in names:
	if name in count:
		count[name] = count[name]+1
	else:
		count[name] = 1
print(count)


print('way2_using: not in')
count=dict()
names = ['a', 'b', 'c', 'd']
for name in names:
	if name not in count:
		count[name] = 1
	else:
		count[name] = count[name]+1
print(count)

댓글