Reading files

파이썬은 readlines() 함수를 통해 파일 전체를 읽을 수 있다.

#!/usr/bin/env python
 
filename = "readme.txt"
 
with open(filename) as fn:
    content = fn.readlines()
 
print(content)


Writing files

w 파라미터는 파일을 쓰기 위함을 파이썬에게 알려주는 것이고,

write()함수를 통해 파일에 데이터 기록 후 close()로 파일을 닫는다.

#!/usr/bin/env python
 
f = open("output.txt","w")
f.write("Pythonprogramminglanugage.com, \n")
f.write("Example program.")
f.close()


'프로그래밍 > Python' 카테고리의 다른 글

Python Tutorial : Line charts [12]  (0) 2016.04.16
Python Tutorial : Statistics [11]  (0) 2016.04.15
Python Tutorial : Dictionary [9]  (0) 2016.04.15
Python Tutorial : Tuples [8]  (0) 2016.04.15
Python Tutorial : Lists [7]  (0) 2016.04.15

파이썬에서는 중괄호를 이용하여 딕셔너리를 이용한 key-value 코드 작성이 가능하다.

k라는 dictionary 정의:

k = { 'EN':'English', 'FR':'French' }
print(k['EN'])


k에 key-value pair 추가

k['DE'] = 'German'


key-value pair 삭제

k = { 'EN':'English', 'FR':'French' }

del k['FR']
print(k)


'프로그래밍 > Python' 카테고리의 다른 글

Python Tutorial : Statistics [11]  (0) 2016.04.15
Python Tutorial : Read and Write File [10]  (0) 2016.04.15
Python Tutorial : Tuples [8]  (0) 2016.04.15
Python Tutorial : Lists [7]  (0) 2016.04.15
Python Tutorial : Functions [6]  (0) 2016.04.15

파이썬에서 튜플은 수정할 수 없는 콜렉션이다. 튜플은 괄호를 사용하여 정의한다.

하나의 아이템을 가지는 튜플

x = (1,)


여러 개의 아이템을 가지는 튜플

x = (1,2,3,4)


Accessing tuples

튜플의 개별 요소에 접근하려면 대괄호를 이용한다.

print(x[0])


튜플의 마지막 요소에 접근하려면 리스트와 같이 -1번째 인덱스로 접근 가능하다.

print(x[-1])

'프로그래밍 > Python' 카테고리의 다른 글

Python Tutorial : Read and Write File [10]  (0) 2016.04.15
Python Tutorial : Dictionary [9]  (0) 2016.04.15
Python Tutorial : Lists [7]  (0) 2016.04.15
Python Tutorial : Functions [6]  (0) 2016.04.15
Python Tutorial : While loop [5]  (0) 2016.04.15

+ Recent posts