jupyter notebook을 열고 하단의 스크립트를 작성하여 first_script.ipynb로 저장하고 실행한다
print("Output #1: hello python")
Output #1: hello python
x=4
y=5
z=x+y
print("Output #2: four plus five equals {0:d}.".format(z))
Output #2: four plus five equals 9.
a=[1,2,3,4]
b=["first","second","third","fourth"]
c=a+b
print("Output #3: {0},{1},{2}".format(a,b,c))
Output #3: [1, 2, 3, 4],['first', 'second', 'third', 'fourth'],[1, 2, 3, 4, 'first', 'second', 'third', 'fourth']
[ipython 파일 py 파일로 만들기]
해당 .ipynb 파일이 있는 곳에서
# ipython nbconvert --to script first_script.ipynb (jupyter로 바뀌면서 deprecated됨)
또는
# jupyter nbconvert --to script first_script.ipynb
[실행 가능한 py 만들기]
Window에서는
> python first_script.py
리눅스계열 에서는
$ chmod +x first_script.py
$ ./first_script.py
[자료형 - 숫자]
x=9
print("Output #4: {0}".format(x))
print("Output #5: {0}".format(3**4))
print("Output #6: {0}".format(int(8.3)/int(2.7)))
Output #4: 9 Output #5: 81 Output #6: 4.0
print("Output #7: {0:.3f}".format(8.3/2.7))
y=2.5*4.8
print("Output #8: {0:.1f}".format(y))
r=8/float(3)
print("Output #9: {0:.2f}".format(r))
print("Output #10: {0:.4f}".format(8.0/3))
print(type(r))
Output #7: 3.074 Output #8: 12.0 Output #9: 2.67 Output #10: 2.6667 <class 'float'>
from math import exp, log, sqrt
print("Output #11: {0:.4f}".format(exp(3)))
print("Output #12: {0:.2f}".format(log(4)))
print("Output #13: {0:.1f}".format(sqrt(81)))
Output #11: 20.0855 Output #12: 1.39 Output #13: 9.0
[자료형_문자열]
print("Output #14:{0:s}".format('I\'m enjoying learning Python'))
print("Output #15:{0:s}".format('long string long string long string long string\
long string long string long string long string long string '))
print("Output #16:{0:s}".format(''' triple single quotes
for multi-line strings'''));
print("Output #16:{0:s}".format(""" triple single quotes
for multi-line strings"""));
Output #14:I'm enjoying learning Python Output #15:long string long string long string long stringlong string long string long string long string long string Output #16: triple single quotes for multi-line strings Output #16: triple single quotes for multi-line strings
string1 = "This is a"
string2 = "short string"
sentence = string1 + string2
print("Output #18: {0:s}".format(sentence))
print("Output #19: {0:s} {1:s} {2:s}".format("She is","Very"*4,"beautiful."))
m = len(sentence)
print("Output #20: {0:d}".format(m))
Output #18: This is ashort string Output #19: She is VeryVeryVeryVery beautiful. Output #20: 21
#split
string1 = "My deliverable is due in May"
string1_list1 = string1.split()
string1_list2 = string1.split(" ",2)
print("Output #21:{0}".format(string1_list1))
print("Output #22:Frist piece:{0} second piece:{1} third piece{2}".format(string1_list2[0],string1_list2[1],string1_list2[2]))
string2 = "your,deliverable,is,due,in,June"
string2_list = string2.split(',')
print("Output #23: {0}".format(string2_list))
print("Output #24: {0} {1} {2}".format(string2_list[1],string2_list[5],string2_list[-1]))
Output #21:['My', 'deliverable', 'is', 'due', 'in', 'May'] Output #22:Frist piece:My second piece:deliverable third pieceis due in May Output #23: ['your', 'deliverable', 'is', 'due', 'in', 'June'] Output #24: deliverable June June
#join, strip, replace
print("Output #25: {0}".format(' '.join(string2_list)))
string3 = " Remove unwanted characters from this string. \t\t \n"
print("Output #26: {0:s}".format(string3))
string3_lstrip = string3.lstrip()
print("Output #27: {0:s}".format(string3_lstrip))
string3_rstrip = string3.rstrip()
print("Output #28: {0:s}".format(string3_rstrip))
string3_strip = string3.strip()
print("Output #29: {0:s}".format(string3_strip))
string4 = "$$Here's another string __---++"
print("Output #30:{0:s}".format(string4))
string4_strip= string4.strip('$_-+')
print("Output #31:{0:s}".format(string4_strip))
string5 = "Let's replace the spaces"
string5_replace = string5.replace(" ","!@!")
print("Output #32:{0:s}".format(string5_replace))
string5_replace = string5.replace(" ",",")
print("Output #33:{0:s}".format(string5_replace))
Output #25: your deliverable is due in June Output #26: Remove unwanted characters from this string. Output #27: Remove unwanted characters from this string. Output #28: Remove unwanted characters from this string. Output #29: Remove unwanted characters from this string. Output #30:$$Here's another string __---++ Output #31:Here's another string Output #32:Let's!@!replace!@!the!@!spaces Output #32:Let's,replace,the,spaces
#lower, upper, capitalize
string6 = "Here's WHAT Happens WHEN you use lower"
print("Output #34:{0:s}".format(string6.lower()))
print("Output #35:{0:s}".format(string6.upper()))
print("Output #36:{0:s}".format(string6.capitalize()))
string6_list = string6.split()
string6_result = ""
for word in string6_list:
string6_result += word.capitalize() +' '
print("Output #37:{0:s}".format(string6_result))
Output #34:here's what happens when you use lower Output #35:HERE'S WHAT HAPPENS WHEN YOU USE LOWER Output #36:Here's what happens when you use lower Output #37:Here's What Happens When You Use Lower
#정규표현식
from math import exp, log, sqrt
import re
string = "The quick brown fox jumps over the lazy dog"
string_list = string.split()
pattern = re.compile(r"The",re.I)
#r: 원시문자열, re.I는 대소문자 구분 없이..
count= 0
for word in string_list:
if pattern.search(word):
count+=1
print("Output #38:{0:d}".format(count))
Output #38:2
#문자열내 발견된 패턴 출력
string = "The quick brown fox jumps over the lazy dog"
string_list = string.split()
pattern = re.compile(r"(?P<match_word>The)",re.I)
print("Output #39:")
for word in string_list:
if pattern.search(word):
print("{:s}".format(pattern.search(word).group('match_word')))
#match_word는 group명, 그룹에 해당하는 단어 출력
Output #39: The the
#문자열 내 'a'를 'the'로 대체하기
string = "The quick brown fox jumps over the lazy dog"
string_to_find = r"The"
pattern = re.compile(string_to_find,re.I)
print("Output #40:{:s}".format(pattern.sub("a",string)))
#re.sub 함수 the 패턴을 a로 교체
Output #40:a quick brown fox jumps over a lazy dog
'Data > Python' 카테고리의 다른 글
Web Scraper 기본 (0) | 2018.05.18 |
---|---|
Python IDE 개발환경, pip proxy ssl 문제해결 (0) | 2018.05.10 |
자료형 - List (0) | 2018.02.13 |
날짜 다루기 (0) | 2018.02.07 |
NLTK 설치 및 수동 다운로드, 토큰화 테스트 (1) | 2018.01.04 |