행위

"파이썬 입문"의 두 판 사이의 차이

DB CAFE

(인스턴스 메소드)
23번째 줄: 23번째 줄:
 
<source lang=python>
 
<source lang=python>
 
>>> names = [ ]
 
>>> names = [ ]
>>> names = [‘a’, ‘b’, ‘c’, ‘d, ‘e’]
+
>>> names = ['a', 'b', 'c', 'd, 'e']
 
</source>
 
</source>
  
 
==== len(s) ====
 
==== len(s) ====
 
<source lang=python>
 
<source lang=python>
>>> names = [‘a’, ‘b’, ‘c’, ‘d, ‘e’]
+
>>> names = ['a', 'b', 'c', 'd, 'e']
 
>>> len(names)
 
>>> len(names)
 
5
 
5
34번째 줄: 34번째 줄:
 
==== indexing ====
 
==== indexing ====
 
<source lang=python>
 
<source lang=python>
>>> names = [‘a’, ‘b’, ‘c’, ‘d, ‘e’]
+
>>> names = ['a', 'b', 'c', 'd, 'e']
 
>>> names[0]
 
>>> names[0]
‘a’
+
'a'
 
>>> names[1]
 
>>> names[1]
‘b’
+
'b'
 
</source>
 
</source>
 
==== append ====
 
==== append ====
 
<source lang=python>
 
<source lang=python>
>>> names = [‘a’, ‘b’,’c’]
+
>>> names = ['a', 'b','c']
>>> names.append(‘d’)
+
>>> names.append('d')
 
>>> names
 
>>> names
[‘a’, ‘b’, ‘c’, ‘d’]
+
['a', 'b', 'c', 'd']
 
</source>
 
</source>
 
==== insert ====
 
==== insert ====
 
<source lang=python>
 
<source lang=python>
>>> names = [‘a’, ‘b’, ‘c’]
+
>>> names = ['a', 'b', 'c']
>>> names.insert(1, ‘e’)
+
>>> names.insert(1, 'e')
 
>>> names
 
>>> names
[‘a’, ‘e’, ‘b’, ‘c’]
+
['a', 'e', 'b', 'c']
 
</source>
 
</source>
 
==== comprehension ====
 
==== comprehension ====
64번째 줄: 64번째 줄:
 
==== 생성 ====
 
==== 생성 ====
 
<source lang=python>
 
<source lang=python>
>>> names = (‘a’, ‘b’, ‘c’, ‘d, ‘e’)
+
>>> names = ('a', 'b', 'c', 'd, 'e')
 
</source>
 
</source>
 
==== len(s) ====
 
==== len(s) ====
 
<source lang=python>
 
<source lang=python>
>>> names = (‘a’, ‘b’, ‘c’, ‘d, ‘e’)
+
>>> names = ('a', 'b', 'c', 'd, 'e')
 
>>> len(names)
 
>>> len(names)
 
5
 
5
74번째 줄: 74번째 줄:
 
==== indexing ====
 
==== indexing ====
 
<source lang=python>
 
<source lang=python>
>>> names = (‘a’, ‘b’, ‘c’, ‘d, ‘e’)
+
>>> names = ('a', 'b', 'c', 'd, 'e')
 
>>> names[0]
 
>>> names[0]
‘a’
+
'a'
 
>>> names[1]
 
>>> names[1]
‘b’
+
'b'
 
</source>
 
</source>
  
89번째 줄: 89번째 줄:
 
==== insert ====
 
==== insert ====
 
<source lang=python>
 
<source lang=python>
>>> cur_price[‘samsung’] = 10000
+
>>> cur_price['samsung'] = 10000
 
>>> cur_price
 
>>> cur_price
{‘samsung’: 10000}
+
{'samsung': 10000}
 
</source>
 
</source>
 
==== indexing ====
 
==== indexing ====
 
<source lang=python>
 
<source lang=python>
>>> cur_price[‘samsung’]
+
>>> cur_price['samsung']
 
>>> 10000
 
>>> 10000
 
</source>
 
</source>
101번째 줄: 101번째 줄:
 
==== delete ====
 
==== delete ====
 
<source lang=python>
 
<source lang=python>
>>> del cur_price[‘samsung’]
+
>>> del cur_price['samsung']
 
>>> cur_price{}
 
>>> cur_price{}
 
</source>
 
</source>
108번째 줄: 108번째 줄:
 
<source lang=python>
 
<source lang=python>
 
>>> cur_price.keys()
 
>>> cur_price.keys()
dict_keys([‘samsung’])
+
dict_keys(['samsung'])
 
>>> cur_price.values()
 
>>> cur_price.values()
 
dict_values([10000])
 
dict_values([10000])
117번째 줄: 117번째 줄:
 
=== len(s) ===
 
=== len(s) ===
 
<source lang=python>
 
<source lang=python>
>>> mystring = “hello world”
+
>>> mystring = "hello world"
 
>>> len(mystring)
 
>>> len(mystring)
 
11
 
11
125번째 줄: 125번째 줄:
 
<source lang=python>
 
<source lang=python>
 
>>> mystring[0]
 
>>> mystring[0]
‘h’
+
'h'
 
</source>
 
</source>
  
131번째 줄: 131번째 줄:
 
<source lang=python>
 
<source lang=python>
 
>>> mystring[0:5]
 
>>> mystring[0:5]
‘hello’
+
'hello'
  
 
>>> mystring[6:]
 
>>> mystring[6:]
‘world’
+
'world'
 
</source>
 
</source>
  
 
=== 문자열.split(S) ===
 
=== 문자열.split(S) ===
 
<source lang=python>
 
<source lang=python>
>>> companies = “yahoo google”
+
>>> companies = "yahoo google"
  
>>> companies.split(‘ ’)
+
>>> companies.split(' ')
[‘yahoo’, ‘google’]
+
['yahoo', 'google']
 
</source>
 
</source>
  
 
=== in ===
 
=== in ===
 
<source lang=python>
 
<source lang=python>
>>> ‘google’ in companies
+
>>> 'google' in companies
 
True
 
True
 
</source>
 
</source>
153번째 줄: 153번째 줄:
 
=== combining ===
 
=== combining ===
 
<source lang=python>
 
<source lang=python>
>>> s1 = “hello”
+
>>> s1 = "hello"
>>> s2 = “world”
+
>>> s2 = "world"
>>> s3 = s1 + ‘ ’ + s2
+
>>> s3 = s1 + ' ' + s2
 
>>> s3
 
>>> s3
“hello world”
+
"hello world"
 
</source>
 
</source>
  
 
=== replace ===
 
=== replace ===
 
<source lang=python>
 
<source lang=python>
>>> a = “yahoo;google”
+
>>> a = "yahoo;google"
>>> new_a = a.replace(;, -)
+
>>> new_a = a.replace(';', '-')
 
>>> new_a
 
>>> new_a
“yahoo-google”
+
"yahoo-google"
 
</source>
 
</source>
  
 
=== index ===
 
=== index ===
 
<source lang=python>
 
<source lang=python>
>>> s = “yahoo google”
+
>>> s = "yahoo google"
>>> s.index(“google”)
+
>>> s.index("google")
 
6
 
6
 
</source>
 
</source>
177번째 줄: 177번째 줄:
 
=== 문자열.find(x) ===
 
=== 문자열.find(x) ===
 
<source lang=python>
 
<source lang=python>
>>> s = “yahoo google”
+
>>> s = "yahoo google"
  
>>> s.find(“google”)
+
>>> s.find("google")
 
6
 
6
 
</source>
 
</source>
185번째 줄: 185번째 줄:
 
=== stripping ===
 
=== stripping ===
 
<source lang=python>
 
<source lang=python>
>>> a = yahoo 
+
>>> a = " yahoo  "
 
>>> new_a = a.strip()
 
>>> new_a = a.strip()
 
>>> new_a
 
>>> new_a
“yahoo”
+
"yahoo"
 
</source>
 
</source>
  
195번째 줄: 195번째 줄:
 
<source lang=python>
 
<source lang=python>
 
if ending_price > 10000:
 
if ending_price > 10000:
     print(“sell”)
+
     print("sell")
 
elif ending_price < 8000:
 
elif ending_price < 8000:
     print(“buy”)
+
     print("buy")
 
else:
 
else:
     print(“hold”)
+
     print("hold")
 
</source>
 
</source>
 
=== 반복 처리  ===
 
=== 반복 처리  ===
216번째 줄: 216번째 줄:
 
>>> for i in range(0, 5):
 
>>> for i in range(0, 5):
 
         if i % 2 == 0:
 
         if i % 2 == 0:
             print(i, end=‘ ’)
+
             print(i, end=' ')
 
0, 2, 4
 
0, 2, 4
>>> buy_list = [‘000660’, ‘039490’]
+
>>> buy_list = ['000660', '039490']
 
>>> for code in buy_list:
 
>>> for code in buy_list:
         print(“buy”, code)
+
         print("buy", code)
  
 
buy 000660
 
buy 000660
 
buy 039490
 
buy 039490
>>> hold_list = {‘naver’: 10, ‘samsung’: 20}
+
>>> hold_list = {'naver': 10, 'samsung': 20}
 
>>> for company, num in hold_list.items():
 
>>> for company, num in hold_list.items():
 
         print(company, num)
 
         print(company, num)
313번째 줄: 313번째 줄:
 
== 클래스 ==
 
== 클래스 ==
 
* 클래스 정의(Class Definitions)
 
* 클래스 정의(Class Definitions)
** Class: 인스턴스의 청사진 , a blueprint for an instance (“instance factories”)
+
** Class: 인스턴스의 청사진 , a blueprint for an instance ("instance factories")
 
** Instance: 클래스의 생성된 객체 , a constructed object of the class  
 
** Instance: 클래스의 생성된 객체 , a constructed object of the class  
 
** Type: 인스턴스가 속한 (타입별)클래스를 가르킴 , indicates the class the instances belong to
 
** Type: 인스턴스가 속한 (타입별)클래스를 가르킴 , indicates the class the instances belong to
 
** Attribute: 모든 객체 값 ,any object value: object.attribute
 
** Attribute: 모든 객체 값 ,any object value: object.attribute
** Method: 클래스에 선언된 호출 가능한 속성 , a “callable attribute” defined in the class
+
** Method: 클래스에 선언된 호출 가능한 속성 , a "callable attribute" defined in the class
  
 
=== 인스턴스 메소드 ===
 
=== 인스턴스 메소드 ===
340번째 줄: 340번째 줄:
 
         print(self.email)
 
         print(self.email)
 
# instantiation and call the method
 
# instantiation and call the method
mem1 = BusinessCard(“Goo”, “goo@gmail.com”)
+
mem1 = BusinessCard("Goo", "goo@gmail.com")
 
mem1.print_info()
 
mem1.print_info()
 
</source>
 
</source>
349번째 줄: 349번째 줄:
 
>>> class Parent:
 
>>> class Parent:
 
         def can_sing(self):
 
         def can_sing(self):
             print(“sing a song”)
+
             print("sing a song")
  
 
>>> father = Parent()
 
>>> father = Parent()
365번째 줄: 365번째 줄:
 
>>> class LuckyChild2(Parent):
 
>>> class LuckyChild2(Parent):
 
       def can_dance(self):
 
       def can_dance(self):
           print(“dance beautifully”)
+
           print("dance beautifully")
  
 
>>> child2 = LuckyChild2()
 
>>> child2 = LuckyChild2()

2020년 4월 10일 (금) 15:01 판

thumb_up 추천메뉴 바로가기


1 데이터 타입[편집]

# int 
>>> a = 3
>>> type(a)
int
  1. float
  2. str
  3. bool
  4. complex
>>> c = 3 + 4j
>>> type(c)
complex

2 데이터 구조체[편집]

2.1 리스트[편집]

2.1.1 생성[편집]

>>> names = [ ]
>>> names = ['a', 'b', 'c', 'd, 'e']

2.1.2 len(s)[편집]

>>> names = ['a', 'b', 'c', 'd, 'e']
>>> len(names)
5

2.1.3 indexing[편집]

>>> names = ['a', 'b', 'c', 'd, 'e']
>>> names[0]
'a'
>>> names[1]
'b'

2.1.4 append[편집]

>>> names = ['a', 'b','c']
>>> names.append('d')
>>> names
['a', 'b', 'c', 'd']

2.1.5 insert[편집]

>>> names = ['a', 'b', 'c']
>>> names.insert(1, 'e')
>>> names
['a', 'e', 'b', 'c']

2.1.6 comprehension[편집]

  • 컴프리핸션: 리스트를 한줄의 코드로 쉽게 만들때 사용
>>> data = [3, 4, 5]
>>> float_data = [float(d) for d in data]

2.2 튜플[편집]

2.2.1 생성[편집]

>>> names = ('a', 'b', 'c', 'd, 'e')

2.2.2 len(s)[편집]

>>> names = ('a', 'b', 'c', 'd, 'e')
>>> len(names)
5

2.2.3 indexing[편집]

>>> names = ('a', 'b', 'c', 'd, 'e')
>>> names[0]
'a'
>>> names[1]
'b'

2.3 딕셔너리[편집]

2.3.1 생성[편집]

>>> cur_price = { }

2.3.2 insert[편집]

>>> cur_price['samsung'] = 10000
>>> cur_price
{'samsung': 10000}

2.3.3 indexing[편집]

>>> cur_price['samsung']
>>> 10000

2.3.4 delete[편집]

>>> del cur_price['samsung']
>>> cur_price{}

2.3.5 key, value[편집]

>>> cur_price.keys()
dict_keys(['samsung'])
>>> cur_price.values()
dict_values([10000])

3 문자열 조작 함수[편집]

3.1 len(s)[편집]

>>> mystring = "hello world"
>>> len(mystring)
11
Python-slicing.jpg

3.2 indexing[편집]

>>> mystring[0]
'h'

3.3 slicing[편집]

>>> mystring[0:5]
'hello'

>>> mystring[6:]
'world'

3.4 문자열.split(S)[편집]

>>> companies = "yahoo google"

>>> companies.split(' ')
['yahoo', 'google']

3.5 in[편집]

>>> 'google' in companies
True

3.6 combining[편집]

>>> s1 = "hello"
>>> s2 = "world"
>>> s3 = s1 + ' ' + s2
>>> s3
"hello world"

3.7 replace[편집]

>>> a = "yahoo;google"
>>> new_a = a.replace(';', '-')
>>> new_a
"yahoo-google"

3.8 index[편집]

>>> s = "yahoo google"
>>> s.index("google")
6

3.9 문자열.find(x)[편집]

>>> s = "yahoo google"

>>> s.find("google")
6

3.10 stripping[편집]

>>> a = "  yahoo  "
>>> new_a = a.strip()
>>> new_a
"yahoo"

4 파이썬 제어문[편집]

4.1 조건별 분기[편집]

if ending_price > 10000:
    print("sell")
elif ending_price < 8000:
    print("buy")
else:
    print("hold")

4.2 반복 처리[편집]

  • Loop – For
>>> for i in range(0, 5):
        print(i)

0
1
2
3
4
>>> for i in range(0, 5):
        if i % 2 == 0:
            print(i, end=' ')
0, 2, 4
>>> buy_list = ['000660', '039490']
>>> for code in buy_list:
        print("buy", code)

buy 000660
buy 039490
>>> hold_list = {'naver': 10, 'samsung': 20}
>>> for company, num in hold_list.items():
        print(company, num)

naver 10
samsung 20

4.3 Loop - While[편집]

>>> i = 0
>>> while i < 5:
        print(i)
        i += 1

0
1
2
3
4
>>> i = 0
>>> while i < 5:
        if i % 2 == 0:
print(i)
        i += 1

0 2 4

5 파이썬 모듈 임포트 방법[편집]

  • 일반적인 import 방식
import os
  • 필요 함수만 import 하는 방식
from os import xxx
  • 모든 함수를 import 하는 방식
from os import *

6 변수[편집]

Python-var.jpg
>>> a = 10
>>> id(a)

7 함수[편집]

>>> def cal_upper_price(price):
        increment = price * 0.3
        upper_price = price + increment
        return upper_price

>>> upper_price = cal_upper_price(10000)
>>> upper_price
13000

8 모듈[편집]

# stock.py
def cal_upper_price(price):
    increment = price * 0.3
    upper_price = price + increment
    return upper_price
  • stock.py 모듈 import
# test.py
import stock
upper_price = stock.cal_upper_price(10000)
  • stock.py 모듈 import
# test.py
from stock import *
upper_price = upper_price(10000)

9 클래스[편집]

  • 클래스 정의(Class Definitions)
    • Class: 인스턴스의 청사진 , a blueprint for an instance ("instance factories")
    • Instance: 클래스의 생성된 객체 , a constructed object of the class
    • Type: 인스턴스가 속한 (타입별)클래스를 가르킴 , indicates the class the instances belong to
    • Attribute: 모든 객체 값 ,any object value: object.attribute
    • Method: 클래스에 선언된 호출 가능한 속성 , a "callable attribute" defined in the class

9.1 인스턴스 메소드[편집]

class Joe:
    def callme(self):
        print("calling 'callme' method with instance")

thisjoe = Joe() #인스턴스 할당
thisjoe.callme() #메소스 호출

9.2 클래스 정의[편집]

class BusinessCard:
    def __init__(self, name, email):
        self.name = name
        self.email = email

    def print_info(self):
        print(self.name)
        print(self.email)
# instantiation and call the method
mem1 = BusinessCard("Goo", "goo@gmail.com")
mem1.print_info()

9.3 클래스 상속[편집]

  • Class – Inheritance
>>> class Parent:
        def can_sing(self):
            print("sing a song")

>>> father = Parent()
>>> father.can_sing()
sing a song


>>> class LuckyChild(Parent):
        pass

>>> child1 = LuckyChild()
>>> child1.can_sing()
sing a song

>>> class LuckyChild2(Parent):
       def can_dance(self):
          print("dance beautifully")

>>> child2 = LuckyChild2()
>>> child2.can_sing()
sing a song
>>> child2.can_dance()
dance beautifully

9.4 클래스 상속2[편집]

  • Class – Inheritance II
>>> class Parent:
        def __init__(self):
            self.money = 10000
        
>>> class Child1(Parent):
        def __init__(self):
            super().__init__()        

>>> class Child2(Parent):
        def __init__(self):
            pass
    
>>> child1 = Child1()
>>> child2 = Child2()
>>> print(child1.money)
10000
>>> print(child2.money)
AttributeError: 'Child2' object has no attribute 'money'