�^�Ɖ��Z�q

Python�v���O�����̍\��

  1. �v���O�����̓��W���[������\�������
  2. ���W���[���͕��̂Ȃ��
  3. ���̓I�u�W�F�N�g�𐶐����C��������

�g�ݍ��݌^

��

�����E�����O�����E���������E���f�� ���l�萔�ł� 10�i�C8�i�C16�i���\��������

�g�ݍ��݃c�[��

���Z�q�ƗD�揇��

���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������ւ̕ϊ�

���l���Z�̎���

>>> 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

�����񉉎Z�̎���

>>> 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!'
�����񐮌`�ϊ��w��(�ڂ����� man printf)
%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 ��������
%% %

������ɑ΂���c�[��

string, __bultins__.str, re, ...
>>> 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)
�o�b�N�X���b�V��(\) �ɂ��G�X�P�[�v
\���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.

���X�g

Python �̃��X�g��
>>> 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]

�f�B�N�V���i��

>>> 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]

�^�v��

�u�X�V�s�\�v�ł��邱�Ƃ������΃��X�g�Ƃقړ����D

�t�@�C��

>>> 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�̈�ʓI����

�^�J�e�S��

�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�\

��ʐ�

���L���t�@�����X

>>> 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}

�召��r�C���l�� ����� �^�U

== ���Z�q�� ���l�����e�X�g����
is ���Z�q�� �I�u�W�F�N�g�̓��ꐫ���e�X�g����

�^�̊K�w��

�g�ݍ��݌^�̗��Ƃ���

���K���

  1. Python ���N�����C�Θb�I�Ɉȉ��̎�����͂��C���̌��ʂɂ‚��Đ������Ȃ����D
    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]
    	
  2. ���̂悤�ȃ^�v�����g��������������Ă݂悤�D
    x = 'spam'
    y = 'eggs'
    x, y = y, x
    print x, y
    
    ���̂悤�Ȍ��ʁi�����j���������邽�߂ɁCC�� Pascal �ł͂ǂ̂悤�ɏ����Ȃ���΂Ȃ�Ȃ����C�v���o���Ă݂悤�D
  3. �ȉ��̗���Q�l�ɁC�u�����v��\���f�[�^�\�����쐬���Ȃ����D
    >>> 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')
    
  4. myfile.txt �Ƃ������̏o�̓t�@�C����V�K�ɍ쐬���C����ɕ����� "Hello file world!" ���������ނ悤�� �X�N���v�g�������Ȃ����D���ɁCmyfile.txt ���I�[�v�����Ă��̓��e��ǂݏo���ĕ\������悤�ȁC �ʂ̃X�N���v�g�������Ȃ����D�����̂Q�‚̃X�N���v�g�� UNIX �̃R�}���h���C�� ������s���C�V�����t�@�C�����쐬����邱�Ƃ��m�F���Ȃ����D