tag:blogger.com,1999:blog-51180010139448053882024-02-08T18:51:09.242+09:00PythonElex의 파이썬 프로그래밍 가이드.Elexhttp://www.blogger.com/profile/04776810249062811247noreply@blogger.comBlogger28125tag:blogger.com,1999:blog-5118001013944805388.post-59405365858990726702017-11-04T16:42:00.003+09:002017-11-04T16:43:44.548+09:00Project Euler - 5<h1 style="flex-basis: auto !important;">최소공배수</h1><div style="flex-basis: auto !important;">1부터 20까지 숫자 모두로 나누어도 몫이 0인 가장 작은 숫자를 구하라.</div><div style="flex-basis: auto !important;"></div><div style="flex-basis: auto !important;"></div><div style="flex-basis: auto !important;"><pre style="background-color: rgb(43, 43, 43); color: rgb(169, 183, 198); font-family: "Source Code Pro"; font-size: 10.5pt; flex-basis: auto !important;"><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;">"""<br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;">Smallest multiple<br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;"><br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;">2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.<br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;"><br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;">What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?<br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;">"""<br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;"><br style="flex-basis: auto !important;"></span><span style="color: rgb(204, 120, 50); flex-basis: auto !important;">if </span>__name__ == <span style="color: rgb(106, 135, 89); flex-basis: auto !important;">'__main__'</span>:<br style="flex-basis: auto !important;"> num = <span style="color: rgb(104, 151, 187); flex-basis: auto !important;">20<br style="flex-basis: auto !important;"></span> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">while True</span>:<br style="flex-basis: auto !important;"> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">for </span>d <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">in </span><span style="color: rgb(136, 136, 198); flex-basis: auto !important;">range</span>(<span style="color: rgb(104, 151, 187); flex-basis: auto !important;">1</span><span style="color: rgb(204, 120, 50); flex-basis: auto !important;">, </span><span style="color: rgb(104, 151, 187); flex-basis: auto !important;">21</span>):<br style="flex-basis: auto !important;"> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">if </span>num % d != <span style="color: rgb(104, 151, 187); flex-basis: auto !important;">0</span>:<br style="flex-basis: auto !important;"> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">break<br style="flex-basis: auto !important;"></span><span style="color: rgb(204, 120, 50); flex-basis: auto !important;"> else</span>:<br style="flex-basis: auto !important;"> <span style="color: rgb(136, 136, 198); flex-basis: auto !important;">print</span>(num)<br style="flex-basis: auto !important;"> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">break<br style="flex-basis: auto !important;"></span> num += <span style="color: rgb(104, 151, 187); flex-basis: auto !important;">20<br style="flex-basis: auto !important;"></span><span style="color: rgb(104, 151, 187); flex-basis: auto !important;"><br style="flex-basis: auto !important;"></span></pre></div><div style="flex-basis: auto !important;"><a href="https://projecteuler.net/problem=5" style="color: rgb(0, 134, 229); flex-basis: auto !important;">https://projecteuler.net/problem=5</a><a href="https://projecteuler.net/problem=5" style="color: rgb(0, 134, 229); flex-basis: auto !important;"></a></div><div style="flex-basis: auto !important;"></div><div style="flex-basis: auto !important;">연산 횟수를 줄이고자, 시작값을 20으로 하고, 증분도 20으로 하였다.</div> Elexhttp://www.blogger.com/profile/04776810249062811247noreply@blogger.com0tag:blogger.com,1999:blog-5118001013944805388.post-59523584964665970142017-11-04T16:42:00.001+09:002017-11-04T16:43:44.555+09:00Project Euler - 4<h1 style="flex-basis: auto !important;">회문 구하기</h1><div style="flex-basis: auto !important;">두 개의 3자리 숫자의 곱으로 만들어지는 가장 큰 회문을 구하라.</div><div style="flex-basis: auto !important;"></div><div style="flex-basis: auto !important;"></div><div style="flex-basis: auto !important;"><pre style="background-color: rgb(43, 43, 43); color: rgb(169, 183, 198); font-family: "Source Code Pro"; font-size: 10.5pt; flex-basis: auto !important;"><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;">"""<br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;">Largest palindrome product<br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;"><br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;">A palindromic number reads the same both ways.<br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;">The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.<br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;"><br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;">Find the largest palindrome made from the product of two 3-digit numbers.<br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;">"""<br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;"><br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;"><br style="flex-basis: auto !important;"></span><span style="color: rgb(204, 120, 50); flex-basis: auto !important;">def </span><span style="color: rgb(255, 198, 109); flex-basis: auto !important;">is_palindrome</span>(n):<br style="flex-basis: auto !important;"> s = <span style="color: rgb(136, 136, 198); flex-basis: auto !important;">str</span>(n)<br style="flex-basis: auto !important;"> s2 = <span style="color: rgb(136, 136, 198); flex-basis: auto !important;">str</span>()<br style="flex-basis: auto !important;"> i = <span style="color: rgb(136, 136, 198); flex-basis: auto !important;">len</span>(s) - <span style="color: rgb(104, 151, 187); flex-basis: auto !important;">1<br style="flex-basis: auto !important;"></span> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">while </span>i >= <span style="color: rgb(104, 151, 187); flex-basis: auto !important;">0</span>:<br style="flex-basis: auto !important;"> s2 += s[i]<br style="flex-basis: auto !important;"> i -= <span style="color: rgb(104, 151, 187); flex-basis: auto !important;">1<br style="flex-basis: auto !important;"></span> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">return </span>s == s2<br style="flex-basis: auto !important;"><br style="flex-basis: auto !important;"><br style="flex-basis: auto !important;"><span style="color: rgb(204, 120, 50); flex-basis: auto !important;">if </span>__name__ == <span style="color: rgb(106, 135, 89); flex-basis: auto !important;">'__main__'</span>:<br style="flex-basis: auto !important;"> maxi = <span style="color: rgb(104, 151, 187); flex-basis: auto !important;">0<br style="flex-basis: auto !important;"></span> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">for </span>a <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">in </span><span style="color: rgb(136, 136, 198); flex-basis: auto !important;">range</span>(<span style="color: rgb(104, 151, 187); flex-basis: auto !important;">100</span><span style="color: rgb(204, 120, 50); flex-basis: auto !important;">, </span><span style="color: rgb(104, 151, 187); flex-basis: auto !important;">1000</span>):<br style="flex-basis: auto !important;"> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">for </span>b <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">in </span><span style="color: rgb(136, 136, 198); flex-basis: auto !important;">range</span>(<span style="color: rgb(104, 151, 187); flex-basis: auto !important;">100</span><span style="color: rgb(204, 120, 50); flex-basis: auto !important;">, </span><span style="color: rgb(104, 151, 187); flex-basis: auto !important;">1000</span>):<br style="flex-basis: auto !important;"> num = a * b<br style="flex-basis: auto !important;"> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">if </span>is_palindrome(num):<br style="flex-basis: auto !important;"> <span style="color: rgb(136, 136, 198); flex-basis: auto !important;">print</span>(num)<br style="flex-basis: auto !important;"> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">if </span>num > maxi:<br style="flex-basis: auto !important;"> maxi = num<br style="flex-basis: auto !important;"> <span style="color: rgb(136, 136, 198); flex-basis: auto !important;">print</span>(<span style="color: rgb(106, 135, 89); flex-basis: auto !important;">"max = "</span><span style="color: rgb(204, 120, 50); flex-basis: auto !important;">, </span>maxi)</pre></div><div style="flex-basis: auto !important;"><a href="https://projecteuler.net/problem=4" style="color: rgb(0, 134, 229); flex-basis: auto !important;">https://projecteuler.net/problem=4</a><a href="https://projecteuler.net/problem=4" style="color: rgb(0, 134, 229); flex-basis: auto !important;"></a></div><div style="flex-basis: auto !important;"></div><div style="flex-basis: auto !important;">회문 여부를 판별하기 위한 함수를 정의하였다. 함수가 입력 받은 숫자를 문자열로 변환 후 순서를 뒤집는다.</div> Elexhttp://www.blogger.com/profile/04776810249062811247noreply@blogger.com1tag:blogger.com,1999:blog-5118001013944805388.post-69333809425567262692017-11-04T16:41:00.003+09:002017-11-04T16:43:44.562+09:00Project Euler - 3<h1 style="flex-basis: auto !important;">최대 소인수</h1><div style="flex-basis: auto !important;">600851475143의 소인수 중에서 최대 값을 구하라.</div><div style="flex-basis: auto !important;"></div><div style="flex-basis: auto !important;"></div><div style="flex-basis: auto !important;"><pre style="background-color: rgb(43, 43, 43); color: rgb(169, 183, 198); font-family: "Source Code Pro"; font-size: 10.5pt; flex-basis: auto !important;"><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;">"""<br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;">Largest prime factor<br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;"><br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;">The prime factors of 13195 are 5, 7, 13 and 29.<br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;"><br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;">What is the largest prime factor of the number 600851475143 ?<br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;">"""<br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;"><br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;"><br style="flex-basis: auto !important;"></span><span style="color: rgb(204, 120, 50); flex-basis: auto !important;">class </span>PrimeNumber:<br style="flex-basis: auto !important;"> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">def </span><span style="color: rgb(178, 0, 178); flex-basis: auto !important;">__init__</span>(<span style="color: rgb(148, 85, 141); flex-basis: auto !important;">self</span>):<br style="flex-basis: auto !important;"> <span style="color: rgb(148, 85, 141); flex-basis: auto !important;">self</span>.current = <span style="color: rgb(104, 151, 187); flex-basis: auto !important;">1<br style="flex-basis: auto !important;"></span> <span style="color: rgb(148, 85, 141); flex-basis: auto !important;">self</span>.num_set = <span style="color: rgb(136, 136, 198); flex-basis: auto !important;">set</span>()<br style="flex-basis: auto !important;"><br style="flex-basis: auto !important;"> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">def </span><span style="color: rgb(178, 0, 178); flex-basis: auto !important;">__iter__</span>(<span style="color: rgb(148, 85, 141); flex-basis: auto !important;">self</span>):<br style="flex-basis: auto !important;"> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">return </span><span style="color: rgb(148, 85, 141); flex-basis: auto !important;">self<br style="flex-basis: auto !important;"></span><span style="color: rgb(148, 85, 141); flex-basis: auto !important;"><br style="flex-basis: auto !important;"></span> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">def </span><span style="color: rgb(178, 0, 178); flex-basis: auto !important;">__next__</span>(<span style="color: rgb(148, 85, 141); flex-basis: auto !important;">self</span>):<br style="flex-basis: auto !important;"> <span style="color: rgb(148, 85, 141); flex-basis: auto !important;">self</span>.current += <span style="color: rgb(104, 151, 187); flex-basis: auto !important;">1<br style="flex-basis: auto !important;"></span> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">while True</span>:<br style="flex-basis: auto !important;"> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">for </span>i <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">in </span><span style="color: rgb(148, 85, 141); flex-basis: auto !important;">self</span>.num_set:<br style="flex-basis: auto !important;"> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">if </span><span style="color: rgb(148, 85, 141); flex-basis: auto !important;">self</span>.current % i == <span style="color: rgb(104, 151, 187); flex-basis: auto !important;">0</span>:<br style="flex-basis: auto !important;"> <span style="color: rgb(148, 85, 141); flex-basis: auto !important;">self</span>.current += <span style="color: rgb(104, 151, 187); flex-basis: auto !important;">1<br style="flex-basis: auto !important;"></span> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">break </span><span style="color: rgb(128, 128, 128); flex-basis: auto !important;"># Break current for loop<br style="flex-basis: auto !important;"></span> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">else</span>:<br style="flex-basis: auto !important;"> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">break </span><span style="color: rgb(128, 128, 128); flex-basis: auto !important;"># Break the while loop and return<br style="flex-basis: auto !important;"></span> <span style="color: rgb(148, 85, 141); flex-basis: auto !important;">self</span>.num_set.add(<span style="color: rgb(148, 85, 141); flex-basis: auto !important;">self</span>.current)<br style="flex-basis: auto !important;"> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">return </span><span style="color: rgb(148, 85, 141); flex-basis: auto !important;">self</span>.current<br style="flex-basis: auto !important;"><br style="flex-basis: auto !important;"><br style="flex-basis: auto !important;"><span style="color: rgb(204, 120, 50); flex-basis: auto !important;">if </span>__name__ == <span style="color: rgb(106, 135, 89); flex-basis: auto !important;">'__main__'</span>:<br style="flex-basis: auto !important;"> number = <span style="color: rgb(104, 151, 187); flex-basis: auto !important;">600851475143<br style="flex-basis: auto !important;"></span> prime_number = PrimeNumber()<br style="flex-basis: auto !important;"> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">for </span>n <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">in </span>prime_number:<br style="flex-basis: auto !important;"> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">if </span>n > number:<br style="flex-basis: auto !important;"> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">break<br style="flex-basis: auto !important;"></span><span style="color: rgb(204, 120, 50); flex-basis: auto !important;"> if </span>number % n == <span style="color: rgb(104, 151, 187); flex-basis: auto !important;">0</span>:<br style="flex-basis: auto !important;"> <span style="color: rgb(136, 136, 198); flex-basis: auto !important;">print</span>(n)</pre></div><div style="flex-basis: auto !important;"><a href="https://projecteuler.net/problem=3" style="color: rgb(0, 134, 229); flex-basis: auto !important;">https://projecteuler.net/problem=3</a><a href="https://projecteuler.net/problem=3" style="color: rgb(0, 134, 229); flex-basis: auto !important;"></a></div><div style="flex-basis: auto !important;"></div><div style="flex-basis: auto !important;">소수를 구하기 위해 Iterator를 구현하였다.</div> Elexhttp://www.blogger.com/profile/04776810249062811247noreply@blogger.com1tag:blogger.com,1999:blog-5118001013944805388.post-40821720610099825462017-11-04T16:41:00.001+09:002017-11-04T16:43:44.542+09:00Project Euler - 2<h1 style="flex-basis: auto !important;">피보나치 수</h1><div style="flex-basis: auto !important;">4백만 미만의 피보나치 수 중에서 짝수인 것들의 합을 구하라.</div><div style="flex-basis: auto !important;"><b style="font-size: 12pt; flex-basis: auto !important;"><b style="flex-basis: auto !important;"></b></b></div><div style="flex-basis: auto !important;"><pre style="background-color: rgb(43, 43, 43); color: rgb(169, 183, 198); font-family: "Source Code Pro"; font-size: 10.5pt; flex-basis: auto !important;"><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;">"""<br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;">Even Fibonacci numbers<br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;"><br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;">Each new term in the Fibonacci sequence is generated by adding the previous two terms.<br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;">By starting with 1 and 2, the first 10 terms will be:<br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;"><br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;">1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...<br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;"><br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;">By considering the terms in the Fibonacci sequence whose values do not exceed four million,<br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;">find the sum of the even-valued terms.<br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;">"""<br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;"><br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;"><br style="flex-basis: auto !important;"></span><span style="color: rgb(204, 120, 50); flex-basis: auto !important;">class </span>Fibonacci:<br style="flex-basis: auto !important;"> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">def </span><span style="color: rgb(178, 0, 178); flex-basis: auto !important;">__init__</span>(<span style="color: rgb(148, 85, 141); flex-basis: auto !important;">self</span>):<br style="flex-basis: auto !important;"> <span style="color: rgb(148, 85, 141); flex-basis: auto !important;">self</span>.a = <span style="color: rgb(104, 151, 187); flex-basis: auto !important;">0<br style="flex-basis: auto !important;"></span> <span style="color: rgb(148, 85, 141); flex-basis: auto !important;">self</span>.b = <span style="color: rgb(104, 151, 187); flex-basis: auto !important;">1<br style="flex-basis: auto !important;"></span><span style="color: rgb(104, 151, 187); flex-basis: auto !important;"><br style="flex-basis: auto !important;"></span> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">def </span><span style="color: rgb(178, 0, 178); flex-basis: auto !important;">__iter__</span>(<span style="color: rgb(148, 85, 141); flex-basis: auto !important;">self</span>):<br style="flex-basis: auto !important;"> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">return </span><span style="color: rgb(148, 85, 141); flex-basis: auto !important;">self<br style="flex-basis: auto !important;"></span><span style="color: rgb(148, 85, 141); flex-basis: auto !important;"><br style="flex-basis: auto !important;"></span> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">def </span><span style="color: rgb(178, 0, 178); flex-basis: auto !important;">__next__</span>(<span style="color: rgb(148, 85, 141); flex-basis: auto !important;">self</span>):<br style="flex-basis: auto !important;"> c = <span style="color: rgb(148, 85, 141); flex-basis: auto !important;">self</span>.a + <span style="color: rgb(148, 85, 141); flex-basis: auto !important;">self</span>.b<br style="flex-basis: auto !important;"> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">if </span><span style="color: rgb(148, 85, 141); flex-basis: auto !important;">self</span>.a > <span style="color: rgb(148, 85, 141); flex-basis: auto !important;">self</span>.b:<br style="flex-basis: auto !important;"> <span style="color: rgb(148, 85, 141); flex-basis: auto !important;">self</span>.b = c<br style="flex-basis: auto !important;"> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">else</span>:<br style="flex-basis: auto !important;"> <span style="color: rgb(148, 85, 141); flex-basis: auto !important;">self</span>.a = c<br style="flex-basis: auto !important;"> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">return </span>c<br style="flex-basis: auto !important;"><br style="flex-basis: auto !important;"><br style="flex-basis: auto !important;"><span style="color: rgb(204, 120, 50); flex-basis: auto !important;">if </span>__name__ == <span style="color: rgb(106, 135, 89); flex-basis: auto !important;">'__main__'</span>:<br style="flex-basis: auto !important;"> s = <span style="color: rgb(104, 151, 187); flex-basis: auto !important;">0<br style="flex-basis: auto !important;"></span> fibonacci = Fibonacci()<br style="flex-basis: auto !important;"> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">for </span>n <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">in </span>fibonacci:<br style="flex-basis: auto !important;"> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">if </span>n >= <span style="color: rgb(104, 151, 187); flex-basis: auto !important;">4000000</span>:<br style="flex-basis: auto !important;"> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">break<br style="flex-basis: auto !important;"></span><span style="color: rgb(204, 120, 50); flex-basis: auto !important;"> else</span>:<br style="flex-basis: auto !important;"> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">if </span>n % <span style="color: rgb(104, 151, 187); flex-basis: auto !important;">2 </span>== <span style="color: rgb(104, 151, 187); flex-basis: auto !important;">0</span>:<br style="flex-basis: auto !important;"> s += n<br style="flex-basis: auto !important;"> <span style="color: rgb(136, 136, 198); flex-basis: auto !important;">print</span>(s)</pre></div><div style="flex-basis: auto !important;"><a href="https://projecteuler.net/problem=2" style="color: rgb(0, 134, 229); flex-basis: auto !important;">https://projecteuler.net/problem=2</a></div><div style="flex-basis: auto !important;"></div><div style="flex-basis: auto !important;">피보나치 수열을 구하기 위해서 Iterator 클래스를 구현하였다.</div> Elexhttp://www.blogger.com/profile/04776810249062811247noreply@blogger.com0tag:blogger.com,1999:blog-5118001013944805388.post-22883139724676703622017-11-04T16:08:00.001+09:002017-11-04T16:09:18.337+09:00Project Euler - 1<div style="flex-basis: auto !important;"><h2 style="color: rgb(117, 117, 117); font-family: Roboto, sans-serif; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; flex-basis: auto !important;">3과 5의 배수</h2><span style="color: rgb(117, 117, 117); font-family: Roboto, sans-serif; font-size: 15px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; float: none; display: inline !important; flex-basis: auto !important;">1000 미만의 자연수 중, 3과 5의 배수의 합을 구하라.</span></div><div style="flex-basis: auto !important;"><span style="color: rgb(33, 150, 243); font-family: Roboto, sans-serif; flex-basis: auto !important;"><span style="background: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; font-size: 15px; flex-basis: auto !important;"><a href="https://projecteuler.net/problem=1" style="color: rgb(0, 134, 229); flex-basis: auto !important;">https://projecteuler.net/problem=1</a></span></span><a href="https://projecteuler.net/problem=1" style="color: rgb(0, 134, 229); flex-basis: auto !important;"></a></div><div style="flex-basis: auto !important;"></div><div style="flex-basis: auto !important;"><pre style="background-color: rgb(43, 43, 43); color: rgb(169, 183, 198); font-family: "Source Code Pro"; font-size: 10.5pt; flex-basis: auto !important;"><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;">"""<br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;">Multiples of 3 and 5<br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;"><br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;">If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9.<br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;">The sum of these multiples is 23.<br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;"><br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;">Find the sum of all the multiples of 3 or 5 below 1000.<br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;">"""<br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;"><br style="flex-basis: auto !important;"></span><span style="color: rgb(98, 151, 85); font-style: italic; flex-basis: auto !important;"><br style="flex-basis: auto !important;"></span><span style="color: rgb(204, 120, 50); flex-basis: auto !important;">if </span>__name__ == <span style="color: rgb(106, 135, 89); flex-basis: auto !important;">'__main__'</span>:<br style="flex-basis: auto !important;"> s = <span style="color: rgb(104, 151, 187); flex-basis: auto !important;">0<br style="flex-basis: auto !important;"></span> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">for </span>i <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">in </span><span style="color: rgb(136, 136, 198); flex-basis: auto !important;">range</span>(<span style="color: rgb(104, 151, 187); flex-basis: auto !important;">1</span><span style="color: rgb(204, 120, 50); flex-basis: auto !important;">, </span><span style="color: rgb(104, 151, 187); flex-basis: auto !important;">1000</span>):<br style="flex-basis: auto !important;"> <span style="color: rgb(204, 120, 50); flex-basis: auto !important;">if </span>i % <span style="color: rgb(104, 151, 187); flex-basis: auto !important;">3 </span>== <span style="color: rgb(104, 151, 187); flex-basis: auto !important;">0 </span><span style="color: rgb(204, 120, 50); flex-basis: auto !important;">or </span>i % <span style="color: rgb(104, 151, 187); flex-basis: auto !important;">5 </span>== <span style="color: rgb(104, 151, 187); flex-basis: auto !important;">0</span>:<br style="flex-basis: auto !important;"> s += i<br style="flex-basis: auto !important;"><br style="flex-basis: auto !important;"> <span style="color: rgb(136, 136, 198); flex-basis: auto !important;">print</span>(s)</pre></div> Elexhttp://www.blogger.com/profile/04776810249062811247noreply@blogger.com1tag:blogger.com,1999:blog-5118001013944805388.post-52972675223638204232014-08-13T07:30:00.000+09:002014-08-13T07:30:00.178+09:00정규표현식<div class="ad" style="border-radius: 5px; border: 1px solid #666; box-shadow: 3px 3px 1px 1px #444; float: right; margin: 0 0 0.76em 0.82em; padding: 3px;">
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 파이선 프로그래밍 블로그 본문 -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-7196581333897022" data-ad-slot="6210625718" style="display: inline-block; height: 250px; width: 300px;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="ennote"><div align="left"><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt">#!/</span><span style="font-size:10pt"><u>usr</u></span><span style="font-size:10pt">/bin/python3</span></font></div><div align="left"><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 정규표현식</span></font></div><div align="left"><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># by</span> <span style="font-size:10pt"><u>Elex</u></span> <span style="font-size:10pt">(http://python.elex.pe.kr/)</span></font></div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">import</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">re</span></font></div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>main</b></span><span style="font-size:10pt">():</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 패턴 컴파일</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> pattern = re.compile(</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"([\.0-9a-z_-]+)@([0-9a-z_-]+)(\.[0-9a-z_-]+){1,2}"</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">, re.IGNORECASE)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> txt =</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"someone's email address is email</i></span><span style="font-size:10pt"><i><u>@email.com</u></i></span> <span style="font-size:10pt"><i>"</i></span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 찾기</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span> <span style="font-size:10pt"><u>match</u></span> <span style="font-size:10pt">= re.search(pattern, txt)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 또는</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> match = re.search(</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"([\.0-9a-zA-Z_-]+)@([0-9a-zA-Z_-]+)(\.[0-9a-zA-Z_-]+){1,2}"</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">, txt)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">if</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">match: </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'{} is valid email address.'</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">.format(match.group()))</span></font></div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 바꾸기</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(re.sub(</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"([\.0-9a-zA-Z_-]+)@([0-9a-zA-Z_-]+)(\.[0-9a-zA-Z_-]+){1,2}"</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"*****"</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">, txt))</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 또는</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">if</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">match:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(txt.replace(match.group(),</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"*****"</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">))</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">if</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">__name__ ==</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"__main__"</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">: main()</span></font></div><div><br/></div><div><br/></div><div>파이썬에서 정규표현식 관련 기능은 re라는 모듈을 통해서 제공된다.</div><div>패턴 문자열을 컴파일한 후에 search() 메서드를 사용할 수도 있고, search() 메서드에 패턴 문자열을 직접 지정해서 사용할 수도 있다. search() 메서드는 일치 결과를 반환하는데 일치하지 않으면 None을 반환한다.</div><div><br/></div><div><br/></div></div> Elexhttp://www.blogger.com/profile/04776810249062811247noreply@blogger.com0tag:blogger.com,1999:blog-5118001013944805388.post-11477247567086602762014-08-12T07:30:00.000+09:002014-08-12T07:30:00.107+09:00문자열<div class="ad" style="border-radius: 5px; border: 1px solid #666; box-shadow: 3px 3px 1px 1px #444; float: right; margin: 0 0 0.76em 0.82em; padding: 3px;">
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 파이선 프로그래밍 블로그 본문 -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-7196581333897022" data-ad-slot="6210625718" style="display: inline-block; height: 250px; width: 300px;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="ennote"><div><span style="color: rgb(192, 192, 192); font-family: &apos;Source Code Pro&apos;; font-size: 10pt;">#!/</span><span style="color: rgb(192, 192, 192); font-family: &apos;Source Code Pro&apos;; font-size: 10pt;"><u>usr</u></span><span style="color: rgb(192, 192, 192); font-family: &apos;Source Code Pro&apos;; font-size: 10pt;">/bin/python3</span></div><div align="left"><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 문자열</span></font></div><div align="left"><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># by</span> <span style="font-size:10pt"><u>Elex</u></span> <span style="font-size:10pt">(http://python.elex.pe.kr/)</span></font></div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>main</b></span><span style="font-size:10pt">():</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"This is a string."</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">.upper())</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># THIS IS A STRING.</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"This is a string."</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">.lower())</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># this is a string.</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"this is a string."</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">.capitalize())</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># This is a string.</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"This Is A String."</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">.swapcase())</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># tHIS iS a sTRING.</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"This is a string."</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">.find(</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"is"</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">))</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 2</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"This is a string."</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">.replace(</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"This"</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"That"</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">))</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># That is a string.</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>" This is a string. "</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">.strip())</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># This is a string.</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>" This is a string. "</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">.strip(</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"\n"</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">))</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># This is a string. </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>" This is a string. "</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">.rstrip())</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># This is a string.</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>" This is a string. "</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">.lstrip())</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># This is a string. </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"This is a string."</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">.isalnum())</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># False</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"This is a string."</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">.isalpha())</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># False</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"This is a string."</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">.isdigit())</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># False</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"This is a string."</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">.isprintable())</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># True</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"This is a string."</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">.split())</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># ['This', 'is', 'a', 'string.']</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"This is a string."</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">.split(</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'i'</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">))</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># ['</span><span style="font-size:10pt"><u>Th</u></span> <span style="font-size:10pt">', 's ', 's a</span> <span style="font-size:10pt"><u>str</u></span><span style="font-size:10pt">', '</span><span style="font-size:10pt"><u>ng</u></span><span style="font-size:10pt">.']</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"-"</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">.join([</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'This'</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'is'</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'a'</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'string.'</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">]))</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># This-is-a-string.</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> a, b =</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">3</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">5</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"a는 {}이고, b는 {}이다."</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">.format(a, b))</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"b는 {1}이고, a는 {0}이다."</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">.format(a, b))</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> d = dict(first =</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">3</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">, second =</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">5</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"퍼스트는 {first}이고, 세컨드는 {second}이다."</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">.format(**d))</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">if</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">__name__ ==</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"__main__"</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">: main()</span></font></div><div><br/></div><div><br/></div><div>문자열을 다룸에 있어서, 파이썬과 다른 프로그래밍 언어와의 가장 큰 차이점 중 하나는 파이썬 언어에서는 문자열 리터럴도 메서드를 연결해서 쓸 수 있다는 것이다.</div><div>위의 소스 코드는 몇 가지 유용한 파이썬 문자열 메서드들을 나열한 것이다.</div><div><br/></div></div>Elexhttp://www.blogger.com/profile/04776810249062811247noreply@blogger.com0tag:blogger.com,1999:blog-5118001013944805388.post-1496466121418894592014-08-11T07:00:00.000+09:002014-08-11T07:00:01.735+09:00예외 처리<div class="ad" style="border-radius: 5px; border: 1px solid #666; box-shadow: 3px 3px 1px 1px #444; float: right; margin: 0 0 0.76em 0.82em; padding: 3px;">
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 파이선 프로그래밍 블로그 본문 -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-7196581333897022" data-ad-slot="6210625718" style="display: inline-block; height: 250px; width: 300px;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="ennote"><div align="left"><div align="left"><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt">#!/</span><span style="font-size:10pt"><u>usr</u></span><span style="font-size:10pt">/bin/python3</span></font></div><div align="left"><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 예외 처리</span></font></div><div align="left"><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># by</span> <span style="font-size:10pt"><u>Elex</u></span> <span style="font-size:10pt">(http://python.elex.pe.kr/)</span></font></div><div align="left"><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>main</b></span><span style="font-size:10pt">():</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">try</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">for</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">line</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">in</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">read_file(</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'someFile.txt'</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">):</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(line)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">except</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">IOError</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">as</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">e:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"입출력 오류"</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">, e)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">except</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">ValueError</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">as</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">e:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"파일 이름 오류"</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">, e)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">except</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"알 수 없는 오류"</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">finally</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">pass</span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>read_file</b></span><span style="font-size:10pt">(filename):</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">if</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">filename.endswith(</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'.</i></span><span style="font-size:10pt"><i><u>txt</u></i></span><span style="font-size:10pt"><i>'</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">):</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">return</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">open(filename).readlines()</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">else</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">raise</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">ValueError(</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'파일 이름은 .</i></span><span style="font-size:10pt"><i><u>txt</u></i></span><span style="font-size:10pt"><i>로 끝나야 함'</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">if</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">__name__ ==</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"__main__"</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">: main()</span></font></div></div><div><br/></div><div><br/></div><div>파이썬에서의 예외 처리는 try-except-finally 구문을 사용한다. 예외를 발생시킬 때에는 raise 키워드를 사용한다.</div><div><br/></div><div><br/></div><div><br/></div></div>Elexhttp://www.blogger.com/profile/04776810249062811247noreply@blogger.com0tag:blogger.com,1999:blog-5118001013944805388.post-83120742786259252012014-08-08T06:30:00.000+09:002014-08-08T06:30:00.515+09:00클래스<div class="ad" style="border-radius: 5px; border: 1px solid #666; box-shadow: 3px 3px 1px 1px #444; float: right; margin: 0 0 0.76em 0.82em; padding: 3px;">
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 파이선 프로그래밍 블로그 본문 -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-7196581333897022" data-ad-slot="6210625718" style="display: inline-block; height: 250px; width: 300px;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="ennote"><div align="left"><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt">#!/</span><span style="font-size:10pt"><u>usr</u></span><span style="font-size:10pt">/bin/python3</span></font></div><div align="left"><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 클래스</span></font></div><div align="left"><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># by</span> <span style="font-size:10pt"><u>Elex</u></span> <span style="font-size:10pt">(http://python.elex.pe.kr/)</span></font></div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">class</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>Dog</b></span><span style="font-size:10pt">:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 생성자</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>__init__</b></span><span style="font-size:10pt">(</span><span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">, name, color=</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'white'</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">):</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span> <span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">._name = name</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span> <span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">._color = color</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 메서드</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>bark</b></span><span style="font-size:10pt">(</span><span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">):</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'{}(이)가 짖는다. "멍멍"'</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">.format(</span><span style="font-size:10pt"><i>self</i></span> <span style="font-size:10pt">._name)) </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>walk</b></span><span style="font-size:10pt">(</span><span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">):</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'{}(이)가 걷는다. "슬금슬금"'</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">.format(</span><span style="font-size:10pt"><i>self</i></span> <span style="font-size:10pt">._name))</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 겟터와 셋터 메서드</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>getName</b></span><span style="font-size:10pt">(</span><span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">):</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">return</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">._name </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>getColor</b></span><span style="font-size:10pt">(</span><span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">):</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">return</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">._color </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>setColor</b></span><span style="font-size:10pt">(</span><span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">, color):</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span> <span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">._color = color</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>main</b></span><span style="font-size:10pt">():</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> dog1 = Dog(</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'땡칠'</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> dog1.bark() </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> dog1.setColor(</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'black'</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(dog1.getColor())</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">if</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">__name__ ==</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"__main__"</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">: main()</span></font></div><div><br/></div><div><br/></div><div>클래스의 인스턴스 생성시에 실행되는 __init__()이라는 생성자 메서드도 있고, 또 인스턴스가 메모리에서 해제될 때 실행되는 __del__()이라는 파괴자 메서드도 있다.</div><div><br/></div><div><br/></div><div><hr/></div><div>파이썬에서는 키워드리스트 매개변수를 사용해서 겟터와 셋터를 좀 더 일반화시킨 형태의 코드가 많이 쓰인다.</div><div align="left"><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt">#!/</span><span style="font-size:10pt"><u>usr</u></span><span style="font-size:10pt">/bin/</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt"><u>python3</u></span></font></div><div align="left"><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 클래스</span></font></div><div align="left"><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># by</span> <span style="font-size:10pt"><u>Elex</u></span> <span style="font-size:10pt">(http://python.elex.pe.kr/)</span></font></div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">class</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>Cat</b></span><span style="font-size:10pt">:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 생성자</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>__init__</b></span><span style="font-size:10pt">(</span><span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">, **kwargs):</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span> <span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">._vars = kwargs</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 메서드</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>getProperty</b></span><span style="font-size:10pt">(</span><span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">, key):</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">return</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">._vars.get(key,</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">None</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">) </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>setProperty</b></span><span style="font-size:10pt">(</span><span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">, key, val):</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span> <span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">._vars[key] = val</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>main</b></span><span style="font-size:10pt">():</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> cat1 = Cat()</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> cat1.setProperty(</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'color'</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'red'</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(cat1.getProperty(</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'color'</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">))</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">if</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">__name__ ==</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"__main__"</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">: main()</span></font></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><hr/></div><div>파이썬에서 클래스를 상속하려면, 클래스 이름 뒤에 괄호 안에 부모 클래스를 적어주면 된다. 부모 클래스의 멤버를 사용하려면 super()를 사용한다.</div><div><br/></div><div align="left"><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt">#!/</span><span style="font-size:10pt"><u>usr</u></span><span style="font-size:10pt">/bin/python3</span></font></div><div align="left"><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 클래스</span></font></div><div align="left"><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># by</span> <span style="font-size:10pt"><u>Elex</u></span> <span style="font-size:10pt">(http://python.elex.pe.kr/)</span></font></div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">class</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>Animal</b></span><span style="font-size:10pt">:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>walk</b></span><span style="font-size:10pt">(</span><span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">):</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'걷는다.'</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>talk</b></span><span style="font-size:10pt">(</span><span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">):</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'말한다.'</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">class</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>Dog</b></span><span style="font-size:10pt">(Animal):</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>walk</b></span><span style="font-size:10pt">(</span><span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">):</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'개가'</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">, end=</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>' '</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> super().walk()</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>balk</b></span><span style="font-size:10pt">(</span><span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">):</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'개가 짖는다.'</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font></div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">class</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>Human</b></span><span style="font-size:10pt">(Animal):</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">pass</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>main</b></span><span style="font-size:10pt">():</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> dog1 = Dog()</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> dog1.walk()</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> human1 = Human()</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> human1.walk()</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">if</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">__name__ ==</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"__main__"</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">: main()</span></font></div><div><br/></div><div><br/></div><div><br/></div><div><hr/></div><div>아래의 소스코드는 range() 함수와 비슷한 역할을 수행한다. 다만, range() 함수는 끝 값을 포함하지 않는 반면, 이 클래스는 끝 값도 포함하도록 작성되었다. 이전 글에서 작성했던 함수 버전과 동일한 기능을 하지만 이번에는 클래스 버전이다. </div><div><br/></div><div align="left"><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt">#!/</span><span style="font-size:10pt"><u>usr</u></span><span style="font-size:10pt">/bin/python3</span></font></div><div align="left"><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 레인지 함수와 유사하지만, 끝 값도 포함한다.</span></font></div><div align="left"><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># by</span> <span style="font-size:10pt"><u>Elex</u></span> <span style="font-size:10pt">(http://python.elex.pe.kr/)</span></font></div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>main</b></span><span style="font-size:10pt">():</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">for</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">i</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">in</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">iRange(</span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">0</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">10</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">2</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">):</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(i, end=</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>' '</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">class</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>iRange</b></span><span style="font-size:10pt">():</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>__init__</b></span><span style="font-size:10pt">(</span><span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">, *args): </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 매개변수의 갯수 따라 start, stop, step 변수에 값을 할당한다.</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> lengthOfArgs = len(args)</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 매개변수의 길이</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">if</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">lengthOfArgs <</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">1</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">raise</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">TypeError(</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'매개변수가 하나도 없네'</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">elif</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">lengthOfArgs ==</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">1</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">:</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 매개변수가 하나인 경우엔,</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span> <span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">.start =</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">0</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span> <span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">.stop = args[</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">0</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">]</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span> <span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">.step =</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">1</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">elif</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">lengthOfArgs ==</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">2</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">:</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 매개변수가 둘인 때에는,</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> (</span> <span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">.start,</span> <span style="font-size:10pt"><i>self</i></span> <span style="font-size:10pt">.stop) = args</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span> <span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">.step =</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">1</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">elif</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">lengthOfArgs ==</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">3</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">:</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 매개변수가 셋인 경우에는,</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> (</span> <span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">.start,</span> <span style="font-size:10pt"><i>self</i></span> <span style="font-size:10pt">.stop,</span> <span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">.step) = args</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">else</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">raise</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">TypeError(</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'매개변수가 {}개라니. 너무 많네'</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">.format(lengthOfArgs))</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>__iter__</b></span><span style="font-size:10pt">(</span><span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">):</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 값을 증감시키면서 하나씩 반환한다.</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> i =</span> <span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">.start</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">while</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">i <=</span> <span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">.stop:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">yield</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">i</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> i +=</span> <span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">.step</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">if</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">__name__ ==</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"__main__"</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">: main()</span></font></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div></div>Elexhttp://www.blogger.com/profile/04776810249062811247noreply@blogger.com0tag:blogger.com,1999:blog-5118001013944805388.post-82727878920300371512014-08-07T06:30:00.000+09:002014-08-07T06:30:00.287+09:00함수<div class="ad" style="border-radius: 5px; border: 1px solid #666; box-shadow: 3px 3px 1px 1px #444; float: right; margin: 0 0 0.76em 0.82em; padding: 3px;">
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 파이선 프로그래밍 블로그 본문 -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-7196581333897022" data-ad-slot="6210625718" style="display: inline-block; height: 250px; width: 300px;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="ennote"><div>파이썬에서 사용자 정의 함수는 def라는 키워드로 시작한다. 값을 반환하려면 return 키워드를 사용하면 된다는 점은 다른 프로그래밍 언어들과 마찬가지이다.</div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>myFunc0</b></span><span style="font-size:10pt">():</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">return</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'안녕'</i></span></font></div><div><br/></div><div><br/></div><div><br/></div><hr/><div>파이썬의 함수는 사용하기 전에 정의되어 있어야 한다. 따라서, 다음의 코드는 잘못된 코딩이다.</div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"><u>myFunc1</u></span><span style="font-size:10pt">()</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>myFunc1</b></span><span style="font-size:10pt">():</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'나는야 함수라네~'</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font></div><div><br/></div><div><br/></div><div><br/></div><div><hr/></div><div>일단 함수의 이름만 선언 해두고 함수의 바디 부분은 아직 구현하지 않았다면 어떻겠는가. 다른 프로그래밍 언어에서는 void myFunc(){ } 처럼 블록 안에 아무것도 안쓰면 그만이다. 그러나, 브레이스{} 통한 블록 구조를 배제해버린 파이썬의 경우에는 구문 해석 과정에서 오류가 발생한다. 그래서, 파이썬에는 pass라는 키워드가 등장하게 되었다. 즉, pass는 '아무 것도 안함'이라는 의미이다. pass 키워드는 if-else 구문에서도 처리할 것이 없을 때에 종종 사용된다.</div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>myFunc2</b></span><span style="font-size:10pt">():</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">pass</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div><br/></div><div><hr/></div><div>기본적으로, 파이썬에서 함수의 매개변수는 다른 프로그래밍언어들에서와 유사하다. 즉, 함수 이름 뒤에 괄호 사이에 매개변수를 나열해 주면 된다.</div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>myFunc3</b></span><span style="font-size:10pt">(param1):</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'나는야 함수라네~'</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">, param1)</span></font></div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>myFunc4</b></span><span style="font-size:10pt">(param1, param2):</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'나는야 함수라네~'</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">, param1, param2)</span></font></div><div><br/></div><div><br/></div><div>함수의 매개변수가 생략된 경우에 사용될 기본값을 지정해줄 수도 있다. 다음과 같은 함수의 경우 myFunc5(1) 또는 myFunc5(1,5)라고 호출할 수 있고, 두번째 매개변수가 생략된 경우 param2는 3이 된다. </div><div>주의해야 할 것은, 기본값을 가진 매개변수는 항상 뒤쪽에 써야 한다는 것이다. 논리적으로 생각해 보면 당연하다, 안그러면 파이썬 인터프리터가 헷갈릴테니까.</div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>myFunc5</b></span><span style="font-size:10pt">(param1, param2=</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">3</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">):</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'나는야 함수라네~'</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">, param1, param2)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div><br/></div><div><hr/></div><div>임의의 갯수를 갖는 매개변수도 있다. *는 C언어의 포인터가 아니다, 두려워하지 말자. 매개변수명 앞에 *를 붙이면 되는데, 이 매개변수는 튜플로 전달된다. 임의 갯수의 매개변수가 항상 뒤쪽에 와야 한다는 점에 주의하기 바란다.</div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>myFunc6</b></span><span style="font-size:10pt">(param1, param2, *args):</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">for</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">i</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">in</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">args:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(i)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(param1)</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 0</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(param2)</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 1</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(type(args), args)</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># <class 'tuple'> (2, 3, 4)</span></font></div><div align="left"><span style="font-size: 10pt; font-family: &apos;Source Code Pro&apos;;"> </span></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt">myFunc6(</span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">0</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">1</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">2</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">3</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">4</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div><br/></div><div><br/></div><div>딕셔너리 타입의 임의 매개변수도 가능한데, 이때는 매개변수명 앞에 **를 붙여준다. 위의 경우와 마찬가지로, 임의 갯수의 매개변수가 항상 뒤쪽에 와야 한다</div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b><u>myFunc7</u></b></span><span style="font-size:10pt">(**kwargs):</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">for</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">i</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">in</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">kwargs:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(i,</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'='</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">, kwargs[i])</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(type(kwargs), kwargs)</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># <class '</span><span style="font-size:10pt"><u>dict</u></span><span style="font-size:10pt">'> {'three': 3, 'two': 2, 'one': 1}</span></font></div><div align="left"><span style="font-size: 10pt; font-family: &apos;Source Code Pro&apos;;"> </span><span style="font-size: 10pt; font-family: &apos;Source Code Pro&apos;;"> </span></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt">myFunc7(one=</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">1</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">, two=</span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">2</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">, three=</span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">3</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font></div><div><br/></div><div><span style="font-size: 10pt; font-family: &apos;Source Code Pro&apos;;"> </span></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>myFunc8</b></span><span style="font-size:10pt">(param1,</span> <span style="font-size:10pt"><u>param2</u></span><span style="font-size:10pt">, *args, **kwargs):</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">pass</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div><br/></div><div><br/></div><div><hr/></div><div>아래의 소스코드는 range() 함수와 비슷한 역할을 수행한다. 다만, range() 함수는 끝 값을 포함하지 않는 반면, 이 함수는 끝 값도 포함하도록 작성되었다.</div><div><br/></div><div align="left"><div align="left"><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt">#!/</span><span style="font-size:10pt"><u>usr</u></span><span style="font-size:10pt">/bin/python3</span></font></div><div align="left"><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 레인지 함수와 유사하지만, 끝 값도 포함한다.</span></font></div><div align="left"><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># by</span> <span style="font-size:10pt"><u>Elex</u></span> <span style="font-size:10pt">(http://python.elex.pe.kr/)</span></font></div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>main</b></span><span style="font-size:10pt">():</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">for</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">i</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">in</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">iRange(</span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">0</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">10</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">2</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">):</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(i, end=</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>' '</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>iRange</b></span><span style="font-size:10pt">(*args):</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 매개변수의 갯수 따라 start, stop, step 변수에 값을 할당한다.</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> lengthOfArgs = len(args)</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 매개변수의 길이</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">if</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">lengthOfArgs <</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">1</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">raise</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">TypeError(</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'매개변수가 하나도 없네'</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">elif</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">lengthOfArgs ==</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">1</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">:</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 매개변수가 하나인 경우엔,</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> start =</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">0</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> stop = args[</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">0</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">]</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> step =</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">1</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">elif</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">lengthOfArgs ==</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">2</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">:</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 매개변수가 둘인 때에는,</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> (start, stop) = args</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> step =</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">1</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">elif</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">lengthOfArgs ==</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">3</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">:</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 매개변수가 셋인 경우에는,</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> (start, stop, step) = args</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">else</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">raise</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">TypeError(</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'매개변수가 {}개라니. 너무 많네'</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">.format(lengthOfArgs))</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 값을 증감시키면서 하나씩 반환한다.</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> i = start</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">while</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">i <= stop:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">yield</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">i</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> i += step</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">if</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">__name__ ==</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"__main__"</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">: main()</span></font></div></div><div><br/></div><div>yield 키워드는 return과 유사하다. 즉, 값을 반환한다. 하지만, 함수가 종결되지는 않고 계속 진행된다는 차이점이 있다.</div><div>raise 키워드는 예외를 발생시키는 역할을 한다. 개인적으로는 예외를 발생(raise)시키는 것보다는 던지는(throw) 것이 더 어감이 좋아 보이긴하다만...</div><div><br/></div></div>Elexhttp://www.blogger.com/profile/04776810249062811247noreply@blogger.com0tag:blogger.com,1999:blog-5118001013944805388.post-57649449504435395442014-08-06T06:30:00.000+09:002014-08-06T06:30:01.308+09:00연산자<div class="ad" style="border-radius: 5px; border: 1px solid #666; box-shadow: 3px 3px 1px 1px #444; float: right; margin: 0 0 0.76em 0.82em; padding: 3px;">
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 파이선 프로그래밍 블로그 본문 -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-7196581333897022" data-ad-slot="6210625718" style="display: inline-block; height: 250px; width: 300px;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="ennote"><b>산술연산자</b><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">7 </span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">+ </span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">2</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 9</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">7 </span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">- </span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">2</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 5</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">7 </span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">* </span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">2</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 14</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">7 </span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">/ </span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">2</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 3.5</span></font></div><div align="left"><div><font face="Source Code Pro" size="2"><span style="font-size: 10pt;"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size: 10pt;">print</span></font><font face="Source Code Pro" size="2"><span style="font-size: 10pt;">(</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size: 10pt;">7</span></font><font face="Source Code Pro" size="2"> <span style="font-size: 10pt;">** </span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size: 10pt;">2</span></font> <font face="Source Code Pro" size="2"><span style="font-size: 10pt;">) </span></font><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size: 10pt;"># 49</span></font></div></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">7 </span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">// </span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">2</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 3</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">7 </span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">% </span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">2</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 1 </span></font></div><div><br/></div><div>위에서부터 순서대로 더하기, 빼기, 곱하기, 나누기, 제곱, 나눗셈의 몫, 나눗셈의 나머지.</div><div><br/></div><div><br/></div><div>덧으로, 나눗셈을 연산하는 divmod()라는 함수도 있는데,</div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size: 10pt;"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(divmod(</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">5</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">3</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">))</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># (1, 2)</span></font></div><div><br/></div><div>divmod()는 몫과 나머지를 튜플로 반환한다.</div><div><br/></div><div><br/></div><div><br/></div><hr/><div><b>할당연산자</b></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font><font face="Source Code Pro" size="2"><span style="font-size: 10pt;">number = </span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size: 10pt;">3</span></font></div><div align="left"><span style="font-size: 13px;"> </span><span style="font-size: 13px;"> </span><font face="Source Code Pro" size="2"><span style="font-size:10pt">number +=</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">3</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> number -=</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">3</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> number *=</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">3</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> number /=</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">3</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size: 10pt;"> number **= </span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size: 10pt;">3</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> number //=</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">3</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> number %=</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">3</span></font></div><div><br/></div><div><br/></div><div><hr/><b>비트 연산자</b></div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b><u>binary</u></b></span><span style="font-size:10pt">(n):</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">return</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"{:08b}"</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">.format(n)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>main</b></span><span style="font-size:10pt">():</span></font><span style="font-size: 10pt; font-family: &apos;Source Code Pro&apos;;"> </span></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> a, b =</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">0x55</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">0xaa</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(binary(a))</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 01010101</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(binary(b))</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 10101010</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><div><span style="font-size: 13px;"> </span><span style="font-size: 13px;"> </span><span style="font-size: 13px;"> </span><span style="font-size: 13px;"> </span><span style="color: rgb(192, 192, 192); font-size: 13px;"># 비트 연산자</span></div></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(binary(a & b))</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 00000000</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(binary(a | b))</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 11111111</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(binary(a ^ b))</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 11111111</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(binary(a ^</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">0x00</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">))</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 01010101</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(binary(a ^</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">0xff</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">))</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 10101010</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(binary(a <<</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">4</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">))</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 10101010000</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(binary(a >></span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">4</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">))</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 00000101</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(binary(~ a))</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># -1010110</span></font></div><div><br/></div><div>비트 연산자에는 AND, OR, XOR, 왼쪽 쉬프트, 오른쪽 쉬프트, 그리고 보수 연산자가 있다.</div><div><br/></div><div><br/></div><div><br/></div><hr/><div><b>비교 연산자</b></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> a, b =</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">5</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">3</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(a == b)</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># False</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(a != b)</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># True</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(a > b)</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># True</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(a >= b)</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># True</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(a < b)</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># False</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(a <= b)</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># False</span></font></div><div align="left"><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(a</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">is</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">b)</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># False</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(a</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">is</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">not</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">b)</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># True</span></font></div><div><br/></div><div>== 연산자는 값을 비교하는 것이고, is 연산자는 동일한 ID인지를 판별하는 것이다. </div><div>예전에는 !=과 같은 의미의 <> 연산자도 있었는데, 최근 버전에서는 완전히 사라졌나보다.</div><div><br/></div><div><br/></div><hr/><div><b>논리 연산자</b></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">True</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">and</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">False</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># False</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">True</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">or</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">False</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># True</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">not</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">True</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># False</span></font></div><div><br/></div><div>대부분의 다른 프로그래밍 언어들에서는 &&, ||, ! 과 같은 기호를 사용하지만, 파이썬에서는 and, or, not을 논리 연산자로 사용하고 있다.</div><div><br/></div><div><br/></div><div><br/></div><hr/><div><b>범위 연산자</b></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> list1 = list(range(</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">100</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">)) </span></font><span style="color: rgb(192, 192, 192); font-size: 13px;"># 0 ~ 99</span></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(list1[</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">2</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">])</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 2</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(list1[</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">12</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">:</span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">48</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">])</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 12 ~ 47</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(list1[</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">12</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">:</span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">48</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">:</span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">2</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">])</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 12, 14, 16, 18, ~ , 44, 46</span></font></div><div><br/></div><div>[] 연산자는 타 프로그래밍 언어에서 배열의 요소를 사용하는 것과 같다고 보면 된다.</div><div>[:] 연산자는 리스트의 일부 구간을 반환하는데, 앞 부분은 시작 인덱스, 뒷부분은 끝 인덱스이다. 주의해야 할 것은, 끝나는 인덱스는 포함되지 않는다는 것이다.</div><div>[::] 연산자는 위와 마찬가지로 리스트의 일부 구간을 반환하지만, 추가로 스텝을 지정할 수 있다.</div><div><br/></div><div><br/></div><div><br/></div><hr/><div><b>멤버 연산자</b></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> list1 = [</span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">0</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">1</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">2</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">3</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">4</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">5</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">]</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">3</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">in</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">list1)</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># True</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">7</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">not</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">in</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">list1)</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># True</span></font></div><div><br/></div><div>in 연산자는 해당 값이 리스트에 포함되어 있으면 참이다.</div><div><br/></div><div><br/></div><div><br/></div><hr/><div><b>연산자의 우선 순위</b></div><div>연산자의 우선 순위는 프로그래밍 연산에서 매우 중요한 주제이기는 하지만, 여기에 일일이 나열한다고 외울 것도 아니고...</div><div>헷갈리면 그 때 그 때 찾아보면 되고, 그래도 헷갈리면 괄호를 남발하면 되고.</div><div><br/></div><div><br/></div><div><br/></div><div><hr/></div><div><b>증감 연산자</b></div><div>a++ 이라거나 --a, a--, --a 등등 이런거 파이썬에는 없다.</div><div><br/></div></div> Elexhttp://www.blogger.com/profile/04776810249062811247noreply@blogger.com0tag:blogger.com,1999:blog-5118001013944805388.post-26914120483876562922014-08-05T06:30:00.000+09:002014-08-05T06:30:00.687+09:00반복문<div class="ad" style="border-radius: 5px; border: 1px solid #666; box-shadow: 3px 3px 1px 1px #444; float: right; margin: 0 0 0.76em 0.82em; padding: 3px;">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 파이선 프로그래밍 블로그 본문 -->
<ins class="adsbygoogle"
style="display:inline-block;width:300px;height:250px"
data-ad-client="ca-pub-7196581333897022"
data-ad-slot="6210625718"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="ennote"> while구문은 다른 프로그래밍 언어와 별반 다를 것이 없다. <div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> a, b =</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">0</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">1</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">while</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">b<</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">50</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(b, end=</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>' '</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> a, b = b, a+b </span></font></div><div><br/></div><div>위 코드는 피보나치 수열을 출력한다.</div><div><br/></div><div><br/></div><div>특이하게도, while-else 구문도 존재하는데, while 뒤의 조건이 False가 되면 else 문으로 분기한다.</div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> a =</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">0</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">while</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">a<</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">10</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">(a)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> a +=</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">1</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">else</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"끝"</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div><br/></div><div>do-while 문은 없고, while 뒤의 조건문이 True이면 무한루프가 된다.</div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> a =</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">0</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">while</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">True</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">(a)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> a +=</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">1</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">if</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">a>=</span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">10</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">:</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">break</span></font></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><hr/></div><div>또 다른 하나의 반복 구문은 for-in인데, 이것은 다른 프로그래밍 언어의 for-each 혹은 for : 구문 등과 유사하다.</div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">for</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">item</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">in</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">[</span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">1</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">2</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">3</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">4</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">5</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">]:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(item)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div>for 다음에는 변수를, in 다음에는 리스트, 튜플, 딕셔너리 등의 컨테이너를 기술한다. 물론, 문자열도 쓸 수 있다.</div><div align="left"><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">for</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">char</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">in</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"가나다라마바사"</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(char)</span></font></div></div><div><br/></div><div><br/></div><div><br/></div><div>그런데, 파이썬에는 for만을 사용하는 전통적인 for(;;)구문은 없다.</div><div><div>만일, 증감하는 정수값이 필요한 경우에는 in 뒤에 range() 함수를 사용할 수 있는데,</div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size: 10pt;"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size: 10pt;"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size: 10pt;">for</span></font> <font face="Source Code Pro" size="2"><span style="font-size: 10pt;">i</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size: 10pt;">in</span></font> <font face="Source Code Pro" size="2"><span style="font-size: 10pt;">range(</span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size: 10pt;">5</span></font> <font face="Source Code Pro" size="2"><span style="font-size: 10pt;">) :</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size: 10pt;"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size: 10pt;">print</span></font><font face="Source Code Pro" size="2"><span style="font-size: 10pt;">(i, end=</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size: 10pt;"><i>' '</i></span></font><font face="Source Code Pro" size="2"><span style="font-size: 10pt;">)</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size: 10pt;"># 0 1 2 3 4</span></font></div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size: 10pt;"><br/></span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size: 10pt;"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size: 10pt;">for</span></font> <font face="Source Code Pro" size="2"><span style="font-size: 10pt;">i</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size: 10pt;">in</span></font> <font face="Source Code Pro" size="2"><span style="font-size: 10pt;">range(</span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size: 10pt;">1</span></font> <font face="Source Code Pro" size="2"><span style="font-size: 10pt;">,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size: 10pt;">5</span></font><font face="Source Code Pro" size="2"><span style="font-size: 10pt;">) :</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size: 10pt;"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size: 10pt;">print</span></font><font face="Source Code Pro" size="2"><span style="font-size: 10pt;">(i, end=</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size: 10pt;"><i>' '</i></span></font><font face="Source Code Pro" size="2"><span style="font-size: 10pt;">)</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size: 10pt;"># 1 2 3 4 </span></font></div><div><br/></div><br/></div><div>또한, enumerate() 함수를 사용할 수도 있다.</div><div align="left"><div align="left"><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">for</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">i, char</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">in</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">enumerate(</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"가나다라마바사"</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">) :</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">if</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">char==</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"다"</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"{}번째 글자!"</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">.format(i))</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 2번째 글자!</span></font></div></div></div><div><br/></div><div>enumerate() 함수의 시작값은 0부터인데, 함수의 두번째 매개변수를 통해서 이 값을 변경할 수도 있다.</div><div><br/></div><div><br/></div><div><br/></div><div><hr/></div><div>break와 continue 같은 분기문은 다른 프로그래밍 언어와 마찬가지의 규칙을 따른다.</div><div><br/></div><div><br/></div></div>Elexhttp://www.blogger.com/profile/04776810249062811247noreply@blogger.com1tag:blogger.com,1999:blog-5118001013944805388.post-8434323537578185642014-08-04T06:00:00.000+09:002014-08-04T06:00:00.087+09:00조건문<div class="ad" style="border-radius: 5px; border: 1px solid #666; box-shadow: 3px 3px 1px 1px #444; float: right; margin: 0 0 0.76em 0.82em; padding: 3px;">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 파이선 프로그래밍 블로그 본문 -->
<ins class="adsbygoogle"
style="display:inline-block;width:300px;height:250px"
data-ad-client="ca-pub-7196581333897022"
data-ad-slot="6210625718"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="ennote"><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> a, b =</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">0</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">1</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># if</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">if</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">a>b:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"a가 더 크다."</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt">#if - else</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">if</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">a>b:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"a가 더 크다."</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">else</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"a가 더 크지 않다."</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># if -</span> <span style="font-size:10pt"><u>elif</u></span> <span style="font-size:10pt">- else</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">if</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">a>b:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"a가 더 크다."</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">elif</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">a<b:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"a가 더 작다."</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">else</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"a와 b가 같다."</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">) </span></font></div><div><br/></div><div>파이썬에서의 if문은 브레이스 {}를 사용하는 블록 구조 대신 들여 쓰기를 사용한다는 것과 elseif 대신 elif를 사용한다는 점 이외에는 타 프로그래밍 언어와 큰 차이점이 없다.</div><div><br/></div><div><br/></div><div><br/></div><div><hr/></div><div>그러나, 파이썬에 switch - case 문은 없다. 이럴 수가! 꼭 if-else구문만으로도 얼마든지 구현 가능하다지만 그래도 switc구문이 없으니 뭔가 섭섭하다. 대신 다음과 같은 접근 방식을 사용할 수 있다. </div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span> <span style="font-size:10pt"><u>choices</u></span> <span style="font-size:10pt">= dict(</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> one =</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">1</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">, two =</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">2</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">, three =</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">3</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">, four =</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">4</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> )</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> val =</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'three'</i></span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(choices[val])</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div>위 코드는 딕셔너리에 정의된 값이 아닌 경우 오류가 발생하는데, 이를 조금 더 다듬어 보자면,</div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> choices = dict(</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> one =</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">1</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">, two =</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">2</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">, three =</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">3</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">, four =</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">4</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> )</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> val =</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'five'</i></span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(choices.get(val,</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'unknown'</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">))</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div><br/></div><div><br/></div><div><hr/></div><div>그리고 또, 삼항 조건 연산자(?:)도 없는 대신 if-else 문을 변칙적으로 사용한다.</div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> a, b =</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">0</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">1</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 통상적인 if-else문</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">if</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">a>b:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> result =</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"a가 더 크다."</i></span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">else</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> result =</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"a가 더 크지 않다."</i></span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">(result)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 삼항 연산자를 대신하는 변칙적인 if-else문</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> result =</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"a가 더 크다."</i></span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">if</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">a>b</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">else</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"a가 더 크지 않다."</i></span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">(result)</span></font></div><div><br/></div><div>위 코드에 등장하는 두 종류의 조건문은 그 의미가 서로 같다.</div><div><br/></div><div><br/></div></div>Elexhttp://www.blogger.com/profile/04776810249062811247noreply@blogger.com0tag:blogger.com,1999:blog-5118001013944805388.post-81009009494399239642014-08-01T09:30:00.000+09:002014-08-01T09:30:00.194+09:00자료형 - 참과 거짓<div class="ad" style="border-radius: 5px; border: 1px solid #666; box-shadow: 3px 3px 1px 1px #444; float: right; margin: 0 0 0.76em 0.82em; padding: 3px;">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 파이선 프로그래밍 블로그 본문 -->
<ins class="adsbygoogle"
style="display:inline-block;width:300px;height:250px"
data-ad-client="ca-pub-7196581333897022"
data-ad-slot="6210625718"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="ennote"><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> a =</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">True</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(type(a), a)</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># <class '</span><span style="font-size:10pt"><u>bool</u></span><span style="font-size:10pt">'> True</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> a =</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">False</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(type(a), a)</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># <class '</span><span style="font-size:10pt"><u>bool</u></span><span style="font-size:10pt">'> False</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div>True는 참이고 False는 거짓이다.</div><div><br/></div><div><br/></div><div align="left"><hr/><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> a = [</span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">1</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">2</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">]</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> b = [</span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">1</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">2</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">]</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(a == b)</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># True</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(a</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">is</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">b)</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt">#</span> <span style="font-size:10pt; background:#ffff96">False</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div></div><div><br/></div><div>동등 비교 연산자 == 는 값을 비교하는 것이고, is 연산자는 변수의 ID값을 비교하는 것이다.</div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div></div>Elexhttp://www.blogger.com/profile/04776810249062811247noreply@blogger.com0tag:blogger.com,1999:blog-5118001013944805388.post-76988254105418733052014-07-31T09:00:00.000+09:002014-07-31T09:00:02.642+09:00자료형 - 리스트와 튜플 그리고 딕셔너리<div class="ad" style="border-radius: 5px; border: 1px solid #666; box-shadow: 3px 3px 1px 1px #444; float: right; margin: 0 0 0.76em 0.82em; padding: 3px;">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 파이선 프로그래밍 블로그 본문 -->
<ins class="adsbygoogle"
style="display:inline-block;width:300px;height:250px"
data-ad-client="ca-pub-7196581333897022"
data-ad-slot="6210625718"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="ennote"><div>파이썬 언어의 리스트와 튜플은 다른 프로그래밍 언어에 존재하는 배열(array) 등과 유사한 역할을 한다.</div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size: 10pt;"> </span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">tuple1 = (</span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">1</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">2</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">3</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(type(tuple1), tuple1)</span></font></div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size: 10pt;"> </span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">list1 = [</span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">1</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">2</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">3</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">]</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(type(list1), list1)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size: 10pt;"> </span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">list1.append(</span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">5</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(list1)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size: 10pt;"> </span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">list1.insert(</span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">0</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">0</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(list1)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div>리스트와 튜플은 모두 1차원 자료 구조로써 배열과 비슷하다. 이 둘의 차이점은 값을 할당할 때 리스트는 []를 사용하고 튜플은 ()를 사용한다는 것 외에도, 리스트는 값을 삽입, 수정, 삭제 등 변경할 수 있는 mutable 객체인 반면, 튜플은 그 값을 변경할 수 없는 immutable 객체라는 것이다. 너무나도 당연하겠지만, 일반적으로 속도는 튜플쪽이 조금 더 빠르다고 한다.<span style="font-size: 10pt; font-family: &amp;amp;amp;apos;Source Code Pro&amp;amp;amp;apos;;"> </span></div><div><span style="font-size: 10pt; font-family: &amp;amp;amp;apos;Source Code Pro&amp;amp;amp;apos;;"> </span></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span> <span style="font-size:10pt"><u>list2</u></span> <span style="font-size:10pt">= [</span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">1</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">2</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">, [</span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">4</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">5</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">]]</span></font><span style="font-size: 10pt; font-family: &amp;amp;amp;apos;Source Code Pro&amp;amp;amp;apos;;"> </span></div><div>리스트 안에 리스트를 넣어서 다차원 리스트를 만드는 것도 가능하다.</div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><hr/><div>파이썬 언어의 딕셔너리는 타 언어의 연관배열 혹은 해쉬와 유사하다. 즉, 키와 값의 쌍을 가지며, 순서에 무관하다. 값을 변경할 수 있는 mutable이다.</div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size: 10pt;"> </span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">dict1 = {</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'one'</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">:</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">1</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'two'</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">:</span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">2</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'three'</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">:</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">3</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'four'</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">:</span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">4</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">}</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(type(dict1), dict1)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size: 10pt;"> </span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">dict2 = dict(</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> one=</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">1</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">, two=</span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">2</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">, three=</span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">3</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">, four=</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">4</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> )</span></font></div><div align="left"><font face="Source Code Pro"><br/></font></div><div>위 코드의 dict2에서와 같이 생성자를 통해서 할당하는 것이 좀 더 편리하다.</div><div><br/></div><div><span style="font-size: 10pt; font-family: &amp;amp;amp;apos;Source Code Pro&amp;amp;amp;apos;;"> </span></div><div align="left"><div align="left"><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><hr/><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size: 10pt;"> </span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">set1 = {</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'h'</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'e'</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'l'</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'l'</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'o'</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">}</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(type(set1), set1)</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># <class 'set'> {'l', 'o', 'e', 'h'}</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size: 10pt;"> </span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt; background:#ffff96"><u>set2</u></span> <span style="font-size:10pt">= set(list1)</span></font></div></div></div></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div>더불어, set이라는 자료형도 있는데, 이는 중복을 허용하지 않는 순서없는 집합이다.</div><div><br/></div><div><br/></div></div> Elexhttp://www.blogger.com/profile/04776810249062811247noreply@blogger.com0tag:blogger.com,1999:blog-5118001013944805388.post-75261832415751910292014-07-30T07:30:00.000+09:002014-07-30T07:30:01.534+09:00자료형 - 문자열<div class="ad" style="border-radius: 5px; border: 1px solid #666; box-shadow: 3px 3px 1px 1px #444; float: right; margin: 0 0 0.76em 0.82em; padding: 3px;">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 파이선 프로그래밍 블로그 본문 -->
<ins class="adsbygoogle"
style="display:inline-block;width:300px;height:250px"
data-ad-client="ca-pub-7196581333897022"
data-ad-slot="6210625718"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="ennote"><div align="left"><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span><span style="font-size:10pt"><u>string</u></span><span style="font-size:10pt"> =</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"This is a string."</i></span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(type(string), string)</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># <class '</span><span style="font-size:10pt"><u>str</u></span><span style="font-size:10pt">'> This is a string.</span></font></div></div><div><br/></div><div>문자열은 따옴표(" 또는 ')로 감싸서 표현한다.</div><div><br/></div><div><br/></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span> <span style="font-size:10pt"><u>string</u></span> <span style="font-size:10pt">=</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"She's back."</i></span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span> <span style="font-size:10pt"><u>string</u></span> <span style="font-size:10pt">=</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'"Hello", he said.'</i></span></font></div><div><br/></div><div>PHP와 마찬가지로, 이중 따옴표(") 또는 단일 따옴표(')를 모두 사용할 수 있는데, 이중 따옴표 안에는 단일 따옴표를 포함시킬 수 있으며, 단일 따옴표 안에는 이중 따옴표를 포함시킬 수 있다. 필요에 따라 상황에 맞게 골라쓰면 된다.</div><div><br/></div><div><br/></div><div><span style="font-size: 10pt; font-family: &amp;amp;apos;Source Code Pro&amp;amp;apos;;"> </span></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span> <span style="font-size:10pt"><u>string</u></span> <span style="font-size:10pt">=</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"두번째,\n문자열이다."</i></span></font></div><div align="left"><font face="Source Code Pro"><br/></font></div><div>줄바꿈 이스케잎 문자(\n)을 써서 여러 줄로 만들 수도 있다. \t, \\, \", \" 등등 그 외에도 다양한 이스케잎 문자들을 사용할 수 있다.</div><div><br/></div><div><br/></div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> number =</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">14</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span> <span style="font-size:10pt"><u>string</u></span> <span style="font-size:10pt">=</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"숫자는 {}이다."</i></span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(string.format(number))</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 숫자는 14이다.</span></font></div><div><br/></div><div>문자열 객체의 format() 메서드를 통해서, 문자열 내에 변수를 삽입할 수 있다. 위의 코드는 python 3 이상에서 사용되는 스타일이며 python 2에서는 <i>"숫자는 %s이다." % number</i> 와 같은 방식으로 사용해야 한다.</div><div><br/></div><div><br/></div><hr/><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> multiline =</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'''\</i></span></font></div><div align="left"><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>안녕,</i></span></font></div><div align="left"><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>블라블라~</i></span></font></div><div align="left"><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>끝.</i></span></font></div><div align="left"><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>'''</i></span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(multiline)</span></font></div><div><br/></div><div>따옴표를 세번 겹쳐 쓰면(''' 또는 """) 여러 줄 문자열을 표현할 수도 있다. 코드 내의 줄바꿈이나 들여 쓰기 등이 보존되므로 가독성이 높아진다는 장점이 있다. </div><div>덧으로, 위 코드에서는 시작하는 따옴표 바로 뒤에 백슬래쉬(\)가 있는데, 다음 줄부터 시작된다는 의미로 이해하면 된다. 시험삼아 백슬래쉬를 지우고 출력해보면 차이점을 쉽게 알 수 있을 것이다.</div><div><br/></div><div><br/></div></div>Elexhttp://www.blogger.com/profile/04776810249062811247noreply@blogger.com0tag:blogger.com,1999:blog-5118001013944805388.post-46870986475807282442014-07-29T07:00:00.000+09:002014-07-29T07:00:00.751+09:00자료형 - 숫자<div class="ad" style="border-radius: 5px; border: 1px solid #666; box-shadow: 3px 3px 1px 1px #444; float: right; margin: 0 0 0.76em 0.82em; padding: 3px;">
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 파이선 프로그래밍 블로그 본문 -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-7196581333897022" data-ad-slot="6210625718" style="display: inline-block; height: 250px; width: 300px;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<div class="ennote" style="-webkit-line-break: after-white-space; -webkit-nbsp-mode: space; word-wrap: break-word;">
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span> <span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">number =</span></span> <span style="color: maroon; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">17</span></span><span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span></div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span> <span style="color: blue; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">print</span></span><span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">(type(number), number)</span></span> <span style="color: silver; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"># <class '</span><span style="font-size: 10pt;"><u>int</u></span><span style="font-size: 10pt;">'> 17</span></span></div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span></div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span> <span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">number =</span></span> <span style="color: maroon; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">17.0</span></span></div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span> <span style="color: blue; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">print</span></span><span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">(type(number), number)</span></span> <span style="color: silver; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"># <class 'float'> 17.0</span></span></div>
<div align="left">
<span style="font-family: Source Code Pro;"><br /></span></div>
<div>
파이썬의 숫자형에는 크게 두 종류가 있는데, 정수형(int)과 부동소수형(float)이다. </div>
<div>
물론, 이 글의 맨 아래에 등장하는 복소수형(complex)도 있다.</div>
<div>
<br /></div>
<div>
<br /></div>
<div align="left">
<hr />
</div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span> <span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">number =</span></span> <span style="color: maroon; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">17</span></span><span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">/</span></span><span style="color: maroon; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">3</span></span></div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span> <span style="color: blue; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">print</span></span><span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">(type(number), number)</span></span> <span style="color: silver; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"># <class 'float'> 5.666666666666667</span></span></div>
<div align="left">
<span style="color: silver; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"><br /></span></span></div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span> <span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">number =</span></span> <span style="color: maroon; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">4</span></span><span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">/</span></span><span style="color: maroon; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">2</span></span></div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span> <span style="color: blue; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">print</span></span><span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">(type(number), number)</span></span> <span style="color: silver; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"># <class 'float'> 2.0</span></span></div>
<div align="left">
<span style="color: silver; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"><br /></span></span></div>
<div>
정수형을 나눗셈 연산하면 부동소수형이 된다.</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<hr />
</div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span> <span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">number =</span></span> <span style="color: maroon; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">17</span></span><span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">//</span></span><span style="color: maroon; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">3</span></span></div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span> <span style="color: blue; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">print</span></span><span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">(type(number), number)</span></span> <span style="color: silver; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"># <class '</span><span style="font-size: 10pt;"><u>int</u></span><span style="font-size: 10pt;">'> 5</span></span></div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span></div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span> <span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">number = round(</span></span> <span style="color: maroon; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">17</span></span><span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">/</span></span><span style="color: maroon; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">3</span></span> <span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">)</span></span></div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span> <span style="color: blue; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">print</span></span><span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">(type(number), number)</span></span> <span style="color: silver; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"># <class '</span><span style="font-size: 10pt;"><u>int</u></span><span style="font-size: 10pt;">'> 6</span></span></div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span></div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span> <span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">number = round(</span></span> <span style="color: maroon; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">17</span></span><span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">/</span></span><span style="color: maroon; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">3</span></span> <span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">,</span></span> <span style="color: maroon; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">2</span></span><span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">)</span></span></div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span> <span style="color: blue; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">print</span></span><span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">(type(number), number)</span></span> <span style="color: silver; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"># <class '</span><span style="font-size: 10pt;"><u>int</u></span><span style="font-size: 10pt;">'> 5.67</span></span></div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span></div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span> <span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">number =</span></span> <span style="color: maroon; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">17</span></span><span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">%</span></span><span style="color: maroon; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">3</span></span></div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span> <span style="color: blue; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">print</span></span><span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">(type(number), number)</span></span> <span style="color: silver; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"># <class '</span><span style="font-size: 10pt;"><u>int</u></span><span style="font-size: 10pt;">'> 2 </span></span></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
나눗셈 연산의 결과를 정수형으로 얻고 싶은 때에는 // 연산자를 사용할 수 있는데 소숫점 이하 부분은 버려진다. 이 외에도 round() 함수를 사용해서 반올림 시킬 수도 있다. round() 함수의 두번째 매개변수는 소숫점 이하 자릿수. 그리고 다른 프로그래밍 언어와 마찬가지로 나머지 연산자(%)가 존재한다.</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<hr />
</div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span> <span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">number = int(</span></span> <span style="color: maroon; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">17.6</span></span><span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">)</span></span></div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span> <span style="color: blue; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">print</span></span><span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">(type(number), number)</span></span> <span style="color: silver; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"># <class '</span><span style="font-size: 10pt;"><u>int</u></span><span style="font-size: 10pt;">'> 17 </span></span></div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span></div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span> <span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">number = float(</span></span> <span style="color: maroon; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">17</span></span><span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">)</span></span></div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span> <span style="color: blue; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">print</span></span><span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">(type(number), number)</span></span> <span style="color: silver; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"># <class 'float'> 17.0</span></span></div>
<div>
<br /></div>
<div>
int()와 float()은 언뜻보기에 형변환 함수인 듯하지만, 실제로는 int와 float 클래스의 인스턴스를 생성하는 것이다.</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<hr />
</div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span> <span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">number =</span></span> <span style="color: maroon; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">2.98e10</span></span><span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span></div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span> <span style="color: blue; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">print</span></span><span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">(type(number), number)</span></span> <span style="color: silver; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"># <class 'float'> 29800000000.0</span></span></div>
<div>
<br /></div>
<div>
공학적 표기법을 사용할 수도 있고,<br />
<br /></div>
<div>
<br /></div>
<div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span> <span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">number =</span></span> <span style="color: maroon; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">0</span></span><span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">b0101</span></span> <span style="color: silver; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"># 2진수 표기법</span></span></div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span> <span style="color: blue; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">print</span></span><span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">(type(number), number)</span></span> <span style="color: silver; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"># <class '</span><span style="font-size: 10pt;"><u>int</u></span><span style="font-size: 10pt;">'> 5</span></span><br />
<span style="color: silver; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"><br /></span></span></div>
</div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span> <span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">number =</span></span> <span style="color: maroon; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">0</span></span><span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">o17</span></span> <span style="color: silver; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"># 8진수 표기법</span></span></div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span> <span style="color: blue; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">print</span></span><span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">(type(number), number)</span></span> <span style="color: silver; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"># <class '</span><span style="font-size: 10pt;"><u>int</u></span><span style="font-size: 10pt;">'> 15</span></span></div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span></div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span> <span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">number =</span></span> <span style="color: maroon; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">0x3f</span></span> <span style="color: silver; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"># 16진수 표기법</span></span></div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span> <span style="color: blue; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">print</span></span><span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">(type(number), number)</span></span> <span style="color: silver; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"># <class '</span><span style="font-size: 10pt;"><u>int</u></span><span style="font-size: 10pt;">'> 63</span></span></div>
<div>
<br /></div>
<div>
2진수, 8진수 혹은 16진수로 표기하는 것도 가능하며,</div>
<div>
<br /></div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span></div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span> <span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">number =</span></span> <span style="color: maroon; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">3</span></span><span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">-</span></span><span style="color: maroon; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">2</span></span><span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">j</span></span> <span style="color: silver; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"># 복소수</span></span></div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span> <span style="color: blue; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">print</span></span><span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">(type(number), number)</span></span> <span style="color: silver; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"># <class 'complex'> (3-2j)</span></span></div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span></div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span> <span style="color: blue; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">print</span></span><span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">(number.real)</span></span> <span style="color: silver; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"># 실수부</span></span></div>
<div align="left">
<span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"> </span></span> <span style="color: blue; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">print</span></span><span style="font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;">(number.imag)</span></span> <span style="color: silver; font-family: Source Code Pro; font-size: x-small;"><span style="font-size: 10pt;"># 허수부</span></span><span style="font-family: &amp; font-size: 10pt;"> </span></div>
<div align="left">
</div>
<div>
복소수 표현도 있다.</div>
<div>
<br /></div>
<div>
<br /></div>
</div>
Elexhttp://www.blogger.com/profile/04776810249062811247noreply@blogger.com0tag:blogger.com,1999:blog-5118001013944805388.post-41993111402534317712014-07-28T06:30:00.000+09:002014-07-28T12:05:54.374+09:00변수, 객체, 그리고 값<div class="ad" style="border-radius: 5px; border: 1px solid #666; box-shadow: 3px 3px 1px 1px #444; float: right; margin: 0 0 0.76em 0.82em; padding: 3px;">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 파이선 프로그래밍 블로그 본문 -->
<ins class="adsbygoogle"
style="display:inline-block;width:300px;height:250px"
data-ad-client="ca-pub-7196581333897022"
data-ad-slot="6210625718"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="ennote"> 파이썬에서는 모든 것이 객체다. 변수도 객체고, 함수도 객체고, 심지어 코드도 객체다. <div><br/><div>모든 객체는 ID, 타입, 그리고 값을 가진다. ID는 객체의 인스턴스가 자지는 고유 식별자이며 변경될 수 없는 값이다. 타입은 객체의 자료형이며 이 또한 변경될 수 없다. 값은 객체의 내용이다. </div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size: 10pt;"> </span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">a =</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">3</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><div align="left"><font face="Source Code Pro" size="2"><span style="font-size: 10pt;"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(id(a))</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># ID</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(type(a))</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 타입</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(a)</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt">#값</span></font></div></div><div align="left"><font face="Source Code Pro"><br/></font></div><div><br/></div><div><br/></div><div>값을 변경할 수 있는 것을 mutable 객체라 하고, 값을 변경할 수 없는 것을 immutable 객체라고 한다. 대부분의 기본 타입들은 immutable 이다. 숫자, 문자열, 튜플은 변경할 수 없는 객체이고, 리스트, 딕셔너리 등은 변경가능한 객체이다.</div></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size: 10pt;"> </span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">a =</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">3</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(id(a))</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 1799077480</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size: 10pt;"> </span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><u>a</u></span> <span style="font-size:10pt">=</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">4</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(id(a))</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 1799077496</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size: 10pt;"> </span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">a =</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">3</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(id(a))</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 1799077480</span></font></div><div><br/></div><div>위 코드와 같이 변수의 값을 변경하는 경우에, 실제로는 변수가 가르키는 대상이 바뀌는 것이다. </div><div>3을 가리키면 '그것'은 3이되고 4를 가리키면 '그것'은 4가 되는지라, 3은 3이요 4는 4이니라.</div><div><br/></div><div><br/></div><div><br/></div><div>명시적으로 변수를 메모리에서 해제하려면 del() 함수를 사용하면 되는데, 파이썬에서는 가비지컬렉션이 작동하므로 굳이 쓸 일은 없다.</div><div><br/></div></div>Elexhttp://www.blogger.com/profile/04776810249062811247noreply@blogger.com0tag:blogger.com,1999:blog-5118001013944805388.post-40018907199249078042014-07-25T15:13:00.002+09:002014-07-25T15:15:12.482+09:00클래스<div class="ad" style="border-radius: 5px; border: 1px solid #666; box-shadow: 3px 3px 1px 1px #444; float: right; margin: 0 0 0.76em 0.82em; padding: 3px;">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 파이선 프로그래밍 블로그 본문 -->
<ins class="adsbygoogle"
style="display:inline-block;width:300px;height:250px"
data-ad-client="ca-pub-7196581333897022"
data-ad-slot="6210625718"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="ennote"><div align="left"><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt">#!/</span><span style="font-size:10pt"><u>usr</u></span><span style="font-size:10pt">/bin/python3</span></font></div><div align="left"><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 클래스</span></font></div><div align="left"><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># by</span> <span style="font-size:10pt"><u>Elex</u></span> <span style="font-size:10pt">(http://python.elex.pe.kr/)</span></font></div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">class</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>MyObject</b></span><span style="font-size:10pt">:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 생성자</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>__init__</b></span><span style="font-size:10pt">(</span><span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">, param1=</span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">0</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">):</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span> <span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">.</span><span style="font-size:10pt"><u>param1</u></span> <span style="font-size:10pt">= param1</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 메서드</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>getParam1</b></span><span style="font-size:10pt">(</span><span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">):</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">return</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><i>self</i></span><span style="font-size:10pt">.param1</span></font></div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>main</b></span><span style="font-size:10pt">():</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 인스턴스 생성</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> obj1 = MyObject()</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span> <span style="font-size:10pt"><u>obj2</u></span> <span style="font-size:10pt">= MyObject(</span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">7</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(obj1.getParam1())</span></font></div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">if</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">__name__ ==</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"__main__"</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">: main()</span></font></div><div><br/></div><div><br/></div><div>파이썬에서는 class 키워드를 사용해서 클래스를 정의할 수 있다.</div><div>클래스의 생성자 메서드는 __init__이다. </div><div>모든 메서드의 첫 번째 매개변수는 self인데, 이것의 용법은 타 언어의 this와 같다.</div><div>클래스의 인스턴스를 생성하는 경우, 다른 프로그래밍 언어와는 달리 new 키워드를 사용하지 않고, 그냥 변수에 할당하면 된다.</div><div><br/></div><div><br/></div></div>Elexhttp://www.blogger.com/profile/04776810249062811247noreply@blogger.com0tag:blogger.com,1999:blog-5118001013944805388.post-85090962814042268462014-07-25T15:13:00.001+09:002014-07-25T15:14:55.934+09:00함수<div class="ad" style="border-radius: 5px; border: 1px solid #666; box-shadow: 3px 3px 1px 1px #444; float: right; margin: 0 0 0.76em 0.82em; padding: 3px;">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 파이선 프로그래밍 블로그 본문 -->
<ins class="adsbygoogle"
style="display:inline-block;width:300px;height:250px"
data-ad-client="ca-pub-7196581333897022"
data-ad-slot="6210625718"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="ennote"><div align="left"><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt">#!/</span><span style="font-size:10pt"><u>usr</u></span><span style="font-size:10pt">/bin/python3</span></font></div><div align="left"><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 함수</span></font></div><div align="left"><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># by</span> <span style="font-size:10pt"><u>Elex</u></span> <span style="font-size:10pt">(http://python.elex.pe.kr/)</span></font></div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>main</b></span><span style="font-size:10pt">():</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> funcOne()</span></font></div><div align="left"><span style="font-size: 10pt; font-family: &apos;Source Code Pro&apos;;"> </span></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>funcOne</b></span><span style="font-size:10pt">():</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">for</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">i</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">in</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">range(</span></font><font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">10</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">):</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(i, end=</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>' '</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">()</span></font></div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">if</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">__name__ ==</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"__main__"</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">: main()</span></font></div><div><br/></div><div><br/></div><div>파이썬에서 함수는 def라는 키워드를 사용해서 정의한다. 함수를 호출할 때에는 당연히 함수 이름을 사용해서 호출하면 된다.</div><div><br/></div><div><br/></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>funcOne</b></span><span style="font-size:10pt">(a):</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">for</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">i</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">in</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">range(a,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">10</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">):</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(i, end=</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>' '</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">()</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>funcTwo</b></span><span style="font-size:10pt">(a=</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">0</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">):</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">for</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">i</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">in</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">range(a,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">10</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">):</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(i, end=</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>' '</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">()</span></font></div><div><br/></div><div>당연히, 함수에 매개변수를 사용할 수도 있다. funcTwo(a=0)처럼 매개변수 생략시의 기본값을 지정해 줄 수도 있다.</div><div><br/></div><div><br/></div><div>참고로, range() 함수는 주로 반복문에서 사용되며, 일련의 가상 수열을 반환한다. range(10)은 0~9, range(3, 10)은 3~9를 나타낸다.</div><div>또 참고로, print()함수는 기본적으로 맨 끝에 줄바꿈 문자(\n)를 붙이는 데, end 매개변수를 지정해서 다른 문자로 변경할 수 있다.</div><div><br/></div><div><br/></div></div>Elexhttp://www.blogger.com/profile/04776810249062811247noreply@blogger.com0tag:blogger.com,1999:blog-5118001013944805388.post-40494283681681129592014-07-25T15:13:00.000+09:002014-07-25T15:14:43.260+09:00조건문<div class="ad" style="border-radius: 5px; border: 1px solid #666; box-shadow: 3px 3px 1px 1px #444; float: right; margin: 0 0 0.76em 0.82em; padding: 3px;">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 파이선 프로그래밍 블로그 본문 -->
<ins class="adsbygoogle"
style="display:inline-block;width:300px;height:250px"
data-ad-client="ca-pub-7196581333897022"
data-ad-slot="6210625718"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="ennote"><div align="left"><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt">#!/</span><span style="font-size:10pt"><u>usr</u></span><span style="font-size:10pt">/bin/python3</span></font></div><div align="left"><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 조건문</span></font></div><div align="left"><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># by</span> <span style="font-size:10pt"><u>Elex</u></span> <span style="font-size:10pt">(http://python.elex.pe.kr/)</span></font></div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>main</b></span><span style="font-size:10pt">():</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> a, b =</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">2</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">1</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">if</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">a > b:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"a가 b보다 더 크다"</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">elif</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">a < b:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"a가 b보다 더 작다"</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">else</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font><font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"a는 b와 같다"</i></span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font></div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">if</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">__name__ ==</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"__main__"</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">: main()</span></font></div><div><br/></div><div><br/></div><div><br/></div><div>파이썬의 조건문은 if, elif, else 키워드를 사용한다. elseif가 아니다. 입 맛에 맞게 elif나 else를 생략할 수 있는 것은 다른 프로그래밍 언어와 동일하다.</div><div><br/></div><div><br/></div><div>하아~! 그런데, 파이썬에는 <i>s = (a>b) ? "크다" : "크지 않다"</i> 같은 삼항 조건 연산자가 없다. </div><div>대신, 파이썬에서는 다음과 같이 사용한다.</div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> s =</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"크다"</i></span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">if</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">a > b</span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">else</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"크지 않다"</i></span></font></div><div>영어권 프로그래머에겐 합리적인 어순일지도 모르겠으나, 적어도 내겐 영 그렇다. 이리 쓸 바에야 차라리 평범한 if-else 구문을 쓰는 게 낫지.</div><div><br/></div><div><br/></div></div>Elexhttp://www.blogger.com/profile/04776810249062811247noreply@blogger.com1tag:blogger.com,1999:blog-5118001013944805388.post-28855814734108367412014-07-25T15:12:00.000+09:002014-07-25T15:14:13.894+09:00변수<div class="ad" style="border-radius: 5px; border: 1px solid #666; box-shadow: 3px 3px 1px 1px #444; float: right; margin: 0 0 0.76em 0.82em; padding: 3px;">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 파이선 프로그래밍 블로그 본문 -->
<ins class="adsbygoogle"
style="display:inline-block;width:300px;height:250px"
data-ad-client="ca-pub-7196581333897022"
data-ad-slot="6210625718"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="ennote"><b>선언 / 할당</b><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span><span style="font-size:10pt"><u>a</u></span> <span style="font-size:10pt">=</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">1</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span> <span style="font-size:10pt"><u>b</u></span> <span style="font-size:10pt">=</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"Hello"</i></span></font></div><div>변수를 선언하기 위한 키워드 같은 건 없다. var라거나 int라거나 float 같은 그런 거 없다. 그냥 새로운 변수 이름을 쓰면 알아서 변수가 생성된다.</div><div><br/></div><div><br/></div><div><b>다중 할당</b></div><div align="left"><div align="left"><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> a = b =</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">1</span></font></div><div align="left"><font face="Source Code Pro">이런 식의 할당은 다른 프로그래밍 언어에서도 가능하다.</font></div></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> a, b =</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">0</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">,</span></font> <font color="#800000" face="Source Code Pro" size="2"><span style="font-size:10pt">1</span></font></div><div align="left"><font face="Source Code Pro">그런데, 특이하게도 파이썬에서는 여러 개의 변수를 한 번에 할당할 수도 있는데, 위와 같이 할당하면 a=0, b=1이 된다.</font></div><div align="left"><font face="Source Code Pro"><br/></font></div><div align="left"><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> a, b = b, a</span></font></div><div align="left"><font face="Source Code Pro">위 문장은 a, b의 값을 서로 바꾸는 동작을 한다. 즉, 스왑.</font></div><div align="left"><font face="Source Code Pro"><br/></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div></div><div align="left"><font face="Source Code Pro"><b>변수명</b></font></div><div align="left"><font face="Source Code Pro">다른 언어와 비슷한 규칙이 적용된다. 대소문자를 구분한다거나, 숫자로 시작할 수 없다거나, 키워드는 사용할 수 없다거나 등등.</font></div><div align="left"><font face="Source Code Pro">참고로 파이썬의 키워드는 다음과 같은 코드를 통해서 확인할 수 있다.</font></div></div><div align="left"><font color="#C0C0C0" face="Source Code Pro"><br/></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">import</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">keyword</span></font></div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>main</b></span><span style="font-size:10pt">():</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(keyword.kwlist)</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt">#키워드 목록 (튜플)</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(len(keyword.kwlist))</span></font> <font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt">#키워드 목록의 갯수</span></font></div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">if</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">__name__ ==</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"__main__"</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> main()</span></font></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div></div>Elexhttp://www.blogger.com/profile/04776810249062811247noreply@blogger.com0tag:blogger.com,1999:blog-5118001013944805388.post-16269448550616188652014-07-25T15:07:00.001+09:002014-07-25T15:11:36.236+09:00메인 스크립트<div class="ad" style="border-radius: 5px; border: 1px solid #666; box-shadow: 3px 3px 1px 1px #444; float: right; margin: 0 0 0.76em 0.82em; padding: 3px;">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 파이선 프로그래밍 블로그 본문 -->
<ins class="adsbygoogle"
style="display:inline-block;width:300px;height:250px"
data-ad-client="ca-pub-7196581333897022"
data-ad-slot="6210625718"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="ennote"><div align="left"><div align="left"><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt">#!/</span><span style="font-size:10pt"><u>usr</u></span><span style="font-size:10pt">/bin/python3</span></font></div><div align="left"><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># 가장 기본적인 메인 스크립트 템플릿</span></font></div><div align="left"><font color="#C0C0C0" face="Source Code Pro" size="2"><span style="font-size:10pt"># by</span> <span style="font-size:10pt"><u>Elex</u></span> <span style="font-size:10pt">(http://python.elex.pe.kr/)</span></font></div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">def</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt"><b>main</b></span><span style="font-size:10pt">():</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> </span></font> <font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">print</span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">(</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"Hello"</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">)</span></font></div><div align="left"><font color="#010101" face="Source Code Pro" size="2"><span style="font-size:10pt"><br/></span></font></div><div align="left"><font color="#0000FF" face="Source Code Pro" size="2"><span style="font-size:10pt">if</span></font> <font face="Source Code Pro" size="2"><span style="font-size:10pt">__name__ ==</span></font> <font color="#00AA00" face="Source Code Pro" size="2"><span style="font-size:10pt"><i>"__main__"</i></span></font><font face="Source Code Pro" size="2"><span style="font-size:10pt">:</span></font></div><div align="left"><font face="Source Code Pro" size="2"><span style="font-size:10pt"> main()</span></font></div></div><div><br/></div><div><br/></div><div>파이썬 스크립트의 맨 첫 줄에는 파이썬 인터프리터의 경로를 지정한다.</div><div>현재 스크립트가 메인 모듈로써 실행되는 경우에는 __name__ 변수의 값이 "__main__"이 된다. 따라서, 이 스크립트는 메인 모듈로써 실행되는 경우에만 main() 함수를 처리하게 된다.</div><div><br/></div><div><br/></div><div><br/></div><div><br/></div></div>Elexhttp://www.blogger.com/profile/04776810249062811247noreply@blogger.com0tag:blogger.com,1999:blog-5118001013944805388.post-85423932133961717332012-08-31T17:20:00.001+09:002014-07-25T15:06:56.496+09:00파이선 배우기 #1<div class="ad" style="border-radius: 5px; border: 1px solid #666; box-shadow: 3px 3px 1px 1px #444; float: right; margin: 0 0 0.76em 0.82em; padding: 3px;">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 파이선 프로그래밍 블로그 본문 -->
<ins class="adsbygoogle"
style="display:inline-block;width:300px;height:250px"
data-ad-client="ca-pub-7196581333897022"
data-ad-slot="6210625718"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<div>
화면에 간단한 문자열을 출력해보고, 주석에 대해서도 알아보겠다.<br />
<br />
다음을 텍스트 편집기에 입력하고 ex001.py란 이름으로 저장해둔다.<br />
<pre style="background-color: white; border-radius: 5px; border: 1px solid #666; box-shadow: 6px 6px 3px 1px #666; font-face: monospace; margin: 0.76em; padding: 0.82em;"><code><span style="color: black;"> 1 </span><span style="color: #7f0055; font-weight: bold;">print</span> <span style="color: blue;">"Hello"</span>
</code></pre>
<br />
터미널에서 python ex001.py라고 입력한 후 실행하면,<br />
누구나 쉽게 예상할 수 있듯이 화면에 Hello를 출력한다.<br />
<br />
한 줄 더 추가해 보자.<br />
<pre style="background-color: white; border-radius: 5px; border: 1px solid #666; box-shadow: 6px 6px 3px 1px #666; font-face: monospace; margin: 0.76em; padding: 0.82em;"><code><span style="color: black;"> 1 </span><span style="color: #7f0055; font-weight: bold;">print</span> <span style="color: blue;">"Hello"</span>
<span style="color: black;"> 2 </span><span style="color: #7f0055; font-weight: bold;">print</span> <span style="color: blue;">"How are you?"</span>
</code></pre>
<br />
한글 출력은 어떤가? 다음과 같이 수정해 보자.<br />
<pre style="background-color: white; border-radius: 5px; border: 1px solid #666; box-shadow: 6px 6px 3px 1px #666; font-face: monospace; margin: 0.76em; padding: 0.82em;"><code><span style="color: black;"> 1 </span><span style="color: #7f0055; font-weight: bold;">print</span> <span style="color: blue;">"안녕"</span>
<span style="color: black;"> 2 </span><span style="color: #7f0055; font-weight: bold;">print</span> <span style="color: blue;">"방가방가"</span>
</code></pre>
<br />
인코딩을 지정할 필요가 있다며 오류를 뿜는다. 수정하자. 인코딩은 소스 파일의 첫 번째 혹은 두번째 라인에 적는다.<br />
<pre style="background-color: white; border-radius: 5px; border: 1px solid #666; box-shadow: 6px 6px 3px 1px #666; font-face: monospace; margin: 0.76em; padding: 0.82em;"><code><span style="color: black;"> 1 </span><span style="color: #2f9956;">#!/usr/bin/python</span>
<span style="color: black;"> 2 </span><span style="color: #2f9956;"># -*- coding: utf-8 -*-</span>
<span style="color: black;"> 3 </span><span style="color: #7f0055; font-weight: bold;">print</span> <span style="color: blue;">"안녕"</span>
<span style="color: black;"> 4 </span><span style="color: #7f0055; font-weight: bold;">print</span> <span style="color: blue;">"방가방가"</span>
</code></pre>
<br />
파일을 utf-8형식으로 저장한 때에는 별 문제가 없겠으나 좀 더 확실히 해두자면,<br />
<br />
<pre style="background-color: white; border-bottom-left-radius: 5px; border-bottom-right-radius: 5px; border-top-left-radius: 5px; border-top-right-radius: 5px; border: 1px solid rgb(102, 102, 102); box-shadow: rgb(102, 102, 102) 6px 6px 3px 1px; margin: 0.76em; padding: 0.82em;"><code><span style="color: black;"> 1 </span><span style="color: #7f0055; font-weight: bold;">print</span> u<span style="color: blue;">"안녕"</span></code></pre>
<br />
이렇게 해야 옳다.<br />
<br />
이는 문자열의 길이를 출력해보면 그 차이를 확인해 볼 수 있다.<br />
<pre style="background-color: white; border-radius: 5px; border: 1px solid #666; box-shadow: 6px 6px 3px 1px #666; font-face: monospace; margin: 0.76em; padding: 0.82em;"><code><span style="color: black;"> 1 </span><span style="color: #7f0055; font-weight: bold;">print</span> <span style="color: #7f0055; font-weight: bold;">len</span><span style="color: black;">(</span><span style="color: blue;">"안녕하세요"</span><span style="color: black;">)</span>
<span style="color: black;"> 2 </span><span style="color: #7f0055; font-weight: bold;">print</span> <span style="color: #7f0055; font-weight: bold;">len</span><span style="color: black;">(</span>u<span style="color: blue;">"안녕하세요"</span><span style="color: black;">)</span>
</code></pre>
<br />
#으로 시작하는 이후의 문장은 주석문으로 인식된다.<br />
<pre style="background-color: white; border-radius: 5px; border: 1px solid #666; box-shadow: 6px 6px 3px 1px #666; font-face: monospace; margin: 0.76em; padding: 0.82em;"><code><span style="color: black;"> 1 </span><span style="color: #7f0055; font-weight: bold;">print</span> <span style="color: blue;">"Hello"</span>
<span style="color: black;"> 2 </span><span style="color: #2f9956;">#print "How are you?"</span>
</code></pre>
<br /></div>Elexhttp://www.blogger.com/profile/04776810249062811247noreply@blogger.com0tag:blogger.com,1999:blog-5118001013944805388.post-18153786532637918992012-08-31T17:07:00.001+09:002012-08-31T17:10:44.407+09:00소스코드 작성 규칙<div class="ad" style="border-radius: 5px; border: 1px solid #666; box-shadow: 3px 3px 1px 1px #444; float: right; margin: 0 0 0.76em 0.82em; padding: 3px;">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-7196581333897022";
/* 파이선 프로그래밍 블로그 본문 */
google_ad_slot = "6210625718";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript">
</script></div>
<div>
파이선의 소스 코드를 작성하는 데에는 다른 프로그래밍 언어들과는 다른 특징들이 있다.<br />
<br />
줄은 개행문자로 구분된다.<br />
<br />
줄 끝에 세미콜론(;)이 사용되지 않는다. C 또는 Java 등과 다른 특징 중 하나이다.<br />
<br />
파이썬에서 하나의 명령은 하나의 줄에 입력한다.<br />
<br />
명시적으로 두 개의 줄을 하나로 연결하고자 하는 경우에는 역슬래시(\)를 사용한다.<br />
<pre style="background-color: white; border-radius: 5px; border: 1px solid #666; box-shadow: 6px 6px 3px 1px #666; font-face: monospace; margin: 0.76em; padding: 0.82em;"><code><span style="color: black;"> 1 </span><span style="color: #7f0055; font-weight: bold;">if</span> <span style="color: black;">1900</span> <span style="color: black;"><</span> year <span style="color: black;"><</span> <span style="color: black;">2100</span> <span style="color: #7f0055; font-weight: bold;">and</span> <span style="color: black;">1</span> <span style="color: black;"><=</span> month <span style="color: black;"><=</span> <span style="color: black;">12</span> \
<span style="color: black;"> 2 </span> <span style="color: #7f0055; font-weight: bold;">and</span> <span style="color: black;">1</span> <span style="color: black;"><=</span> day <span style="color: black;"><=</span> <span style="color: black;">31</span> <span style="color: #7f0055; font-weight: bold;">and</span> <span style="color: black;">0</span> <span style="color: black;"><=</span> hour <span style="color: black;"><</span> <span style="color: black;">24</span> \
<span style="color: black;"> 3 </span> <span style="color: #7f0055; font-weight: bold;">and</span> <span style="color: black;">0</span> <span style="color: black;"><=</span> minute <span style="color: black;"><</span> <span style="color: black;">60</span> <span style="color: #7f0055; font-weight: bold;">and</span> <span style="color: black;">0</span> <span style="color: black;"><=</span> second <span style="color: black;"><</span> <span style="color: black;">60</span><span style="color: black;">:</span> <span style="color: #2f9956;"># Looks like a valid date</span>
<span style="color: black;"> 4 </span> <span style="color: #7f0055; font-weight: bold;">return</span> <span style="color: black;">1</span>
</code></pre>
<br />
괄호 ( (, {, [ )로 둘러 쌓인 여러 줄은 하나의 줄로 연결된 것으로 인식된다.<br />
<pre style="background-color: white; border-radius: 5px; border: 1px solid #666; box-shadow: 6px 6px 3px 1px #666; font-face: monospace; margin: 0.76em; padding: 0.82em;"><code><span style="color: black;"> 1 </span>month_names <span style="color: black;">= [</span><span style="color: blue;">'Januari'</span><span style="color: black;">,</span> <span style="color: blue;">'Februari'</span><span style="color: black;">,</span> <span style="color: blue;">'Maart'</span><span style="color: black;">,</span> <span style="color: #2f9956;"># These are the</span>
<span style="color: black;"> 2 </span> <span style="color: blue;">'April'</span><span style="color: black;">,</span> <span style="color: blue;">'Mei'</span><span style="color: black;">,</span> <span style="color: blue;">'Juni'</span><span style="color: black;">,</span> <span style="color: #2f9956;"># Dutch names</span>
<span style="color: black;"> 3 </span> <span style="color: blue;">'Juli'</span><span style="color: black;">,</span> <span style="color: blue;">'Augustus'</span><span style="color: black;">,</span> <span style="color: blue;">'September'</span><span style="color: black;">,</span> <span style="color: #2f9956;"># for the months</span>
<span style="color: black;"> 4 </span> <span style="color: blue;">'Oktober'</span><span style="color: black;">,</span> <span style="color: blue;">'November'</span><span style="color: black;">,</span> <span style="color: blue;">'December'</span><span style="color: black;">]</span> <span style="color: #2f9956;"># of the year</span>
</code></pre>
<br />
#은 주석의 시작을 나타내며, 해당 줄의 끝까지 주석으로 인식된다.<br />
<br />
빈 줄은 무시된다.<br />
<br />
문장을 시작하기 전의 들여쓰기는 문장의 그룹을 묶는 것으로 파이썬 언어에 있어서 매우 중요한 특징이다.<br />
<pre style="background-color: white; border-radius: 5px; border: 1px solid #666; box-shadow: 6px 6px 3px 1px #666; font-face: monospace; margin: 0.76em; padding: 0.82em;"><code><span style="color: black;"> 1 </span><span style="color: #7f0055; font-weight: bold;">def</span> <span style="color: black;">perm</span><span style="color: black;">(</span>l<span style="color: black;">):</span>
<span style="color: black;"> 2 </span> <span style="color: #2f9956;"># Compute the list of all permutations of l</span>
<span style="color: black;"> 3 </span> <span style="color: #7f0055; font-weight: bold;">if</span> <span style="color: #7f0055; font-weight: bold;">len</span><span style="color: black;">(</span>l<span style="color: black;">) <=</span> <span style="color: black;">1</span><span style="color: black;">:</span>
<span style="color: black;"> 4 </span> <span style="color: #7f0055; font-weight: bold;">return</span> <span style="color: black;">[</span>l<span style="color: black;">]</span>
<span style="color: black;"> 5 </span> r <span style="color: black;">= []</span>
<span style="color: black;"> 6 </span> <span style="color: #7f0055; font-weight: bold;">for</span> i <span style="color: #7f0055; font-weight: bold;">in</span> <span style="color: #7f0055; font-weight: bold;">range</span><span style="color: black;">(</span><span style="color: #7f0055; font-weight: bold;">len</span><span style="color: black;">(</span>l<span style="color: black;">)):</span>
<span style="color: black;"> 7 </span> s <span style="color: black;">=</span> l<span style="color: black;">[:</span>i<span style="color: black;">] +</span> l<span style="color: black;">[</span>i<span style="color: black;">+</span><span style="color: black;">1</span><span style="color: black;">:]</span>
<span style="color: black;"> 8 </span> p <span style="color: black;">=</span> <span style="color: black;">perm</span><span style="color: black;">(</span>s<span style="color: black;">)</span>
<span style="color: black;"> 9 </span> <span style="color: #7f0055; font-weight: bold;">for</span> x <span style="color: #7f0055; font-weight: bold;">in</span> p<span style="color: black;">:</span>
<span style="color: black;">10 </span> r<span style="color: black;">.</span><span style="color: black;">append</span><span style="color: black;">(</span>l<span style="color: black;">[</span>i<span style="color: black;">:</span>i<span style="color: black;">+</span><span style="color: black;">1</span><span style="color: black;">] +</span> x<span style="color: black;">)</span>
<span style="color: black;">11 </span> <span style="color: #7f0055; font-weight: bold;">return</span> r
</code></pre>
<br /></div>
Elexhttp://www.blogger.com/profile/04776810249062811247noreply@blogger.com1