import keyword
print(keyword.kwlist)
print()
print(len(keyword.kwlist))
# python3에 추가됨 : 'False', 'None', 'True', 'nonlocal'
# python3에서는 제거됨 : 'exec', 'print'
#
# Python2 :
# ['and', 'as', 'assert', 'break', 'class',
# 'continue', 'def', 'del', 'elif', 'else',
# 'except', 'exec', 'finally', 'for', 'from',
# 'global', 'if', 'import', 'in', 'is', 'lambda',
# 'not', 'or', 'pass', 'print', 'raise', 'return',
# 'try', 'while', 'with', 'yield']
#
# 31
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield'] 33
print(abs(3))
print(abs(-3))
3 3
print(max(1, 2))
print(max([1, 2, 3]))
print(max("python"))
2 3 y
print(min(1, 2))
print(min([1, 2, 3]))
print(min("python"))
1 1 h
print(pow(2, 4))
print(pow(3, 3))
print(pow(2, -1))
16 27 0.5
print(chr(97))
print(chr(65))
print(chr(48))
a A 0
str(object='') -> str
print(str(3))
print(str([1, 2]))
3 [1, 2]
range(stop) -> range object
range(start, stop[, step]) -> range object
print(range(10))
print(range(3, 10))
print(range(3, 10, 2))
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# [3, 4, 5, 6, 7, 8, 9]
# [3, 5, 7, 9]
print()
print(list(range(10)))
print(list(range(3, 10)))
print(list(range(3, 10, 2)))
range(0, 10) range(3, 10) range(3, 10, 2) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [3, 4, 5, 6, 7, 8, 9] [3, 5, 7, 9]
print(type(-1))
print(type('abc'))
print(type([1, 2, 3]))
# <type 'int'>
# <type 'str'>
# <type 'list'>
# type 에서 class로 바뀜
<class 'int'> <class 'str'> <class 'list'>
파이썬 식별자는 변수, 함수, 모듈, 클래스 또는 객체를 식별하는데 사용되는 이름이다.
식별자의 조건
대소문자 구별함
식별자는 문자 A~Z 또는 a~z과 언더바(_)로 시작할 수 있다.
식별자 첫 시작을 제외하고 식별자 내에 숫자(0~9)를 사용할 수 있다.
특수문자 @, $, % 등은 식별자의 어느 자리에도 올 수 없다.
예를 들어 다음과 같은 것은 식별자가 될 수 없음
print(str(12345))
12345
str = 'abc'
print(str(12345))
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-27-9612f3c17921> in <module>() 1 str = 'abc' 2 ----> 3 print(str(12345)) TypeError: 'str' object is not callable
a = 1
print(a)
1
print(b)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-29-4851c8fca996> in <module>() ----> 1 print(b) NameError: name 'b' is not defined
b = 2
print(b)
del b
print(b)
2
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-30-2a13a1bf33fc> in <module>() 3 4 del b ----> 5 print(b) NameError: name 'b' is not defined
#이것은 주석입니다.
import sys #이것도 주석입니다.
a = 1
b = 3
if (a == 1) and \
(b == 3):
print('connected lines')
connected lines
a = 1
b = 3
if (a == 1) and (b == 3):
print('connected lines')
connected lines
a = 1
b = a
1 + 3 = a
File "<ipython-input-35-3aa35fdab4b7>", line 1 1 + 3 = a ^ SyntaxError: can't assign to operator
a = 1
a = a + 1
print(a)
2
c, d = 3, 4
print(c, d)
x = y = z = 0
print(x, y, z)
e = 3.5; f = 5.6
print(e, f)
3 4 0 0 0 3.5 5.6
e = 3.5; f = 5.6
e, f = f, e
print(e, f)
5.6 3.5
a = (b = c + d)
File "<ipython-input-39-79d71c957091>", line 1 a = (b = c + d) ^ SyntaxError: invalid syntax
a = 1
a += 4
print(a)
5
a = 10
a -= 3
print(a)
7
a = 10
a *= 2+3
print(a)
50
a = [1,2,3]
b = [10, a, 20]
c = ['x', a, 'y']
print(a)
print(b)
print(c)
[1, 2, 3] [10, [1, 2, 3], 20] ['x', [1, 2, 3], 'y']
a[1] = 1000
print(a)
print(b)
print(c)
[1, 1000, 3] [10, [1, 1000, 3], 20] ['x', [1, 1000, 3], 'y']
# raw_input 에서 input 으로 변경
# name = raw_input('name?')
name = input('name?')
name?홍길동
print(name)
홍길동
int(x=0) -> integer int(x, base=10) -> integer
print(int(3.14)) # For floating point numbers, this truncates towards zero. 0이 되는 방향으로 소숫점 이하를 버림
print(int()) # return 0 if no arguments are given. 그냥 int()는 0을 리턴
3 0
# k = int(raw_input('int : '))
k = input('str : ')
print(k)
print(type(k))
print()
k = int(input('int : '))
print(k)
print(type(k))
int : 10 10 <class 'str'> int : 10 10 <class 'int'>
# Python3에서 input()의 기능
# Read a string from standard input. The trailing newline is stripped.
? input
k = input('expr:')
print(k)
# expr:30 + 50
# 80
expr:30+50 30+50
print(4 + 5, 4 - 2)
9 2
print(1); print(2)
1 2
print(1),
print(2)
#1 2
print()
print(1)
print(2)
1 2 1 2
end=' '
를 사용 (디폴트는 end='\n'
)print(1, end=", ")
print(2)
print()
print(1, end=" ")
print(2)
1, 2 1 2
print(12 + 'spam')
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-60-5e105a439075> in <module>() ----> 1 print(12 + 'spam') TypeError: unsupported operand type(s) for +: 'int' and 'str'
print('12' + 'spam')
12spam
참고 문헌: 파이썬(열혈강의)(개정판 VER.2), 이강성, FreeLec, 2005년 8월 29일