본문 바로가기

Python/왕초보를 위한 파이썬50

7.5. 특별한 메서드들 이제 메서드에 대해서는 다들 알고 계시겠죠? 메서드라는 것은 우리가 클래스를 만들면서 그 안에 만들어 넣은 함수를 말하지요? 만들어진 메서드를 사용하려면 객체.메서드()와 같은 형식으로 호출을 해주었구요. 오늘은 그런 일반적인 메서드들과는 조금 다른 특별한 메서드들에 대해 함께 알아보려고 합니다. __init__ 메서드 (초기화)# bookstore.py class Book: def setData(self, title, price, author): self.title = title self.price = price self.author = author def printData(self): print '제목 : ', self.title print '가격 : ', self.price print '저자 : '.. 2012. 4. 12.
Python 문자열 1. 시퀀스 자료형 : 문자열, 리스트, 튜플 1) 인덱싱(indexing) : [k] >>> s = 'abcdef' # 문자열 >>> l = [100, 200, 300] # 리스트 >>> s[0] # 참조 'a' >>> s[1] 'b' >>> s[-1] 'f' >>> l[1] 200 >>> l[1] = 900 # 치환 2) 슬라이싱(Slicing) : [s:t] >>> s = 'abcdef' >>> l = [100, 200, 300] >>> s[1:3] # 1번 위치와 3번 위치 사이를 나타냄 'bc' >>> s[1:] # 1부터 끝까지 'bcdef' >>> s[:] # 처음부터 끝까지 'abcdef' >>> s[-100:100] # 범위를 넘어서면 범위 내의 값으로 자동 처리 'abcdef' >>> .. 2012. 4. 10.
Python 실습_Replace at Index Replace at Index You can replace a specific character in a string by creating a new string that consists of all characters up to some index, the new character than you want to insert, and then the remainder of the original string. Create the function replace_at_index() that accepts a string, an index, and a character as inputs and returns a string with the character at the index replaced with th.. 2012. 4. 10.
Python 실습_After Index After Index You can return a portion of a string from some index onwards by using the bracket operator and and the expression index:. For example: if color = 'blue' then color[1:] = 'lue' since the result is all of the characters in color from the index onwards. Create a function called after_index() that accepts a string and a number n as parameters and returns a string consisting of the charac.. 2012. 4. 10.