파이썬 - 모듈

김현우·2020년 6월 28일
0

모듈 가져오기 - import

import 시키는 2가지 방법

import 모듈 //모듈 내의 변수를 사용하기 위해서는 '모듈.변수'의 형식
from 모듈 import 변수나 함수

import 모듈

import Tkinter
Tkinter.widget = Tkinter.Label(None, text='I love Python!')
Tkinter.widget.pack()

from 모듈 import 변수나 함수

from Tkinter import *
widget = Label(None, text='I love Python!')
widget.pack()

모듈 삭제

del 모듈

다시 불러오기

reload(모듈)

여러가지 모듈 - import

sys 모듈
파이썬 인터프리터를 제어

>>> import sys
>>> sys.ps1                                  # 현재의 프롬프트는?
'>>> '
>>> sys.ps1 = '^^; '                         # 요걸로 바꿔!
^^; print 'hello'
hello
^^; 5 * 3
15
^^;

os 모듈
Windows로 파일과 폴더를 만들고 복사하는 일

>>> import os
>>> os.getcwd()
'C:\\Python27'
>>> os.listdir('C:\Python27')
['DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt',
'python.exe', 'pythonw.exe', 'README.txt', 'tcl', 'Tools',
'w9xpopen.exe']
>>> os.rename('README.txt', 'readme.txt')
>>> os.listdir('C:\Python27')
['DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt',
'python.exe', 'pythonw.exe', 'readme.txt', 'tcl', 'Tools',
'w9xpopen.exe']
>>>

string 모듈
기본적인 문자열 연산 제공

>>> import string
>>> string.capitalize('python')        # 첫 글자를 대문자로
'Python'
>>> string.replace('simple', 'i', 'a') #'simple'의 'i'를 'a'로 바꿈
'sample'
>>> string.split('break into words')   # 문자열을 분리한 리스트 구함
['break', 'into', 'words']

랜덤(random) 모듈

랜덤 문법

>>> import random
>>> random.random()
0.90389642027948769

random() => 0 이상 1 미만의 숫자 중에서 아무 숫자나 하나 뽑아서 돌려줌

셔플 문법

>>> abc = ['a', 'b', 'c', 'd', 'e']
>>> random.shuffle(abc)
>>> abc
['a', 'd', 'e', 'b', 'c']
>>> random.shuffle(abc)
>>> abc
['e', 'd', 'a', 'c', 'b']

shuffle() => 순서형 자료(sequence)를 뒤죽박죽으로 섞어놓는 함수

초이스 문법

>>> abc
['e', 'd', 'a', 'c', 'b']
>>> random.choice(abc)
'a'
>>> random.choice(abc)
'd'

>>> menu = '쫄면', '육계장', '비빔밥'
>>> random.choice(menu)
'쫄면'

choice() => 아무 원소나 하나 뽑아줌

matplotlib

profile
코딩을 잘하는 개발자가 되자!

0개의 댓글