x = 11
if x < 10 :
print('Smaller')
else :
print('Bigger')
x = 6
if x < 2 :
print('Small')
elif x < 20 :
print('Medium')
elif x < 10 : //확인안함
print('Medium')
else : //확인안함
print('Ginormous')
astr = "123"
try://성공할경우
print("Hello")
isInt = int(astr)
print("World")
except://실패할경우
isInt = "Integer로 변환할 수 없습니다."
print('Done', isInt)
# Hello
# World
# Done 123이 순서대로 출력됩니다.
가장 작은 수를 찾는 코드 완성하기
is 연산자는 None 사용할 때
smallest = None
print('Before')
numbers = [9, 41, 12, 3, 74, 15]
for value in numbers :
if smallest is None :
smallest = value
elif value < smallest :
smallest = value
print(smallest, value)
print('After', smallest)
부울값을 사용하여 특정 값을 검색하기
found = False
print('Before', found)
numbers = [9, 41, 12, 3, 74, 15]
for value in numbers :
if value == 3 :
found = True
print(found, value)
break # 특정 값을 찾았을때 해당 루프를 종료하는 것이 더욱 적절해 보입니다.
print('After', found)
up to but not including
s[0:4]
abcd 0123
.upper() #대문자로
.lower()
dir('ssss') #ssss로 할 수 있는걸 보여줌
.find(' ', 31) #31 이후에 공백이 몇번째에 있는지
str.replace('Bob','Jane') #Bob을 Jane으로 바꿈
str.lstrip() #왼쪽 공백 없앰
str.rstrip() #오른쪽 공백 없앰
str.strip() #양쪽 공백 없앰
line.startwith('aaa') #T or F
list에.append('요소')
is sth in a list? → 9 in list
list를.split()
list.split(';')
dic()
{} key : value
counts = dict()
names = ['csev','cwen']
for name in names :
if name not in counts :
counts[name] = 1
else :
counts[name] = counts[name] + 1
print(counts)
if name in counts:
x = counts[name]
else :
x = 0
x = counts.get(name,0)
.keys()
.values()
.items() //튜플
for aaa,bbb in jjj.items() :
print(aaa,bbb)
name = input('Enter file : ')
handle = open(name)
counts = dict()
for line in handle:
words = line.split()
for word in words:
counts[word] = counts.get(word, 0) + 1
bigcount = None
bigword = None
for word,count in counts.items():
if bigcount is None or count > bigcount :
bigcount = count
bigword = word
print(bigword, bigcount)
tuple () immutable( similar to a string)
for k,v in sorted(d.items()):
#키를 기준으로 정렬하기
d = {'b':1, 'a':10, 'c':22}
d.items()
# dict_items([('b', 1), ('a', 10), ('c', 22)])
sorted(d.items())
# [('a', 10), ('b', 1), ('c', 22)]
#값을 기준으로 정렬하기
c = {'a':10, 'b':1, 'c':22}
tmp = list()
for k, v in c.items() :
tmp.append( (v, k) )
print(tmp)
# [(10, 'a'), (1, 'b'), (22, 'c')]
tmp = sorted(tmp, reverse=True)
print(tmp)
# [(22, 'c'), (10, 'a'), (1, 'b')]
#가장 많이 등장한 단어 Top 10 출력하기
fhand = open('romeo.txt')
counts = {}
for line in fhand:
words = line.split()
for word in words:
counts[word] = counts.get(word, 0 ) + 1
lst = []
for key, val in counts.items():
newtup = (val, key)
lst.append(newtup)
lst = sorted(lst, reverse=True)
for val, key in lst[:10] :
print(key, val)
c = {'a':10, 'b':1, 'c':22}
tmp = list()
for k, v in c.items() :
tmp.append( (v, k) )
tmp = sorted(tmp)
print(tmp)
# [(1, 'b'), (10, 'a'), (22, 'c')]
그리고 다음의 코드는 정확히 위의 코드와 동일한 역할을 합니다.
c = {'a':10, 'b':1, 'c':22}
print( sorted( [ (v,k) for k,v in c.items() ] ) )
# [(1, 'b'), (10, 'a'), (22, 'c')]
'Python' 카테고리의 다른 글
week5 - ch02. HTML (0) | 2022.07.26 |
---|---|
week5 - Ch01. 강의 개요 (0) | 2022.07.25 |
d (0) | 2021.11.21 |
211112 파이썬 벼락치기 (0) | 2021.11.13 |
참고용 (0) | 2021.11.02 |