elevne's Study Note
Python 기초 복습 본문
import math
math.radians(30)
Radian 각도의 값을 반환한다.
type(1) #int
type(12.1) #float
type("Hello") #str
type 반환한다.
x1 = 3
id(x1) #2297624160624
x2 = "Hello"
id(x2) #2297710128304
객체의 주소값을 반환한다.
x1 = 3
x1<<1 # x1*2 = 6
x1<<2 # x1*(2**2) = 12
x2 = 8
x2>>1 # x2/2 = 4
x2>>2 # x2/(2**2) = 2
shift 연산자이다.
name = "Harry"
name.lower() # 'harry'
x = 1
y = 2
(1 ^ 0) # XOR
lower() 메서드와 XOR 연산자이다.
abs(-33) # 절대값 33
# 문자열
"%s * %s = %s"%(3, 3, 3*3)
# 정수형
"%d * %d = %d"%(3, 3, 3*3)
pass # => 실행할 코드가 없다는 것을 의미
continue # => 다음 순번의 Loop 를 돌도록 강제
pass 와 continue 의 차이이다.
def varfunc(*args):
print(type(args))
print(args)
varfunc(10, 20, 30, 40, 60)
# <class 'tuple'>
# (10, 20, 30, 40, 60)
함수에서 여러 개의 인자를 위와 같은 방식으로 받아서 사용할 수 있다.
def varfunc2(**kwargs):
print(type(kwargs))
print(kwargs)
varfunc2(name="Steph", number=30, team="GSW")
# <class 'dict'>
# {'name': 'Steph', 'number': 30, 'team': 'GSW'}
위와 같이 Dictionary 형식 (key, value) 으로 받아서 사용해볼 수도 있다.
alist = [1, 2, 3, "asdf"]
print(*alist) # 1 2 3 asdf
print(*alist, sep=" :: ") # 1 :: 2 :: 3 :: asdf
위와 같이 iterable 객체를 언패킹해줄 수 있다.
def factorial(n):
if n == 1:
return(1)
else:
return n * factorial(n-1)
factorial(11) # 39916800
재귀함수를 사용하여 팩토리얼을 계산할 수 있다.
gx = 100
g = 200
def myfunc():
global gx
gx = g
print(gx)
myfunc() # 200
global 을 통해 함수 내에서 전역변수를 사용할 수 있다.
names = ["Harry", "Iron Man"]
names.index("Iron Man") # 1
names = ["Harry", "A", "Iron Man", "B", "C", "Iron Man"]
names.index("Iron Man", 3) # 5 (시작위치 지정가능)
names.pop(5)
names # ['Harry', 'A', 'Iron Man', 'B', 'C']
names.remove("A")
names # ['Harry', 'Iron Man', 'B', 'C']
names.sort()
names # ['B', 'C', 'Harry', 'Iron Man']
names.sort(reverse=True)
names # ['Iron Man', 'Harry', 'C', 'B']
sorted(names) # ['B', 'C', 'Harry', 'Iron Man']
names.count("B") # 1
names_copy = names.copy()
names_copy # ['Iron Man', 'Harry', 'C', 'B']
위와 같이 list 자료형에 기본 메서드들을 사용해볼 수 있다.
from functools import reduce
reduce(lambda x, y : print(y + "asdf"), names_copy)
# Harryasdf
# Casdf
# Basdf
reduce 함수는 여러 개의 데이터를 대상으로 주로 누적 집계를 내기 위해서 사용된다.
all(len(word) > 2 for word in names) # False
all(len(word) > 0 for word in names) # True
from itertools import accumulate
x = [1, 2, 3, 4]
list(accumulate(x)) # [1, 3, 6, 10]
list(filter(lambda x : x > 3, x)) # [4]
list(map(float, x)) # [1.0, 2.0, 3.0, 4.0]
x[::-1] # [4, 3, 2, 1]
그 외에도 all, any, accumulate, filter, map 등 다양한 메서드를 사용해볼 수 있다.
A = [1, 2, 3, 4, 5, 6, 7]
B = [5, 6, 7, 8, 9, 10]
A = set(A)
B = set(B)
C = A | B # {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} or A.union(B)
D = A & B # {5, 6, 7} or A.intersection(B)
E = A - B # {1, 2, 3, 4} or A.difference(B)
B.issubset(A) # False
Set 자료형에서 사용할 수 있는 메서드들이다.
capitals = {"Korea" : "Seoul", "USA" : "Washington", "UK" : "London"}
city = capitals.pop("UK") # 'London'
capitals # {'Korea': 'Seoul', 'USA': 'Washington'}
for key in capitals:
print(key)
# Korea
# USA
for key, value in capitals.items():
print(key, value)
# Korea Seoul
# USA Washington
Dict 자료형에 사용할 수 있는 메서드들이다.
chr(1) # 숫자 to 문자열 '\x01'
ord("H") # 문자열 to 숫자 72
name = "harry"
name.find("h") # 0
name.rfind("r") # 3
name.count("r") # 2
name.isalpha() # True
name.isdigit() # False
name.islower() # True
문자열 자료형에 사용할 수 있는 메서드들이다.
alist = ["1", "2", "3"]
",".join(alist) # '1,2,3'
random.sample("asdfqwerzxcv", 8) # ['q', 'r', 'c', 'd', 'e', 'z', 's', 'x']
이러한 메서드도 사용해볼 수 있다.
'Backend > Python + FastAPI' 카테고리의 다른 글
OpenCV 를 활용한 동영상 처리 (Face Detection) (0) | 2023.04.03 |
---|---|
@property 데코레이터에 대하여 (0) | 2023.04.02 |
~ 연산자에 대하여 (0) | 2023.04.01 |
Python Class - Magic Method (0) | 2023.03.30 |
Python Collections 간단하게 알아보기 (0) | 2023.02.28 |