10 åã§å¦ã¶ Python ã®ãã¥ã¼ããªã¢ã«
"Stavros' Stuff" ã¨ãããµã¤ãã«æ²è¼ããã¦ãã "Tutorial - Learn Python in 10 minutes" ã¨ããè¨äºã®å¦è¨³ 翻訳ã§ã。ãããã Python ãã¯ããããã¨ããæ¹åãã§ã。ã¡ãã£ã¨åãããããããã« ? è£è¶³æ
å ±ãä»å ãã¡ãã£ã¦ã¾ã。ã¤ãã§ã«ç§ã Python ãæ¸ãã¨ãã«æèãã¦ãããã¨ãä»å ãã¡ãã£ã¦ã¾ã。ä»å æ
å ±ãééã£ã¦ããããããªãã ><
ããããã£
Python ã¯å¼·ãåä»ã (strongly typed language)、åçåä»ã (dynamically typed)、æé»çåä»ã (implicitly typed)、大æåå°æåãåºå¥ (case sensitive)、ãªãã¸ã§ã¯ãæå (object-oriented) ã§ã。
- å¼·ãåä»ãè¨èªã®ç¹å¾´: å¦çãæ¼ç®ãééã£ãåã®å¼æ°ãåãã¨ããã¨ãåºæ¥ãªã (C è¨èªã¨ãã¯å¼±ãåä»ã)。å®å ¨ã§ãã。
- åçåä»ãè¨èªã®ç¹å¾´: åã®æ¤æ»ãå®è¡æã«è¡ããã¨ãå¤ã。ç°¡åã«è¨ãã¨、åã¯å¤æ°ã§ã¯ãªãå¤ã«ä»ãããã¾ã。
- æé»åä»ãè¨èªã®ç¹å¾´: æ示çã«æå®ããªãã¦ãã³ã³ãã¤ã©ãèªåçã«åå¤æãå¤æãã¦ããã
ãã«ã
Python ã®ãã«ãã¯å¸¸ã«ã¤ã³ã¿ããªã¿ããå©ç¨ããäºãã§ãã¾ã。ãã、ãªãã¸ã§ã¯ãã®åä½ã«ã¤ãã¦èª¿ã¹ããå ´åã¯、help(<object>)
ãå¼ã³åºãã¾ããã。ã¾ã、dir(<object>)
ã§ãªãã¸ã§ã¯ãã®ã¡ã½ãããå¤æ°çã®ã¡ã³ãä¸è¦§ãåç
§ããã、<object>.__doc__
ã§ããã¥ã¡ã³ããåç
§ããã®ã便å©ã§ãã。
>>> help(5) Help on int object: class int(object) ... >>> dir(5) ['__abs__', '__add__', ...] >>> print abs.__doc__ abs(number) -> number Return the absolute value of the argument.
è足ã§ãã、å人çã«ã¯ IPython 使ã£ã¦ã¾ã。<object>?
<object>??
ã§ããã¥ã¡ã³ããå«ãæ§ã
ãªæ
å ±ãåç
§ã§ãã¡ããã¾ã。
æ§æ (ã·ã³ã¿ãã¯ã¹)
Python ã¯ã¹ãã¼ãã¡ã³ãã®çµç«¯æåããªã、ãããã¯ãã¤ã³ãã³ãã§æå®ãã¾ã。ãããã¯ã®å§ã¾ãã«ã¤ã³ãã³ãã、çµããã«ã¤ã³ãã³ããæ»ãã¾ã。ã³ãã³ (:
) ã§çµäºããã¹ãã¼ãã¡ã³ã以ä¸ãã¤ã³ãã³ããã¾ã。ã³ã¡ã³ãã¯、1 è¡ãªãã·ã£ã¼ã #
ã®å¾ã«ç¶ã、è¤æ°è¡ã«ãããå ´åã¯è¤æ°è¡ã®æåå """ã¹ãã \nã¨ãã°"""
ã«ãã¾ã。å¤ã®å²ãå½ã¦ (å³å¯ã«ã¯ãªãã¸ã§ã¯ãã¸ã®ååä»ã) ã¯çè¨å· "=
" ã使ã、ç価æ¯è¼ã¯ 2 ã¤ã®çè¨å· "==
" ã使ãã¾ã。"+=
" ã "-=
" æ¼ç®åã§、æ¼ç®åã®å³ã«è¨è¼ããå¤ã§ã¤ã³ã¯ãªã¡ã³ãããã¯ãªã¡ã³ããããã¨ãã§ãã¾ã。ããã¯æååãå«ãå¤ãã®åã§å©ç¨ã§ãã¾ã。ã¾ã、è¤æ°ã®å¤æ°ã 1 è¡ã§å®ç¾©ãããã¨ãã§ãã¾ã。
>>> spam = 3 >>> spam 3 >>> spam += 2 >>> spam 5 >>> spam -= 1 >>> spam 4 # ä¸è¡ã®ã³ã¡ã³ã """This is a multiline comment. The following lines concatenate the two strings.""" u"""ããã¯è¤æ°è¡ã³ã¡ã³ãã§ã。 æ¥æ¬èªã§ã®ã³ã¡ã³ãã¯ã¦ãã³ã¼ãã§ã³ã¡ã³ãããäºããå§ããã¾ã。 ãã¨ã§ __doc__ ã¨ãã§æååãããã«è¡¨ç¤ºãããã§ãã :D ä¸ã®è¡ã§ã¯、2 ã¤ã®æååãé£çµãã¦ãã¾ã。""" >>> egg = "Hello" >>> egg += " world!" >>> print egg Hello world! # ã¦ãã³ã¼ãæååã ã£ã¦ >>> ja = u"ããã«ã¡ã" >>> ja += u"ä¸ç!" >>> print ja ããã«ã¡ãä¸ç! # 1 è¡ã§å¤æ°ã®äº¤æã ã£ã¦ã§ãã¡ããã¾ã。 # å¤ãå²ãå½ã¦ããã¦ããããã§ã¯ãªãã®ã§、å¼·ãåä»ãã«ããå¶éãåãã¾ãã。 # æ°ãããªãã¸ã§ã¯ããååã«ç´ä»ãããã¾ã。 >>> spam, egg = egg, spam >>> print spam Hello world! >>> egg 4
ãã¼ã¿å
Python ã§å©ç¨ã§ãããã¼ã¿æ§é ã¯ãªã¹ã (list)、ã¿ãã« (tuple)、è¾æ¸ (dict)、ã»ãã (set) (Python 2.5 以å㯠sets ã¢ã¸ã¥ã¼ã«ã¨ãã¦æä¾) ã§ã。ãªã¹ãã¯ä¸æ¬¡å é å (ãã¡ãããªã¹ãå ã«ãªã¹ããå«ãããã¨ãã§ãã¾ã)、è¾æ¸ã¯é£æ³é å (ãããã¯ããã·ã¥ãã¼ãã«)、ã¿ãã«ã¯ã¤ãã¥ã¼ã¿ãã« (ç¶æ ãå¤æ´ãããã¨ãã§ããªã) ä¸æ¬¡å é åã§ã。Python ã®é åã¯åã«ããå¶éããªããã、æ°å¤ãæååãªã©ããªã¹ã/ã¿ãã«/è¾æ¸/ã»ããå ã«æ··å¨ããããã¨ãã§ãã¾ã。ãã¹ã¦ã®é ååã§è¦ç´ ã®ã¤ã³ããã¯ã¹ã¯ 0 ããå§ã¾ãã¾ã。ã¤ã³ããã¯ã¹ã«è² ã®æ°ã®ãæå®ããã¨é åã®çµããããæ°ãããã、æå¾ã®è¦ç´ ã®ã¤ã³ããã¯ã¹ã¯ -1 ã¨ãªãã¾ã。é¢æ°ãå¤æ°ã¨ãããã¨ãã§ãã¾ã。Python ã¯ãã¡ã¼ã¹ãã¯ã©ã¹ãªãã¸ã§ã¯ãã§ã。ããã¯æ¸å·å çã翻訳ããã¦ãã "The history of Python" ã®ããã°ãè¦ãã¨å¹¸ãã«ãªãã¾ã。
>>> sample = [1, [u"æ¥æ¬èª", "list"], ("a", "tuple")] >>> mylist = ["List item 1", 2, 3.14] >>> mylist[0] = "List item 1 again" >>> mylist[-1] = 3.14 >>> mydict = {"Key 1": "Value 1", 2: 3, "pi": 3.14} >>> mydict["pi"] = 3.15 >>> mytuple = (1, 2, 3) >>> myfunction = len >>> print myfunction(mylist) 3
ã³ãã³ (:) ã使ã£ã¦é åå ã®æå®ããç¯å²ã«ã¢ã¯ã»ã¹ãããã¨ãã§ãã¾ã。ã¤ã³ããã¯ã¹ã®éå§ãçç¥ããã¨æåã®è¦ç´ ãã、ã¤ã³ããã¯ã¹ã®çµäºãçç¥ããã¨æå¾ã®è¦ç´ ã¾ã§ã¨ãªãã¾ã。è² ã®ã¤ã³ããã¯ã¹ãæå®ããã¨、æå¾ã®ã¢ã¤ãã (-1 ãæå¾ã®ã¢ã¤ãã ã®ã¤ã³ããã¯ã¹) ããæ°ãã¾ã。
ã©ãã§ã©ããããã¼ã¿åã使ããã¨ããã®ã¯é常ã«éè¦ã§ã。åãã¼ã¿åã®ç¹å¾´ããã£ããææ¡ãã¦ããã¨å¹¸ãã«ãªãã¾ã。é åå ã§ã¦ãã¼ã¯ã«ãã¼ã¿ãæããããå ´åã¯ã»ãã、é çªãä¿è¨¼ãããã®ã§ããã°ãªã¹ã!ãªã©ãªã©
>>> mylist = ["List item 1", 2, 3.14] >>> print mylist[:] ['List item 1', 2, 3.1400000000000001] >>> print mylist[0:2] ['List item 1', 2] >>> print mylist[-3:-1] ['List item 1', 2] >>> print mylist[1:] [2, 3.14]
ã¡ãªã¿ã« array[:]
ã§é
åã® ã·ã£ãã¼ã³ãã¼ ãã£ã¼ãã³ãã¼ ãã§ãã¾ã。å
ã®é
åãä¿æããã¾ã¾、é
åãæä½ããæã«ä½¿ãã¾ã。
æåå
æååã¯ã·ã³ã°ã«ã¯ã©ã¼ãã¼ã·ã§ã³ãããã«ã¯ã©ã¼ãã¼ã·ã§ã³ã®ä½ããã§å®ç¾©ã§ã、"He said 'Hello'"
ã®ããã«ã¯ã©ã¼ãã¼ã·ã§ã³å
ã§ç°ãªãã¯ã©ã¼ãã¼ã·ã§ã³ã使ç¨ãããã¨ãã§ãã¾ã。è¤æ°è¡ã«ã¾ãããæåå㯠3 ã¤ã®ããã«ã¯ã©ã¼ãã¼ã·ã§ã³ (ãããã¯ã·ã³ã°ã«ã¯ã©ã¼ãã¼ã·ã§ã³) ã§å²ã¿ã¾ã。Python ã¯ã¦ãã³ã¼ãããµãã¼ããã¦ãã、u"ããã¯ã¦ãã³ã¼ãæå"
ã¨å®ç¾©ã§ãã¾ã。æååãã©ã¼ãããæä½ã¯ % (ã¢ã¸ã¥ã) æ¼ç®åã¨ã¿ãã«ã使ãã¾ã。å %s ã %d çã、å·¦ããå³ã¸ã¿ãã«å
ã®åã¢ã¤ãã ã«ç½®ãæãããã¾ã。ã¿ãã«ã®å¤ããã«è¾æ¸ã使ããã¨ãåºæ¥ã¾ã。
>>> print u"åå: %s\næ°å: %d\nå°æ°: %f\næåå: %s" % (myclass.name, 3, 3.14, 3 * "-") åå: Poromenos æ°å: 3 å°æ°: 3.140000 æåå: --- multiline = """This is a multiline string.""" # WARNING: Watch out for the trailing s in "%(key)s". >>> print "This %(verb)s a %(noun)s." % {"noun": "test", "verb": "is"} This is a test.
ç¾ããã³ã¼ããæ¸ãããã«、è¤æ°è¡ã«ã¾ãããæååã® 3 ã¤ã®å¼ç¨ç¬¦ã¯ããã«ã¯ã©ã¼ãã¼ã·ã§ã³ã、ãã°ãã¡ãã»ã¼ã¸ãªã©ã¦ã¼ã¶ã«è¡¨ç¤ºãéç¥ããæååãããã«ã¯ã©ã¼ãã¼ã·ã§ã³ã、ãã®ä»ã³ã¼ãå ã§å©ç¨ããè¨å®æ å ± (URL ã DBæ¥ç¶æ å ±ç) ãå®æ°ã¯ã·ã³ã°ã«ã¯ã©ã¼ãã¼ã·ã§ã³ã使ãã¾ããã。ã£ã¦ããããã決ãã¦ããã¨、å¾ã§ã³ã¼ãã追ããããã§ã。éç¨ãã人ããã°ãã¡ãã»ã¼ã¸ã追å 、å¤æ´ãããã®ã§ããã°ããã«ã¯ã©ã¼ãã追ãããã、ãã¸ãã¯ãç°å¢ãå¤æ´ããå ´åã¯ã·ã³ã°ã«ã¯ã©ã¼ãã追ãããã、ã¨ãã§ãã¾ã。ã¾ã、ç¾ããã³ã¼ããæ¸ãããã«è¤æ°è¡æååã¯éæ示çæååé£çµã使ãã¨ãããã¯ããã¯ãããã¾ãã。
ããã¼å¶å¾¡æ§æ
ããã¼å¶å¾¡æ§æ㯠if
、for
、while
ã§ã。switch
ã select
ã¯ããã¾ããã、if
ã elif
ã§ä»£ç¨ãã¾ã。
rangelist = range(10) >>> print rangelist [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] for number in range(10): # number ãã¿ãã«å ã«å«ã¾ãã¦ãããããã§ã㯠if number in (3, 4, 7, 9): # "break" 㯠"else" ã«è¡ããã« for ãçµäºãã¾ã break else: # "continue" ã¯æ¬¡ã®ã«ã¼ããç¶ãã¾ã continue else: # "else" 㯠"break" 以å¤ã®å ´åã«å®è¡ããã¾ã pass # ä½ãããªãå ´å㯠"pass" ã¨è¨è¼ if rangelist[1] == 2: print "The second item (lists are 0-based) is 2" elif rangelist[1] == 3: print "The second item (lists are 0-based) is 3" else: print "Dunno" while rangelist[1] == 1: pass
é¢æ°
é¢æ°ã¯ "def" ã§å®£è¨ãã¾ã。å¿ é ã®å¼æ°ã®å¾ã«、ããã©ã«ãã®å¤ãæå®ãããªãã·ã§ã³å¼æ°ã宣è¨ãããã¨ãã§ãã¾ã。ååä»ãå¼æ°ã¯å¤ã渡ãå¼æ°ã®ååãæå®ããäºãåºæ¥ã¾ã。é¢æ°ãè¿ãããçµæãè¤æ°ããå ´å、ã¿ãã«ãè¿ãã¨、å¹ççã« è¤æ°ã®çµæãã¢ã³ããã¯ãããã¨ãã§ãã¾ã。ã©ã ãé¢æ°ã¯ 1 è¡ã®æ§æãå«ãã¢ãããã¯é¢æ°ãå®ç¾©ãã¾ã。ãã©ã¡ã¼ã¿ã¯åç §æ¸¡ãã§ãã、ã¤ãã¥ã¼ã¿ãã«å (ã¿ãã«、æ°å¤、æååç) ã¯å¤æ´ã§ãã¾ãã。ããã¯å¼æ°ã¨ãã¦ãã®ãªãã¸ã§ã¯ãã®ã¡ã¢ãªä¸ã®ä½ç½®ã渡ãã¦ããããã§ãã、ãããä»ã®ãªãã¸ã§ã¯ãã¸çµã³ã¤ããã¨ä»ã¾ã§ã®ãªãã¸ã§ã¯ãã¯ç ´æ£ãã、ã¤ãã¥ã¼ã¿ãã«åã®ãªãã¸ã§ã¯ãã¯æ°ããçµã³ã¤ãããããªãã¸ã§ã¯ãã«ãªã£ã¦ãã¾ãã¾ã。ã¤ãã¥ã¼ã¿ãã«åã®ãªãã¸ã§ã¯ãã®ãã¡é åã«é¢ãã¦ã¯、ä¸è¿°ããã·ã£ãã¼ã³ãã¼ (array[:]) ã§æ¸¡ãã¦ãããã¨、æ°ããªãªãã¸ã§ã¯ããä½æããã®ã§、å ã®é å (ã·ã£ãã¼ã³ãã¼ãªã®ã§å¤ã¯åç §ã§ã) ãä¿æãããã¨ãã§ãã¾ã。
# def f(x): return x + 1 ã¨åãã㨠functionvar = lambda x: x + 1 >>> print functionvar(1) 2 # an_int 㨠a_string ã¯åæå¤ãæã¤ãªãã·ã§ã³å¼æ°ã§ã # æåã®å¼æ°ã¯çç¥ãããã¨ãã§ãã¾ãã def passing_example(a_list, an_int=2, a_string="A default string"): a_list.append("A new item") an_int = 4 return a_list, an_int, a_string >>> my_list = [1, 2, 3] >>> my_int = 10 >>> print passing_example(my_list, my_int) ([1, 2, 3, 'A new item'], 4, "A default string") >>> my_list [1, 2, 3, 'A new item'] >>> my_int 10
ã¡ãªã¿ã«ãªãã·ã§ã³å¼æ°ã¨、ååä»ãå¼æ°ãæ··åãã¦ãã?ã¨è¦åããããæç®ããã¾ã«è¦ããã¾ãã、両è ã¯ã¾ã£ããéãã¾ã。ãªãã·ã§ã³å¼æ°ã¯åè¿°ã®éã、ããã©ã«ãå¤ãæå®ããããªãã·ã§ãã«ã®å¼æ°。ååä»ãå¼æ°ã¨ããã®ã¯、å¼æ°ã®ååãæå®ãã¦å¤ã渡ããã¨ãã§ããæ©è½ã®ãã¨ã§ã。
def spam(foo, bar="optional"): # foo ã¯å¿ é ã®å¼æ° # bar ã¯ãªãã·ã§ã³å¼æ° print foo, bar # ãªãã·ã§ã³å¼æ°ãæå®ããªã、éååä»ãå¼æ°å¼ã³åºã spam("mandatory") # ãªãã·ã§ã³å¼æ°ãæå®ãã、éååä»ãå¼æ°å¼ã³åºã spam("mandatory", "optional") # ãªãã·ã§ã³å¼æ°ãæå®ãã、ååä»ãå¼æ°å¼ã³åºã 1 spam(foo="mandatory", bar="optional") # ãªãã·ã§ã³å¼æ°ãæå®ãã、ååä»ãå¼æ°å¼ã³åºã 2 # ååä»ãå¼æ°ã使ã£ã¦ã、å¼æ°ã®é çªã¯å®ããã!èªã¿ã«ãããªã。 spam(bar="optional", foo="mandatory") # ãã¹ã¦ "mandatory optional" ã¨åºåããã¾ã
ã¯ã©ã¹
Python ã®ã¯ã©ã¹ã¯、å¶éä»ãå¤éç¶æ¿ããµãã¼ããã¦ãã¾ã。ãã©ã¤ãã¼ã (å³å¯ã«ã¯ãã©ã¤ãã¼ãã§ã¯ãªã) å¤æ°ãã¡ã½ããã¯、 __spam ã®ããã«æåã« 2 ã¤ä»¥ä¸ã®ã¢ã³ãã¼ã©ã¤ã³ãããååã§å®£è¨ãã¾ã。ã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ã«ã¯ä»»æã®ååãä»ãããã¨ãã§ãã¾ã。
class MyClass: common = 10 def __init__(self): self.myvariable = 3 def myfunction(self, arg1, arg2): return self.myvariable >>> classinstance = MyClass() >>> classinstance.myfunction(1, 2) 3 # å¤æ° "common" ã¯ãã¹ã¦ã®ã¯ã©ã¹ã«å ±éããã¯ã©ã¹å¤æ° >>> classinstance2 = MyClass() >>> classinstance.common 10 >>> classinstance2.common 10 # ã¯ã©ã¹å¤æ°ã®æ´æ°ã¯、ã¤ã³ã¹ã¿ã³ã¹ã§ã¯ãªãã¯ã©ã¹åã«ããæä½ >>> MyClass.common = 30 >>> classinstance.common 30 >>> classinstance2.common 30 # ã¯ã©ã¹å¤æ°ãæ´æ°ããã«ã¤ã³ã¹ã¿ã³ã¹å¤æ°ã®ã¿ãæ´æ° >>> classinstance.common = 10 # ã¯ã©ã¹å¤æ°ããã¤ã³ã¹ã¿ã³ã¹å¤æ°ã¸ >>> classinstance.common 10 >>> classinstance2.common 30 >>> MyClass.common = 50 # "common" ã¯ã¤ã³ã¹ã¿ã³ã¹å¤æ°ã«ãªã£ã¦ããããå¤æ´ãããªã >>> classinstance.common 10 # "common" ã¯ã¯ã©ã¹å¤æ°ã®ã¾ã¾ãªã®ã§å¤æ´ããã >>> classinstance2.common 50 # MyClass ãç¶æ¿ããã¯ã©ã¹ # ã¯ã©ã¹ãå¤éç¶æ¿ãããå ´åã¯ä»¥ä¸ã®ããã«å®ç¾©ãã: # class OtherClass(MyClass1, MyClass2, MyClassN) class OtherClass(MyClass): # å¤æ° "self" ã¯ã¯ã©ã¹ã¤ã³ã¹ã¿ã³ã¹ã¸ã®åç §ãèªåçã«ä»£å ¥ããã def __init__(self, arg1): self.myvariable = 3 print arg1 >>> classinstance = OtherClass("hello") hello >>> classinstance.myfunction(1, 2) 3 # ãã®ã¯ã©ã¹ã«ã¯ "test" ã¨ããã¡ã³ãå¤æ°ãæã£ã¦ãã¾ããã、 # ãã®ã¤ã³ã¹ã¿ã³ã¹ã«å¯¾ãã¦ãã¤ã§ã追å ãããã¨ãã§ãã¾ã。 >>> classinstance.test = 10 >>> classinstance.test 10
ä¾å¤
Python ã®ä¾å¤ã¯ try-except ãããã¯ã§ãã³ããªã³ã°ãã¾ã。ã¾ã finally å¥ã§、äºå¾å¦çãããäºãã§ãã¾ã。
def spam(): try: # 0 ã«ããä¹ç®ã§ä¾å¤ãã¹ãã¼ 10 / 0 except ZeroDivisionError: # ä¾å¤çºçæã®å¦ç print "Oops, invalid." else: # ä¾å¤ãçºçããªãã£ãå ´åã®å¦ç pass finally: # ä¾å¤çºçã®æç¡ã«ãããããå¦ç print "We're done with that." >>> spam() Oops, invalid. We're done with that.
ã¤ã³ãã¼ã
å¤é¨ã®ã©ã¤ãã©ãªã¯ import <libname>
ã§ã¤ã³ãã¼ããã¾ã。ã¾ã、from <libname> import <funcname> ã使ã£ã¦、ã©ã¤ãã©ãªå
ã®é¢æ°ãã¤ã³ãã¼ããããã¨ãã§ãã¾ã。
import random from time import clock randomint = random.randint(1, 100) >>> print randomint 64
ç¾ããã³ã¼ããæ¸ãããã« from <libname> import *
ãå¤ç¨ããã®ã¯é¿ãã¾ããã。åå空éãå¿é
ã«ãªãã¾ã。ãã¨、ãªãã¹ãé¢æ°ã§ã¯ãªãã¢ã¸ã¥ã¼ã«ã¬ãã«ã§ã¤ã³ãã¼ããã¾ããã。read
ã¨ãããé¢æ°ã¨ããã¶ãããã§ããã。ããã« spam.read
ã egg.read
ã¨æ¸ãã¦ãã£ãã»ããè³ã«åªããã§ã。
ãã¡ã¤ã«å ¥åºå
Python ã«ã¯å¤ãã®æ¨æºã©ã¤ãã©ãªã¼ãåãã£ã¦ãã¾ã。ä¾ãã°、ãã¡ã¤ã«å ¥åºåã使ã£ãã·ãªã¢ã©ã¤ãº (pickle ã使ã£ã¦ãã¼ã¿æ§é ãæååã«å¤æãã) ã¯ä»¥ä¸ã®ããã«æ¸ããã¨ãã§ãã¾ã。
import pickle mylist = ["This", "is", 4, 13327] # "C:\binary.dat" ãæ¸è¾¼æ¨©éã§éã # ãã¡ã¤ã«åã®åã® "r" ã¯æååãã¨ã¹ã±ã¼ãããªãã£ã¦ããæå³ # "raw string" ã® "r" myfile = file(r"C:\binary.dat", "w") pickle.dump(mylist, myfile) myfile.close() myfile = file(r"C:\text.txt", "w") myfile.write("This is a sample string") myfile.close() myfile = file(r"C:\text.txt") >>> print myfile.read() 'This is a sample string' myfile.close() # ãã¡ã¤ã«ãèªå権éã§éã myfile = file(r"C:\binary.dat") loadedlist = pickle.load(myfile) myfile.close() >>> print loadedlist ['This', 'is', 4, 13327]
ãã®ä»
- åæã«è¤æ°ã®ã³ã³ãã£ã·ã§ã³ããã§ãã¯ã§ãã¾ã。1 < n < 3 ã§ã¯ n ã 1 ãã大ãã、 3 ããå°ãããã©ããããã§ãã¯ãã¾ã。
- é åããã¢ã¤ãã ãåé¤ãããå ´å㯠del ã使ãã¾ã。
- ãªã¹ãå
å
表è¨ã¯ãªã¹ããçæããããã®å¼·åãªæ¹æ³ãæä¾ãã¾ã。ããã¯è¤æ°ã® for-in ã¨、if ã§æ¡ä»¶ãå«ããäºãã§ãã¾ã。
>>> spam = [1, 2, 3] >>> egg = [3, 4, 5] >>> print [x * y for x in spam for y in egg] [3, 4, 5, 6, 8, 10, 9, 12, 15] >>> print [x for x in spam if 4 > x > 1] [2, 3] # "any" ã§ã¢ã¤ãã å ã«å¤ãçå¤ã§ããããã§ãã¯ã§ãã¾ã >>> any([i % 3 for i in [3, 3, 4, 4, 3]]) True # 4 % 3 ã 1 ã«ãªã、1 ã¯çå¤ã§ãããã、True ãè¿ãã¾ã # æ¡ä»¶ã«è©²å½ããå¤ã®åæ°ã調ã¹ããå ´åã¯ä»¥ä¸ã®ããã«æ¸ãã¾ã >>> sum(1 for i in [3, 3, 4, 4, 3] if i == 4) 2 # å¤ã®åé¤ >>> del spam[0] >>> print spam [2, 3] >>> del spam
- ã°ãã¼ãã«å¤æ°ã¯é¢æ°å¤ã«å®£è¨ã、ç¹å¥ãªå®£è¨ãããªãã¦ãé¢æ°å
ã§åç
§ã§ãã¾ã。ãããå¤ãå¤æ´ãããå ´åã¯、é¢æ°ã®æåã« "global" ã®å¾ã«å¤æ´ããå¤æ°ãè¨è¿°ããå¿
è¦ãããã¾ã。"global" 宣è¨ããªãå ´å、Python ã¯ãããæ°ããæ°ãããã¼ã«ã«å¤æ°ã¨ã¿ãªãã¾ã。
number = 5 def spam(): # 5 ã¨åºåãã¾ã print number def egg(): # æ°ããå¤æ°ãä½ãåã«ãã®å¤æ°ãåç §ãã¦ããã¨ã¿ãªãã、 # ä¾å¤ãã¹ãã¼ããã¾ã。 print number number = 3 def yetanother(): global number # ã°ãã¼ãã«å¤æ°ãæ¸ãæãã number = 3
åé 、確ãã«「å¦è¨³」ã§ã¯ãããã§ãããã、「æ訳」ã®èª¤å?
è¿ä¿¡åé¤ãããå è¨äºããçç¥ããã¦ããã¨ãããè¦å½ããã、ãããä»å ããã¦ããé¨åããã£ããããã®ã§、ãã®å ´åã¯åãªã「翻訳」「æ¥æ¬èªè¨³」ã§ããã®ã§ã¯ãªããã¨。
ãªãã¨・・・。ããè¦ãããã使ã£ã¨ããã¨æå³ããããã使ã£ã¦ã¾ããï½
è¿ä¿¡åé¤ãã "æ訳" ã£ã¦ãããã§ãã!
ãææãããã¨ããããã¾ã。åå¼·ã«ãªãã¾ãã!
「 array[:] ã§é åã®ãã£ã¼ãã³ãã¼ãã§ãã¾ã。」
è¿ä¿¡åé¤ã¨ããã¾ãã、
[:] ã¯ã·ã£ãã¼ã³ãã¼ã§ãã。
ãææãããã¨ããããã¾ã。ä¿®æ£ãããã¾ãã m(_ _)m
è¿ä¿¡åé¤