리스트 컴프리핸션

1
2
3
4
5
6
7
n = 4
m = 3

array = [[0] \* m for \_ in range(n)]

array[0][1] = 5
print(array)

2차원 배열 참조값 복사 오류

1
2
3
4
5
6
7
8
n1 = 4
m1 = 3

# 참조값이 복사되어 원하는 부분 이외의 요소도 변경됨

array1 = [[0]*m]*n
array1[0][1] = 5
print(array1)

dictionary

1
2
3
4
5
6
7
8
9
10
data = dict()
data['apple'] = '사과'
data['banana'] = '바나나'

if 'apple' in data:
print(data['apple']+'가 존재합니다.')

key_list = list(data.keys())

print(key_list)

set 자료형

1
2
3
4
5
6
c = 3.11
print(int(c))

sett = set([1,2,3])
print(sett)
print(sett)

입력

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
n = int(input())

data = list(map(int,input().split()))

x,y,z = map(int,input().split())

print(n)

print(data)

print(x,y,z)

# sys.stdin.readline() 빠른 입력

import sys

data1 = sys.stdin.readline().rstrip();

print(data1)

출력

1
2
3
4
5
6
7
8
9
f = 1
g = 2

print(f,g)
print(f, end=' ')
print(g, end=" ")

answer = 7
print(f"정답은 {answer}입니다.")

조건문

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
x = 15
if x >= 10:
print('X는 10이상입니다.')

if x >= 0:
print('X는 0 이상입니다.')

if x >= 0 and x <= 100:
print('X는 0보다 크거나 같고 100보다 작거나 같습니다.')

res = 'cool' if x>10 else 'fail'
print(res)

if x > 12: rest = 'cooler'

print(rest)

반복문

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
i = 1
result = 0

while i <= 9:
result += i
i += 1

print(result)

arr = [9,8,7,6,5,4]

for x in arr:
print(x)

rss = 0
for k in range(1,10):
rss += k

print(rss)

for u in range(2,10):
for o in range(1,10):
print(f"{u} X {o} = {u\*o}")
print()

함수

1
2
3
4
5
6
7
8
9
10
gf = 0

def func():
global gf
gf += 1

for \_ in range(10):
func();

print(gf)

람다표현식

1
2
3
4
5
6
7
8
9
10
11
12
print((lambda a,b: a+b)(33,7))

people = [('홍길동',10),('이순신', 5),('아무개',70)]

print(sorted(people,key=lambda x: x[1]))

list1 = [1,2,3,4,5]
list2 = [6,7,8,9,10]

list_result = map(lambda a,b:a+b, list1, list2)

print(list(list_result))

내장함수

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
minvalue = min(10,2,3,7)

print(minvalue)

evalvalue = eval('3\*5-2');

print(evalvalue)

from itertools import permutations
from itertools import combinations
from itertools import product
from itertools import combinations_with_replacement

dataaa = ['a','b','c']

print(list(permutations(dataaa,3)))
print(list(combinations(dataaa,2)))

# 2개를 뽑는 모든 순열 (중복포함)

print(list(product(dataaa,repeat=2)))

# 2개를 뽑는 모든 조합 (중복포함)

print(list(combinations_with_replacement(dataaa,2)))

# 객체의 갯수 구하기

from collections import Counter

counter = Counter(['red','blue','red','green','blue','blue'])

print(counter['blue'])
print(counter['green'])
print(dict(counter))

# 최대 공약수, 최소 공배수

import math

def lcm(a,b):
return a\*b // math.gcd(a,b);

v = 21
j = 14

print(math.gcd(v,j))
print(lcm(v,j))