본문 바로가기

Coding Test

[Python] 잊어버린 기억을 찾아서...

 

print option

#separation
print('a', 'b', 'c', sep='&')
>> a&b&c

#end
print('a', end=' ')
print('b')
>> a b

#format
print("{0} b {1}" .format(a, c))
>> a b c

#%
print("%d은 숫자 %s는 문자" %(1, "abc"))
>> 1은 숫자 abc는 문자

 

 

 

반복문

#1) 1 ~ N까지의 홀수 출력
n = int(input())
for i in range(1, n + 1) :
    if i%2 == 1 :
    	print(i)
    
#2) 1 ~ N까지의 합 출력
n = int(input())
sum = 0
for i in range(1, n + 1) :
    sum += i
print(sum)

#3) N의 약수 출력
n = int(input())
for i in range(1, n + 1) :
    if n%1 == 0 :
    	print(i, end = ' ')

 

 

문자열 내장함수

#upper, lower
text = "Hello Python"
print(text.upper())
>> HELLO PYTHON

print(text.lower())
>> hello python

#find, count, slice
print(text.find('p'))
>> 6	#index

print(text.count('o'))
>> 2	#count

print(text[:2])
>> He	#index 0 ~ 2-1

#isupper, islower, isalpha : 대문자, 소문자, 알파벳(공백X) 여부 T/F 확인 가능
#ord() : 아스키코드 숫자 반환
#chr() : 아스키코드 문자 반환

 

 

리스트 내장함수

#list 선언
a = []
b = list()
c = [1, 2, 3, 4, 5]

#list간의 + 가능

#append
c.append(6)
print(c)
>> [1, 2, 3, 4, 5, 6]

#insert(index, value)
c.insert(2, 7)
print(c)
>> [1, 2, 7, 3, 4, 5, 6]

#pop : index
c.pop()
print(c)
>> [1, 2, 7, 3, 4, 5]

c.pop(2)
print(c)
>> [1, 2, 3, 4, 5]

#remove : value
c.remove(3)
print(c)
>> [1, 2, 4, 5]

#index : value
print(c.index(5))
>> 3

#sum, max, min
print(sum(c))
>> 12
print(max(c))
>> 5
print(min(c))
>> 1

#shuffle : import random 했다고 가정
random.shuffle(c)
print(c)
>> [1, 5, 4, 2]

#sort, sort(reverse=True)
c.sort()
print(c)
>> [1, 2, 4, 5]

c.sort(reverse=True)
print(c)
>> [5, 4, 2, 1]

#clear
c.clear()
print(c)
>> []

#######################

a = [12, 24, 31, 36, 50]

#print : index
for i in range(len(a)) :
    print(a[i], end = ' ')
print()

#print : value
for x in a :
    print(x, end = ' ')
print()

#enumerate : tuple(index, value)
for x in enumerate(a) :
    print(x)

for index, value in enumerate(a) :
    print(index, value)

#list 값 변경O, tuple 값 변경X

#if all : 모두 True
if all(60 > x for x in a) :
    print("True")
else :
    print("False")
    
#if any : 하나 이상 True
if any(15 > x for x in a) :
    print("True")
else :
    print("False")

 

 

2차원 리스트

#2차원 list 생성
a = [[0] * 3 for _ in range(3)]
print(a)
>> [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

#a[row][column]

 

 

함수

#Python function : define
def add(a, b) :
    return a + b

#return 여러개 가능 tuple 형태로 return
def calc(a, b) :
    add = a + b
    minus = a - b
    return add, minus
print(calc(3, 2))
>> (5, 1)

#소수 판단
def isPrime(x) :
    for i in range(2, x) :
    if x % i == 0 :
        return False	#return시 함수 종료
    return True
print(isPrime(2))
>> True

#lambda : sort에 유용
def plus_one(x) :	#def
    return x + 1
lam = lambda x : x + 1    #lambda

print(plus_one(2))
>> 3
print(lam(2))
>> 3

#map(함수, 자료) : 자료에 함수 적용, 원자료를 변경하지 않음
c = [1, 2, 3]

print(list(map(plus_one, c)))
>> [2, 3, 4]

print(list(map(lambda x : x + 1, c)))
>> [2, 3, 4]

 

 

알고리즘 공부를 위한 파이썬 복습 끝... 과연 끝인가

tab 왜 저렇게 나오지...

 

*강의 : 파이썬 알고리즘 문제풀이 입문(코딩테스트 대비)