본문 바로가기
  • 紹睿: 자유롭고 더불어 사는 가치있는 삶

Data/python·알고리즘39

[python 기초] python의 file mode 파일 열기 1. File mode 값 설명 'r' 읽기 'w' 쓰기 'a' append 'b' binary(다른 mode에 추가) '+' Read/write(다른 mode에 추가 Binary 모드에서는 MS windows에서의 \n\r을 변환시키는 문제 등을 해결한다. 2. 파일관련 methods - 파일 읽기 및 쓰기 f = open('py_test.txt', 'w') f.write('hello') f.close() f = open('py_test.txt', 'r') print(f.readline()) # Open source software is made better when users can easily contribute code and documentation to fix bugs and add.. 2018. 6. 29.
[python 기초] python의 함수에 대하여 Python 함수: Functional Language 특징을 가지고 있음 추상화의 한 단계 1. 함수의 작성 def method1() def square(x): return x*x print(square(5)) # 25 Parameter: abc(x, y) 정의 시: Formal parameter 호출 시: Actual parameter 1. Loop 변수의 문제 local scope(자신이 지정된 function 또는 block)만 을 이용하는 변수 함수 내에서는 기본적으로 별도의 copy본을 만들어 사용 (두 x는 다른 아이들임) def inc(x): return x+1 x = 20 print(inc(x)) # 21 print(x) # 20 2. return 값을 굳이 주지 않아도 됨 def cha.. 2018. 6. 28.
[python 기초] python의 module에 대하여 module 프로그램 파일 Folder 단위(Directory)로 관리함 = package 1. module 작성 mymath.py """my math""" pi = 3.14159 def area(r): global pi return (pi*r**2) 2. module 이용 from import 를 이용해서 다른 module들을 가져다 쓸수 있음 import mymath print(mymath.area(5)) # 78.53975 print(mymath.pi) # 3.14159 3. 검색경로 python 경로안에 설치 sys.path 변경 PYTHONPATH 이용 .pth 파일 작성 4. Private name 5. Scoping Rule Local > Global > Built-in 2018. 6. 28.
[python 기초] 조건문과 Loop문 + List Comprehension Block 지정- Indentation - level 당 4 spaces 조건문1. 조건지정(Boolean 값)1) False- False, None, 0, "", {}, (), []2) True- True, - False가 아닌 기타의 모든 것 2. 조건실행- if, elif, else 1234567891011121314var = 100if var 2018. 6. 28.
[python 기초] python의 dictionary에 대하여 Ditionary{1:one, 2:two}mapping: key값을 가지고 찾음! 1. 기본적인 사용 방법: {}12345678910111213141516171819202122months = {1:'Jan', 2:'Feb', 3:'Mar', 4:'Apr', 5:'May', 6:'Jun', 7:'Jul', 8:'Aug', 9:'Sep', 10:'Oct', 11:'Nov', 12:'Dec'} print(months[1]) #Jan items = [('name', 'May'), ('birth', 5)]dict_items = dict(items)print(dict_items) # {'name': 'May', 'birth': 5}print(len(dict_items)) # 2 del dict_items['birt.. 2018. 6. 28.
[python 기초] python의 String에 대하여 String문자열 "~~"'~~'"""~~"""- input()을 이용하면 string으로 저장되었었음 1. Raw String: 특수 부호\n (next line)\t (tab만큼 이동)' "r'o': 불가침 영역으로 인정1234567891011121314151617path = "c:\temp"print(path) # c: emp # \를 이용print("c:\\temp") # c:\temp # \ escape sequencepath = 'c:\\program File\\python\\bin'print(path) # c:\program File\python\bin # r''이용print(r'c:\program File\python\bin') #c:\program File\python\bin #Let's.. 2018. 6. 27.