���Z�q | ���� |
---|---|
X or y, lambda�����F�� | �_���a�C������ |
X and y | �_���� |
not x | �_���ے� |
<, <=, >, >=, ==, <>, !=, is, is not, in, not in | ��r�C ���ꐫ�C �����o�V�b�v�e�X�g |
x | y | �r�b�g���̘_���a |
x & y | �r�b�g���̘_���� |
x << y, x >> y | x�����܂��͉E��y�r�b�g�V�t�g |
x + y, x - y | ���Z/�A�� ���Z |
x * y, x / y, x % y | ��Z/�J��Ԃ�, ���Z�C��]/���� |
-x, +x, ~x | �P���̕������]�C�����C�r�b�g���̔��] |
x[i], x[i:j], x.y, x(...) | �Y���t���C�X���C�X�C�C���C���Ăяo�� |
(...), [...], {...}, `...` | �^�v���C���X�g�C�f�B�N�V���i���C������ւ̕ϊ� |
>>> a = 3 >>> b = 4 >>> b / 2 + a 5 >>> b / (2.0 + a) 0.80000000000000004 >>> x = 1 >>> x << 2 4 >>> x | 2 3 >>> x & 1 1 >>> 9999999999999999 + 1 OverflowError: integer literal too large >>> 9999999999999999L + 1 10000000000000000L >>> 1j * 1J (-1+0j) >>> 2 + 1j * 3 (2+3j) >>> (2 + 1j) * 3 (6+3j) >>> import math >>> math.pi 3.1415926535897931 >>> sin(2*pi) Traceback (innermost last): File "", line 1, in ? sin(2*pi) NameError: There is no variable named 'sin' >>> math.sin(2*math.pi) -2.4492127076447545e-016 >>> from math import * >>> sin(2*pi) -2.4492127076447545e-016 >>> abs(-42), 2**4, pow(2, 4) (42, 16, 16.0) >>>
���� | ���� |
---|---|
s1 = '' | ���� |
s2 = "spam's" | �_�u���N�H�[�g |
block = """...""" | �g���v���N�H�[�g�u���b�N |
s1 + s1, s2 * 3 | �A���C ���� |
s2[i], s2[i:j], len(s2) | �Y�����t���C �X���C�X ���� |
"a %s parrot" % 'dead' | ������ |
for x in s2, 'm' in s2 | �J��Ԃ��C �����o�V�b�v |
�����`�ϊ��w��(�ڂ����� man printf)>>> len('abc') 3 >>> 'abc' + 'def' 'abcdef' >>> 'Ni!' * 4 'Ni!Ni!Ni!Ni!' >>> myjob = "hacker" >>> for c in myjob: print c, h a c k e r >>> "k" in myjob 1 >>> S = 'spam' >>> S[0], S[-2] ('s', 'a') >>> S[1:3], S[1:], S[:-1] ('pa', 'pam', 'spa') >>> S[0] = 'x' Traceback (innermost last): File "", line 1, in ? S[0] = 'x' TypeError: object doesn't support item assignment >>> S = 'x' + S[1:] >>> S 'xpam' >>> S = 'spam' >>> S = S + 'Spam!' >>> S 'spamSpam!' >>> S = S[:4] + 'Burger' + S[-1] >>> S 'spamBurger!' >>> 'That is %d %s bird!' % (1, 'dead') 'That is 1 dead bird!'
%s | ������ |
%c | ���� |
%d | 10�i��(����) |
%i | ���� |
%u | ��������(����) |
%o | 8�i���� |
%x | 16�i���� |
%X | 16�i����(�啶��) |
%e,%E | ��������(��1.25e-5) |
%f | ����(0.0000125) |
%g,%G | �������� |
%% | % |
�o�b�N�X���b�V��(\) �ɂ��G�X�P�[�v>>> import string >>> S = 'spammify' >>> string.upper(S) 'SPAMMIFY' >>> string.find(S,'mm') 3 >>> string.atoi('42'), `42` (42, '42') >>> string.join(string.split(S, 'mm'), 'XX') 'spaXXify' >>> S.upper() 'SPAMMIFY' >>> S.find('mm') 3 >>> import re >>> re.split('mm',S) ['spa', 'ify'] >>> re.sub('mm','XX',S) 'spaXXify' >>> string.sub('mm','XX',S)
\���s | �s�̌p�� |
\\ | �o�b�N�X���b�V�� |
\' | �V���O���N�I�[�g |
\" | �_�u���N�I�[�g |
\a | �x�� |
\b | �o�b�N�X�y�[�X |
\000 | null |
\n | ���s |
\v | �����^�u |
\t | �����^�u |
\r | ���A(�L�����W���^�[��) |
\f | ���y�[�W |
\0XX | 8�i�R�[�h XX |
\xXX | 16�i�R�[�h XX |
\(���̑�) | ���̂܂� |
>>> big = """This is a multi-line block of text; Python puts an end-of-line marker after each line.""" >>> big 'This is\012a multi-line block\012of text; Python puts\012an end-of-line marker\012after each line.' >>> print big This is a multi-line block of text; Python puts an end-of-line marker after each line.
>>> L1 = [] >>> L2 = [0, 1, 2, 3] >>> L3 = ['abc', ['def', 'ghi']] >>> L2[2] 2 >>> L3[1][1] 'ghi' >>> len(L2) 4 >>> for x in L2: print x, (���s) 0 1 2 3 >>> L2.append(4) >>> L2 [0, 1, 2, 3, 4] >>> L2.reverse() >>> L2 [4, 3, 2, 1, 0] >>> L2.sort() >>> L2 [0, 1, 2, 3, 4]
>>> d1 = {} >>> d2 = {'spam': 2, 'eggs': 3} >>> d2['spam'] 2 >>> d3 = {'food': {'ham': 1, 'egg': 2}} >>> d3['food']['egg'] 2 >>> d2.has_key('eggs') 1 >>> d2.has_key('ham') 0 >>> d2.keys() ['spam', 'eggs'] >>> d2.values() [2, 3]
>>> myfile = open('myfile','w') >>> myfile.write('hello text file\n') >>> myfile.close() >>> mf = open('myfile','r') >>> mf.readline() 'hello text file\012' >>> mf.readline() ''
�I�u�W�F�N�g�^ | �J�e�S�� | �X�V�\�� |
---|---|---|
�� | ���l | No |
������ | �V�[�P���X | No |
���X�g | �V�[�P���X | Yes |
�f�B�N�V���i�� | �ʑ��^ | Yes |
�^�v�� | �V�[�P���X | No |
�t�@�C�� | �g�� | �K�p�s�\ |
>>> X = [1, 2, 3] >>> L = ['a', X, 'b'] >>> D = {'x':X, 'y':2} >>> X [1, 2, 3] >>> L ['a', [1, 2, 3], 'b'] >>> D {'x': [1, 2, 3], 'y': 2} >>> X[1] = 'surprise' >>> X [1, 'surprise', 3] >>> L ['a', [1, 'surprise', 3], 'b'] >>> D {'x': [1, 'surprise', 3], 'y': 2}
2 ** 16 2 / 5, 2 / 5.0 "spam" + "eggs" S = "ham" "eggs " + S S * 5 S[:0] "green %s and %s" % ("eggs", S) ('x',)[0] ('x', 'y')[1] L = [1,2,3] + [4,5,6] L, L[:], L[:0], L[-2], L[-2:] ([1,2,3] + [4,5,6])[2:4] [L[2], L[3]] L.reverse(); L L.sort(); L L.index(4) {'a':1, 'b':2}['b'] D = {'x':1, 'y':2, 'z':3} D['w'] = 0 D['x'] + D['w'] D[(1,2,3)] = 4 D.keys(), D.values(), D.has_key((1,2,3)) [[]], ["",[],(),{},None]
���̂悤�Ȍ��ʁi�����j���������邽�߂ɁCC�� Pascal �ł͂ǂ̂悤�ɏ����Ȃ���Ȃ�Ȃ����C�v���o���Ă݂悤�Dx = 'spam' y = 'eggs' x, y = y, x print x, y
>>> me = { 'name': {'first': 'Tokio', 'last': 'Kikuchi'}, \ 'age': 50, 'email': '[email protected]'} >>> me {'email': '[email protected]', 'age': 50, 'name': {'first': 'Tokio', 'last': 'Kikuchi'}} >>> me['name'] {'first': 'Tokio', 'last': 'Kikuchi'} >>> me['email'] '[email protected]' >>> me['name']['first'] , me['name']['last'] ('Tokio', 'Kikuchi')