모듈: 파이썬 정의와 문장들을 담고 있는 파일import 모듈 __name__을 통해 모듈 이름 확인 import sys print(sys.__name__)모듈 안에 있는 이름들을 직접 임포트 할 수 있음특정 기능들만 임포트 가능 from sys import copyright print(copyright)as임포트한 모듈을 특정 이름으로 바꿔 사용가능 import sys as system print(system.copyright) Reference: https://docs.python.org/ko/3.6/tutorial/modules.html
1. Class Definition Syntax클래스: 데이터와 기능을 묶을 수 있음 인스턴스: 클래스로 찍어낸 객체123456class MyClass: """A simple example class""" i = 12345 def f(self): return 'hello world' 2. Class Objectsattribute 참조12345678910class MyClass: """A simple example class""" i = 12345 def f(self): return 'hello world' print(MyClass.i)print(MyClass.f)print(MyClass.__doc__) 인스턴스 생성 12345678class MyClass: """A simple example class"..
1. Handling Exceptions 선택한 예외를 처리하는 프로그램 가능 먼저, try 부분이 실행됨예외가 발생하지 않으면 except 부분을 건너 뛰고 try 구문 종료예외가 발생하면 except 부분으로 건너 뛰고 try 구문 종료except 부분에 매치되는 에러가 없다면 상위 try 구문으로 에러 전달123456while True: try: x = int(input("Please enter a number: ")) break except ValueError: print("Oops! That was no valid number. Try again...") 마지막 except 절은 예외 이름 생략 가능와일드 카드 역할 기능12345678910111213import sys try: f = open(..
1. Tuples and Sequences 출력되는 튜플은 항상 괄호로 둘러싸임12345678910111213141516t = 12345, 54321, 'hello!'t[0] t # Tuples may be nested:u = t, (1, 2, 3, 4, 5)u # Tuples are immutable:t[0] = 88888 # but they can contain mutable objects:v = ([1, 2, 3], [3, 2, 1])vColored by Color Scriptercs 빈 튜플은 빈 괄호쌍으로 만들어짐1234567empty = ()singleton = 'hello', #
1. Dictionary 키와 값으로 이루어짐 키는 중복될 수 없음숫자 대신에 키로 인덱싱됨1234tel = {'jack': 4098, 'sape': 4139}tel['guido'] = 4127tel Colored by Color Scriptercs dict()키-값 쌍들의 시퀀스로 딕셔러니 생성 가능1dict([('sape', 4139), ('guido', 4127), ('jack', 4098)]) 삭제123del tel['sape']tel['irv'] = 4127tel Reference: https://docs.python.org/ko/3.6/tutorial/datastructures.html#dictionaries
1. Sets 중복요소가 없는 순서 없는 컬렉션 집합을 만들 경우 중괄호나 set() 함수를 사용빈 집합을 만들 경우 set() 함수를 사용1234567891011121314151617181920basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}print(basket) # show that duplicates have been removed 'orange' in basket # fast membership testing 'crabgrass' in basket # Demonstrate set operations on unique letters from two words a = set('abracadabra')b = set('alacazam'..
1. Defining Functions def 키워드는 함수의 정의의 시작을 의미파라미터의 값들은 값에 의한 호출(Call by Value)로 전달123456789101112131415161718def fib(n): # write Fibonacci series up to n """Print a Fibonacci series up to n.""" a, b = 0, 1 while a None 값fib(0)print(fib(0))Colored by Color Scriptercs 2. Default Argument Values 필수 인자와 선택 인자를 사용하여 정의 가능 함수 호출시 선택 인자는 입력하지 않아도 무방1234567891011def ask_ok(prompt, retries=4, reminder='P..
1. for 문 리스트나 문자열의 항목들을 순서대로 이터레이션1234words = ['cat', 'window', 'defenestrate'] for w in words: print(w, len(w)) 시퀀스를 이터레이트 할 때 사본이 만들어지지 않음 따라서 수정할 경우 슬라이싱을 통해 사본을 만들어 수정 권장12345words = ['cat', 'window', 'defenestrate'] for w in words[:]: if len(w) > 6: words.insert(0, w) 2. range()range(stop) ☞ 0 ~ stop - 1range(start, end) ☞ start ~ end - 1range(start, end, step) ☞ start ~ end - 1, step 크기만큼 ..