Python

From YYpBD's MediaWiki

Jump to: navigation, search

목차

개요

범용 스크립트 언어이다.

Link

Built-in objects

Numbers

Strings

Lists

Dictionaries

Tuples

Files

String Manipulation

Baisc


>>> len('abc')         # Length: number items 
3
>>> 'abc' + 'def'      # Concatenation: a new string
'abcdef'
>>> 'Ni!' * 4          # Repitition: like "Ni!" + "Ni!" + ...
'Ni!Ni!Ni!Ni!'



>>> myjob = "hacker"
>>> for c in myjob: print c,       # Step through items.
...
h a c k e r
>>> "k" in myjob                   # 1 means true (found).
1
>>> "z" in myjob                   # 0 means false (not found).
0


Indexing and Slicing

>>> S = 'spam'
>>> S[0], S[-2]               # Indexing from front or end
('s', 'a')
>>> S[1:3], S[1:], S[:-1]     # Slicing: extract section
('pa', 'pam', 'spa')


String formatting codes

%s

문자열 출력

%r

문자열 자체를 그대로 출력한다.

repr()를 이용하도록 한다.

%c

character 출력

%d

Decimal 출력

%i

Integer 출력

%u

Unsigned 출력

%o

8진수로 출력

%x

Hex를 소문자로 출력

%X

Hex를 대문자로 출력

%e

부동 소수점 기수를 소문자로 표시

%E

부동 소수점 기수를 대문자로 표시

%f

부동 소수점 실수 표시

%g

부동 소수점을 소문자로 표시

%G

부동 소수점을 대문자로 표시

%%

% 문자 출력

>>> x = 1.23456789
>>> x
1.2345678899999999

>>> '%e | %f | %g' % (x, x, x)
'1.234568e+000 | 1.234568 | 1.23457'


Anonymous Functions: lambda

표현방식일뿐 문장이 아니다.


>>> f = lambda x, y, z: x + y + z
>>> f(2, 3, 4)
9

>>> x = (lambda a="fee", b="fie", c="foe": a + b + c)
>>> x("wee")
'weefiefoe'

맞춤검색