파이썬 3 기초 강좌 두번째 시간, 파이썬을 "Tutorial" 따라 코딩해보자!
수행했던 내용을 그대로 갈무리 한거다...
그러면서 설명을 간단히 곁들여볼까 한다.
그냥 해보시라...
1. if 문...
Type "copyright", "credits" or "license()" for more information.
>>> the_world_is_flat = 1
>>> if the_world_is_flat:
print("Be careful not to fall off!")
Be careful not to fall off!
위엣것을 설명하자면... (필요할지 모르겠지만)
the_world_is_flat 에다가 1을 세팅하는 것이다.
그리고는 if the_world_is_flat 이면은... 아래의 print를 수행하라는... 극히 기본적인....
그래서 그러니까, 아래에... Be careful not to fall off! 라는 문구가 프린트된 것이다.
print("Falled off!!")
not 이냐고 물었더니... 아무 반응 없었다. ㅋㅋㅋ
2. 계산기 기능...
2.1 단순 산수...
>>> 2+2
4
>>> # This is a comment
2+2
4
>>> 2+2 # and a comment on the same line as code
4
>>> (50-5*6)/4
5.0
>>> 8/5 # Fractions aren't lost when dividing integers
1.6
( / 와 // 의 차이점을 설명하시는 것이겠다...)
>>> 7//-3 # Integer division returns the floor
-3
>>> 7//3
2
>>> 7.//-3
-3.0
>>> 7.//-3.
-3.0
(혹시나.... -2.3 정도가 나올 수 있을까? 하는 심정으로 해봤으나... 아니다...)
>>> x=y=z=0
>>> x
0
>>> y
0
>>> z
0
(한방에 변수에다가 initial 시키는 거겠다.)
>>> # try to access an undefined variable
>>> n
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
n
NameError: name 'n' is not defined
(변수에 아무것도 세팅하지 않고, 그냥 실행하면 에러를 맛보게 된다.
이건 아니잖아... 하면서...)
>>> 7/3
2.3333333333333335
>>> 7/-3
-2.3333333333333335
>>> 7.0/2
3.5
2.2 복소수였던가.... 그놈이다...
>>> 1j * 1J
(-1+0j)
>>> 1j * complex(0,1)
(-1+0j)
>>> 3+1j*3
(3+3j)
>>> (3+1j)*3
(9+3j)
>>> (1+2j)/(1+1j)
(1.5+0.5j)
>>> a = 1.5 + 0.5j
>>> a.real #실수부겠지...
1.5
>>> a.imag #허수부겠지...
0.5
>>>
>>> a=3.0+4.0j
>>> float(a)
Traceback (most recent call last):
File "<pyshell#33>", line 1, in <module>
float(a)
TypeError: can't convert complex to float
(복소수를 float로 바꿀 수 없다는 것이겠지...)
>>> a.real
3.0
>>> a.imag
4.0
>>> abs(a)
5.0
>>> tax = 12.5 /100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> price + tax
100.625
>>> _
100.625
>>> round(_,2)
100.62
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_,2)
113.06
>>> round(113.0625,2)
113.06
>>> price + 12.5625
113.0625
( _ 가 뭔지 한참 헤맸다. 그러나... 바로 직전의 계산결과값이란 걸 알게됐다...)
2.3 문자열 처리...
>>> 'spam eggs'
'spam eggs'
>>> 'doesn\'t' # single quotation 이겠지...
"doesn't"
>>> "doesn't"
"doesn't"
>>> '"Yes," he said.'
'"Yes," he said.'
>>> "\"Yes,\" he said." # double quotations 이겠지...
'"Yes," he said.'
>>> '"Isn\'t," she said.'
'"Isn\'t," she said.'
>>> hello = "This is a rather long string containing\n\ # 줄바꿈 이겠지...
several lines of text just as you would do in C.\n\
Note that whithespace at the beginning of the line is\
significant."
>>> print(hello)
This is a rather long string containing
several lines of text just as you would do in C.
Note that whithespace at the beginning of the line is significant.
>>> print("""\ # triple quotations 은 줄바꿈이나 탭마져도 그대로 살려주는 기능 되시겠다....
Usage : thingy [OPTIONS]
-h Display this usage message
-H hlostname Hostname to connect to
""")
Usage : thingy [OPTIONS]
-h Display this usage message
-H hlostname Hostname to connect to
>>> hello = r"This is a rather long string continaing\n\
several lines of text much as you yould do in C."
>>> print(hello)
This is a rather long string continaing\n\
several lines of text much as you yould do in C.
( "raw" 문자열은 있는 그대로 보여주게된다. 그냥 문자로만 인식하게 하는 거겠다.)
이렇게 변수에 넣어서 print 해도 잘 된다는 걸 보여준거겠다.
물론 차이는 그냥... 그대로 들어갔고, 그대로 나왔다는 것이 차이점이겠다...)
아래부터는 문자열 지지고 볶는 부분이다.
tutorial에 나와있는 것보다 몇개 더 해봤다.
이유는 잘 이해가 되질 않아서였다.
그래서 좀 더 해보니... 이해가 좀 되었다.
천천히 들여다보거나, 직접 해보게되면 이해되는 부분 되시겠다...(모르면 pass....)
>>> word = 'Help' + 'A'
>>> word
'HelpA'
>>> '<' + word*5 + '>'
'<HelpAHelpAHelpAHelpAHelpA>'
>>> '<' + (word-1)*5 + '>'
Traceback (most recent call last):
File "<pyshell#72>", line 1, in <module>
'<' + (word-1)*5 + '>'
TypeError: unsupported operand type(s) for -: 'str' and 'int'
>>> '<' + (word-'A')*5 + '>'
Traceback (most recent call last):
File "<pyshell#73>", line 1, in <module>
'<' + (word-'A')*5 + '>'
TypeError: unsupported operand type(s) for -: 'str' and 'str'
>>> '<' + word[0:4]*5 + '>'
'<HelpHelpHelpHelpHelp>'
>>> '<' + word[0:4] + ' ' *5 + '>'
'<Help >'
>>> '<' + (word[0:4] + ' ') *5 + '>'
'<Help Help Help Help Help >'
>>> word[4]
'A'
>>> word[0:6]
'HelpA'
>>> word[2:4
]
'lp'
>>> word[:2]
'He'
>>> word[2:]
'lpA'
>>> word[2] = 'x'
Traceback (most recent call last):
File "<pyshell#83>", line 1, in <module>
word[2] = 'x'
TypeError: 'str' object does not support item assignment
>>> 'x'+word[1:]
'xelpA'
>>> 'Splat'+word[4]
'SplatA'
>>> word[:2]+word[2:]
'HelpA'
>>> word[0]
'H'
>>> s='supercalifragilisticexpialidocious'
>>> len(s)
34
>>> "Äpfel".encode('utf-8')
b'\xc3\x84pfel'
>>> b'\xc3\x84pfel'.encode('
SyntaxError: EOL while scanning string literal
>>> a = ['spam','eggs',100,1234]
>>> a
['spam', 'eggs', 100, 1234]
>>> a[0]
'spam'
>>> a[1]
'eggs'
>>> a[2]
100
>>> a[3]
1234
>>> a[4]
Traceback (most recent call last):
File "<pyshell#98>", line 1, in <module>
a[4]
IndexError: list index out of range
>>> a[-2]
100
>>> a[-1]
1234
>>> a[-3]
'eggs'
>>> a[-4]
'spam'
>>> a[-5]
Traceback (most recent call last):
File "<pyshell#103>", line 1, in <module>
a[-5]
IndexError: list index out of range
>>> a[1:-1]
['eggs', 100]
>>> a[:2] + ['bacon',2*2]
['spam', 'eggs', 'bacon', 4]
>>> 3*a[:3]+['Boo!
SyntaxError: EOL while scanning string literal
>>> 3*a[:3]+['Boo!']
['spam', 'eggs', 100, 'spam', 'eggs', 100, 'spam', 'eggs', 100, 'Boo!']
>>> a
['spam', 'eggs', 100, 1234]
>>> a[2] = a[2] + 23
>>> a
['spam', 'eggs', 123, 1234]
>>> a[2] =a[2] - 24
>>> a
['spam', 'eggs', 99, 1234]
>>> a[2] = a[2] + 1
>>> a
['spam', 'eggs', 100, 1234]
>>> a[2] = a[2] + 23
>>> a
['spam', 'eggs', 123, 1234]
>>> a[0:2] = [1,12]
a
>>>
>>> a
[1, 12, 123, 1234]
>>> a[0:2]=[]
>>> a
[123, 1234]
>>> a[1:1] = ['bletch','xyzzy']
>>> a
[123, 'bletch', 'xyzzy', 1234]
>>> a[:0]=a
>>> a
[123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]
>>> a[:]=[]
>>> a
[]
>>> a = ['a','b','c','d','e']
>>> a
['a', 'b', 'c', 'd', 'e']
>>> len(a)
5
>>> a[-1:]=['k']
>>> a
['a', 'b', 'c', 'd', 'k']
>>> a[-1:]=['e','f']
>>> a
['a', 'b', 'c', 'd', 'e', 'f']
>>> a=a[:]
>>> a
['a', 'b', 'c', 'd', 'e', 'f']
>>> a[:0]=['go']
>>> a
['go', 'a', 'b', 'c', 'd', 'e', 'f']
>>> len(a)
7
>>> q=[2,3]
>>> p=[1,q,4]
>>> len(p)
3
>>> p[1]
[2, 3]
>>> p[0]
1
>>> p[1][0]
2
>>> p[1][1]
3
>>> p[1].append('xtra')
>>> p
[1, [2, 3, 'xtra'], 4]
>>> q
[2, 3, 'xtra']
※ 피보나치 수열 되시겠다.
너무 재밌는 부분 중의 하나였다.
간단하면서도, 산수의 수열을 순식간에 표현할 수 있다니...
퍼헐~
>>> a,b = 0,1
>>> a
0
>>> b
1
>>> while b < 10:
print(b)
a,b=b,a+b
1
1
2
3
5
8
>>> while b < 100:
print(b)
a,b=b,a+b
13
21
34
55
89
>>> a,b=0,1
>>> while b < 100:
print(b)
a,b=b,a+b
1
1
2
3
5
8
13
21
34
55
89
>>> a,b=0,1
>>> while b < 601:
print(b)
a,b=b,a+b
1
1
2
3
5
8
13
21
34
55
89
144
233
377
>>> a,b=0,1
>>> while b < 611:
print(b)
a,b=b,a+b
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
피보나치 수열을 좀 더 쉽게 보고 싶었다.
제대로 보고싶었다.
하지만 C, PYTHON이 짧아서 잠시 삽질한다....
>>> a,b=0,1
>>> while b < 1000:
print(++1,b)
a,b=b,a+b
1 1
1 1
1 2
1 3
1 5
1 8
1 13
1 21
1 34
1 55
1 89
1 144
1 233
1 377
1 610
1 987
>>> a,b=0,1
>>> i=0
>>> while b < 1000:
print(++i,b)
a,b=b,a+b
0 1
0 1
0 2
0 3
0 5
0 8
0 13
0 21
0 34
0 55
0 89
0 144
0 233
0 377
0 610
0 987
>>> a,b=0,1
>>> i=1
>>> while b < 1000:
print(i,b)
a,b=b,a+b
++i
1 1
1
1 1
1
1 2
1
1 3
1
1 5
1
1 8
1
1 13
1
1 21
1
1 34
1
1 55
1
1 89
1
1 144
1
1 233
1
1 377
1
1 610
1
1 987
1
이제 제대로 모양을 갖췄다. 크헐헐...
>>> a,b=0,1
>>> i=1
>>> while b < 1000:
print(i,b)
a,b=b,a+b
i=i+1
1 1
2 1
3 2
4 3
5 5
6 8
7 13
8 21
9 34
10 55
11 89
12 144
13 233
14 377
15 610
16 987
요게 완성판이다....
근데.... 여기서는 탭이 제대로 안보이게 되네....
여하튼 파이썬에서는 잘 나왔다. 에잇... 캡쳐때려야겠다....
>>> a,b=0,1
>>> i=1
>>> while b < 1000:
print(i,b,sep='\t')
a,b=b,a+b
i=i+1
1 1
2 1
3 2
4 3
5 5
6 8
7 13
8 21
9 34
10 55
11 89
12 144
13 233
14 377
15 610
16 987
>>>
<the Fibonacci series>
ㅎㅎㅎ 일단 강좌 두번째를 성공적으로 마무리한 것 같다.
실력은 없어도...
처음 하시는 분들과 동등한 수준(?) 이므로...
보시면서 바로~ 이해되실 것 같다.
사람들이 이래서 파이썬, 파이썬 하는구나 생각한다.
생각보다 쉽기 때문이다. ^^;