Python18

(useful) Methods 😲 파이썬에는 정말 다양하고 유용한 method가 존재한다. 여러 코딩 문제를 풀면서 다양한 method를 활용해보았는데, 이번 포스팅을 통해 처음 보는, 유용한 method만 골라 간단히 정리하고자 함 :) #rjust 😲 지정한 첫번째 인자 글자 길이만큼 문자열이 오른쪽으로 나열 - 그 앞의 공간은 두번째 인자로 채워짐 print('abcd'.rjust(5,"0")) #'0abcd' #zfill 😲 위 rjust()와 비슷하되, 0이 채워진다고 생각하면 됨 → zfill syntax string.zfill(len) #'Required. A number specifying the desired length of the string'# → len 개수만큼의 문자열이 만들어지고, 기존 문자열이 차지하는 자.. Python/Fundamentals 2022. 11. 13.
python standard libraries 🍹 python에서 실전으로 유용하게 사용하는 표준 라이브러리들을 이번 포스팅을 기회삼아 정리해보려 한다 Module & Package1. Module [1] 필요성 및 정의 * 코드가 당연히 길어지는 상황에서 모든 함수, 변수를 구현하는 것은 불가능하다. 따라서 누군가 만들어놓은 함수, 변수 등을 활용해야 한다 * 모듈 = 특정 목적을sh-avid-learner.tistory.com 🍹 모듈, 패키지가 무엇인지는 상단 포스팅에서 가볍게 다룬 적이 있다. 🍹 libraries종류설명내장함수기본적인 함수들 제공 (필수적인 기능 포함)(print, input() 등등)itertools반복되는 형태의 데이터 처리 (특히 순열, 조합)heapqheap 자료구조 제공 (주로 우선순위 queue 기능 구현)b.. Python/Fundamentals 2022. 8. 22.
λ 표현식 🤘🏻 파이썬에서 λ 표현식을 이용하면 함수를 간단하게 작성할 수 있다. 즉, 특정한 기능을 수행하는 함수를 단 한 줄에 작성할 수 있다는 게 큰 특징 #lambda 표현식으로 구현한 더하기 함수 print((lambda a, b: a +b)(3,7)) 🤘🏻 쓰는 방법 ① 특수문자 lambda 키워드 ② 매개변수를 쭉 나열 ③ 이후 콜론(:) 입력 ④ 입력된 매개변수를 더한 결괏값 - 리턴될 출력 결과를 콜론(:) 다음에 입력 - 출력 ※ 변수를 새로 사용해서 return하는 경우 lambda 표현식을 사용할 수 없음 ※ 🤘🏻 함수 자체를 입력으로 받는 함수, 또는 함수 자체가 return문으로 사용되는 함수를 사용할 때, 또는 함수 자체가 매우 간단해 한 번만 사용하고 말 때 주로 lambda 식을 이용.. Python/Fundamentals 2022. 8. 21.
list, string, tuple, dictionary, set (iterables) ** iterable의 대표 5가지 list, string, tuple, dictionary, set에 대해 깔끔히 정리해보려 한다! ** 파이썬의 대표 자료 저장 방식으로, 반드시 알아야 하는 개념! 꼭 숙지하도록 하자 1. list * intro → 대괄호 []로 묶어서 표시 → list()로 list를 만들 수 있음 print(list(range(1,11))) #[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print(list('hello')) #['h', 'e', 'l', 'l', 'o'] → []안에는 ,로 분리하여 여러 개 요소를 담을 수 있음 → 숫자, 문자열, 그리고 숫자와 문자를 섞은 모든 list 구현 가능(즉, 한 list 내에 다양한 data type이 들어가도 가능) *.. Python/Fundamentals 2022. 8. 19.
File/Exception/Log Handling * coursera 강좌에서 exceptiong handling에 대해 간략하게 배웠다. 관련 내용에 대해 좀 더 자세히 알아보자!! Python Basics(1). (from Coursera) 1) Python Basics # Types e.g) 11 (int) / 12.32 (float) - can check the specifics of floats 'sys.float_info' / "I love Python" (str) / True, False (bool) → By using the data type, we can see the a.. sh-avid-learner.tistory.com Excpetion Handling ① 예상 가능한 예외: 발생 여부를 사전에 인지할 수 있는 예외로, 개발자가 반.. Python/Fundamentals 2022. 7. 14.
python OOP * python에서 반드시 알아야 할 OOP(Object-Oriented Programming) 개념에 대해서 종전보다 좀 더 깊게! 알아보자 😍 → 실제 세상을 모델링 concepts> * 객체 - 속성(attribute) & 행동(action) 두 개를 가짐 → OOP는 이러한 객체 개념을 프로그램으로 표현한다. 속성은 변수(variable), 행동은 함수(method)로 표현됨 * OOP는 설계도에 해당하는 class와 실제 구현체인 instance로 나눔 * __init__은 객체 초기화 예약 함수 + parameter로 여러 속성 정보 + self ★ __는 특수한 예약 함수나 변수 그리고 함수명 변경 맨글링(name mangling)으로 사용 - magic method ex) __main__,.. Python/Fundamentals 2022. 7. 7.
Python Basics(2). (from Coursera) 4) Working with Data in Python # Reading & Writing Files with Open * Reading Files with Open - open) provides a File object that contains the methods and attributes you need in order to read, save, and manipulate the file, the first parameter is the file path and file name / the second argument is mode(optional): default value is r - It is very important that the file is closed in the end. This .. Python/Fundamentals 2022. 4. 13.
Web Crawling(Scraping) 개요 + DOM 기초 🕵🏻 우리가 주어진 data는 대게 정돈된 structured data가 아닐 확률이 높다. 특히 web에 흩뿌려진 data를 가져오는 경우가 종종 있는데, 이 때 web scraping이라는 기법을 사용함! (개인적으로 너무 재밌는 web scraping 🏄🏻‍♀️) Q. Web Scraping vs. Web Crawling? ≫ web scraping은 우리가 찾을 'data'에 초점을 둔 것 / web crawling은 우리가 찾을 장소인 'url'에 초점을 둔 것. 대게 crawling과 scraping 과정을 병행한다. (web scraping is about extracting the data from one or more websites. While crawling is about findi.. Python/Web Crawling+Scraping 2022. 4. 5.
Python Basics(1). (from Coursera) 1) Python Basics # Types e.g) 11 (int) / 12.32 (float) - can check the specifics of floats 'sys.float_info' / "I love Python" (str) / True, False (bool) → By using the data type, we can see the actual data type (type(11) = int) → type casting) change the type of the expression (float(2), int(2.2), string(1), int(True) = 1) # Expressions & Variables → Expressions describe a type of operation the .. Python/Fundamentals 2022. 4. 4.
시계열 데이터 - datetime ▧ python에서는 특별하게 datetime이라는 type이 존재한다 ▧ from datetime import datetime → 시계열 데이터로 바꾸기 위해서는 'xxxx-xx-xx (년-월-일)' data에 datetime()을 붙인다 dt = datetime(2022,1,24,21,30,42) dt.year #2022 dt.month #1 dt.day #24 dt.hour #21 dt.minute #30 dt.second #42 → 시계열 data는 indexing / slicing이 가능! (문자열과 다른 점) ex) pandas_datareader를 이용해 data를 import하고 '삼성증시' dataframe을 가져온다고 하면 !pip install pandas_datareader from.. Python/Fundamentals 2022. 3. 24.
(예제) - 한국 도쿄올림픽 medal count 가져오기 - ! -- 저번 포스팅에서는 HTML 형식의 파일이 존재한다면 open()을 이용해서 가져오고 BeautifulSoup parsing을 통해 직접 데이터를 검색했던 적이 있었다. 이번에는 직접 web browser url을 이용하여 web page를 가져오고 원하는 data를 찾아 출력하는 실습을 해보겠다 -- ! (하단 HTML file - BeautifulSoup 검색 포스팅) HTML 문서를 BeautifulSoup으로 검색하기 (+re module) ! -- web browser에서 직접적인 url을 가져와 parsing하는 web crawling은 아니지만 web에 존재하는(또는 컴퓨터에 자체적으로 존재하는) HTML file 내용을 가져와 나타내는 방법을 알아본다 -- ! ** Beautiful.. Python/Web Crawling+Scraping 2022. 3. 22.
HTML 문서를 BeautifulSoup으로 검색하기 (+re module) ! -- web browser에서 직접적인 url을 가져와 parsing하는 web crawling은 아니지만 web에 존재하는(또는 컴퓨터에 자체적으로 존재하는) HTML file 내용을 가져와 나타내는 방법을 알아본다 -- ! ** BeautifulSoup library tolerates highly flawed HTML & still lets you easily extract the data you need (repairs & parses HTML to make it easier for a program to understand) 1. import & parsing 1> BeautifulSoup import 2> open()의 read() method를 사용하여 sample.html file을 받는.. Python/Web Crawling+Scraping 2022. 3. 21.