ããç°¡æœã§èªã¿ãããã³ãŒããæžãããã§ããïŒ ã§ããã ãå€ãã®æå³ã1ã€ã®è¡šçŸã«åãããã§ããïŒ æ®ãã®äººçãããã¥ã¡ã³ããèªãããããããã€ãã®ããªãã¯ãèªãæ¹ãè¯ããšæããŸããïŒ å®ãŠãŸããã Pythonãå°ã䜿çšããå Žåã«ééããå¯èœæ§ã®ããå°ããªããªãã¯ããå§ããŸãã ããããèšäºã®çµããé ã«ã¯ããã£ãšããããªãã®ãèŠã€ãããšçŽæããŸãã
å 容
1.ã¡ãã£ãšããããªãã¯ã 4çš®é¡ã®åŒçšç¬Šã ããŸããŸãªãªããžã§ã¯ãã®ä¿¡veræ§ã éšåæååã®åºçŸã確èªããŠãã ããã çŸãããªã¹ãåºåã æŽæ°é€ç®ãšæµ®åå°æ°ç¹é€ç®ã ã©ã ãé¢æ°ã
2.ãªã¹ãã ãªã¹ããžã§ãã¬ãŒã¿ãŒãšåŒãžã§ãã¬ãŒã¿ãŒã
ãã¹ãŠã®ã³ãŒããã©ã°ã¡ã³ããè¿œå ã®å€æŽãªãã§å®è¡ãããããã«ããŸããã å¿ èŠã«å¿ããŠãããããPythonã·ã§ã«ã«ã³ããŒããŠãäœãèµ·ãããã確èªã§ããŸãã å€ãã®äŸã«ã¯ãã³ã¡ã³ãã¢ãŠãããããééã£ãããã©ã°ã¡ã³ããå«ãŸããŠããããšã«æ³šæããŠãã ããã è¡ã®ã³ã¡ã³ãã解é€ããŠäœãèµ·ãããã確èªããããšã劚ãããã®ã¯äœããããŸããã
ãã®èšäºã®trueãšTrueã®ããããªéãïŒãªããžã§ã¯ããtrueã§ãããšèšããšãããŒã«åã«ãã£ã¹ãããããšTrueã«ãªããŸãã åæ§ã«falseããã³Falseã䜿çšããŸãã
1ã¡ãã£ãšããããªãã¯
1.1 4çš®é¡ã®åŒçšç¬Š
ãŸã第äžã«ãããªãã¯ãã§ã«ç¥ã£ãŠãããããããŸããã äžéšã®ããã°ã©ãã³ã°èšèªã§ã¯ãäžéåŒçšç¬ŠãšäºéåŒçšç¬Šã¯ç°ãªããã®ãè¡šããŸãã Pythonã§ã¯ãäž¡æ¹ã®ãªãã·ã§ã³ã䜿çšã§ããŸãïŒãã ããæååã¯åãã¿ã€ãã®åŒçšç¬Šã§éå§ããã³çµäºããå¿ èŠããããŸãïŒã Pythonã«ã¯ãããã«2çš®é¡ã®åŒçšç¬ŠããããŸãïŒ '' 'ïŒããªãã«ã·ã³ã°ã«ïŒãš "" "ïŒããªãã«ããã«ïŒããããã£ãŠããšã¹ã±ãŒããå¿é ããåã«ãããã€ãã®ã¬ãã«ã®åŒçšç¬Šã䜿çšã§ããŸããããšãã°ã次ã®ã³ãŒãã¯æ£ããã§ãã
print """ , : ''' : " : ' '"'''"""
1.2ããŸããŸãªãªããžã§ã¯ãã®çå®æ§
äžéšã®ããã°ã©ãã³ã°èšèªãšã¯ç°ãªããPythonã§ã¯ããªããžã§ã¯ãã¯ç©ºã®å Žåã®ã¿åœãšèŠãªãããŸãã ããã¯ãæååãã¿ãã«ããŸãã¯èŸæžã®é·ãããã§ãã¯ããå¿ èŠããªãããšãæå³ããŸã-è«çåŒãšããŠãã§ãã¯ããã ãã§ãã
0ãåœã§ãããæ®ãã®æ°ãçã§ãããšäºæž¬ããã®ã¯ç°¡åã§ãã
ããšãã°ã次ã®åŒã¯åçã§ãã ãã®å Žåãmy_objectã¯æååã§ãããç°ãªãã¿ã€ããååšããå¯èœæ§ããããŸãïŒifãããã¯ã®æ¡ä»¶ã«å¯Ÿå¿ããå€æŽãããïŒã
my_object = 'Test' # True example # my_object = '' # False example if len(my_object) > 0: print 'my_object ' if len(my_object): # 0 False print 'my_object ' if my_object != '': print 'my_object ' if my_object: # False print 'my_object '
ãããã£ãŠããªããžã§ã¯ãã空ã§ãããã©ããã ãã«é¢å¿ãããå Žåã¯ããªããžã§ã¯ãã®é·ãããã§ãã¯ããå¿ èŠã¯ãããŸããã
1.3ãµãã¹ããªã³ã°ã®ç¢ºèª
ããã¯å°ãããããªãæçœãªãã³ãã§ãããç§ã¯Pythonã1幎éåŠç¿ããŠããåããŠããã«ã€ããŠåŠã³ãŸããã ãitem in listããŸãã¯ãitem not in listãã䜿çšããŠãç®çã®ã¢ã€ãã ãã¿ãã«ããªã¹ããèŸæžã«å«ãŸããŠãããã©ããã確èªã§ããããšãç¥ã£ãŠããå¿ èŠããããŸãã ãããæååã§æ©èœãããšã¯æ³åã§ããŸããã§ããã ç§ã¯ãã€ããã®ãããªãã®ãæžããïŒ
string = 'Hi there' # True example # string = 'Good bye' # False example if string.find('Hi') != -1: print 'Success!'
ãã®ã³ãŒãã¯ããªãåä»ã§ãã ãæååã®ãµãã¹ããªã³ã°ã®å Žåãã¯ãŸã£ããåãããã«æ©èœããŸãã
string = 'Hi there' # True example # string = 'Good bye' # False example if 'Hi' in string: print 'Success!'
ããã·ã³ãã«ã§æ確ã 99ïŒ ã®äººã«ã¯æçœãããããŸããããç§ãç¥ãåã«ããã«ã€ããŠç¥ããããšæããŸãã
1.4çŸãããªã¹ãåºå
printã䜿çšãããªã¹ãã®éåžžã®åœ¢åŒã¯ãããŸã䟿å©ã§ã¯ãããŸããã ãã¡ããããªã¹ããäœã§ãããã¯æ確ã«ãªããŸãããã»ãšãã©ã®å ŽåããŠãŒã¶ãŒã¯åèŠçŽ ãåŒçšç¬Šã§å²ã¿ãããããŸããã æååã®çµåæ¹æ³ã䜿çšããç°¡åãªè§£æ±ºçããããŸãã
recent_presidents = [' ', ' ', ' '] print ' %s.' % ', '.join(recent_presidents) # " , , ."
joinã¡ãœããã¯ããªã¹ããæååã«å€æããåèŠçŽ ãæååãšããŠæ±ããŸãã ã»ãã¬ãŒã¿ã¯ãçµåãåŒã³åºãããè¡ã§ãã æåŸã®èŠçŽ ã®åŸã«åºåãæåãæ¿å ¥ããªãã®ã¯ååè³¢æã§ãã
è¿œå ããŒãã¹ïŒçµåã¯ç·åœ¢æéã§å®è¡ãããŸãã forã«ãŒãã§ãªã¹ãã¢ã€ãã ãæããããã§æååãäœæããããšã¯ãããŸãããèŠèŠããã ãã§ãªãã 2次çãªæéãããããŸãã
1.5æŽæ°é€ç®ãšæµ®åå°æ°ç¹é€ç®
æŽæ°ãæŽæ°ã§é€ç®ãããšãããã©ã«ãã§çµæã¯æŽæ°ã«åãæšãŠãããŸãã ããšãã°ã5/2ã¯2ãè¿ããŸãã
ãããä¿®æ£ããã«ã¯2ã€ã®æ¹æ³ããããŸãã æåã®æãç°¡åãªæ¹æ³ã¯ãæ°å€ã®1ã€ãfloatåã«å€æããããšã§ãã å®æ°ã®å Žåãæ°åã®1ã€ã«ã.0ããè¿œå ããã ãã§ãã5.0/ 2ã¯2.5ãè¿ããŸãã floatïŒ5ïŒ/ 2ã³ã³ã¹ãã©ã¯ãã䜿çšããããšãã§ããŸãã
2çªç®ã®æ¹æ³ã¯ãããã¯ãªãŒã³ãªã³ãŒããæäŸããŸãããããã°ã©ã ããã®éèŠãªå€æŽããå£ããªãããã«ããå¿ èŠããããŸãã ãfrom __future__ import divisionããåŒã³åºããåŸãPythonã¯åžžã«é€ç®ã®çµæãšããŠfloatãè¿ããŸãã æŽæ°é€ç®ãå¿ èŠãªå Žåã¯ãæŒç®å//ïŒ5 // 2ã䜿çšãããšåžžã«2ãè¿ãããŸãã
5/2 # 2 5.0/2 # 2.5 float(5)/2 # 2.5 5//2 # 2 from __future__ import division 5/2 # 2.5 5.0/2 # 2.5 float(5)/2 # 2.5 5//2 # 2
Pythonã®æ¬¡ã®ããŒãžã§ã³ã®1ã€ã§ã¯ããã®åäœãããã©ã«ãã«ãªããŸãã ã³ãŒãã®äºææ§ãç¶æããå Žåã¯ããã®ã€ã³ããŒãã䜿çšããŠããªãå Žåã§ããæŽæ°é€ç®ã«//æŒç®åã䜿çšããŸãã
1.6ã©ã ãé¢æ°
é¢æ°ãåŒæ°ãšããŠæž¡ãããçããè€éãªæäœãæ°åè¡ãå¿ èŠãããå ŽåããããŸãã éåžžã®æ¹æ³ã§é¢æ°ãå®çŸ©ããããã©ã ãé¢æ°ïŒåäžã®åŒã®çµæãè¿ãå°ããªé¢æ°ïŒã䜿çšã§ããŸãã 次ã®2ã€ã®å®çŸ©ã¯ãŸã£ããåãã§ãã
def add(a,b): return a+b add2 = lambda a,b: a+b
ã©ã ãé¢æ°ã®å©ç¹ã¯ããããåŒã§ãããå¥ã®åŒã®äžã§äœ¿çšã§ããããšã§ãã 以äžã¯ããªã¹ãå ã®åã¢ã€ãã ã«å¯ŸããŠé¢æ°ãåŒã³åºããçµæã®ãªã¹ããè¿ãmapé¢æ°ã䜿çšããäŸã§ãã ïŒæ¬¡ã®æ®µèœã§ã¯ãããããå®éã«åœ¹ã«ç«ããªãããšã瀺ããŸããããããããã¯1è¡ã§è¯ãäŸã瀺ãæ©äŒãäžããŠãããŸããïŒ
squares = map(lambda a: a*a, [1,2,3,4,5]) # squares = [1,4,9,16,25]
ã©ã ãé¢æ°ããªããã°ãé¢æ°ãåå¥ã«å®çŸ©ããå¿ èŠããããŸãã 1è¡ã®ã³ãŒããš1ã€ã®å€æ°åãä¿åããŸããã
ã©ã ãé¢æ°ã®æ§æïŒ ã©ã ãå€æ°ïŒåŒ
å€æ° -ã³ã³ãã§åºåãããåŒæ°ã®ãªã¹ãã ããŒã¯ãŒãã¯äœ¿çšã§ããŸããã åŒæ°ãæ¬åŒ§ã§å²ãå¿ èŠã¯ãããŸããã
expressionã¯ã€ã³ã©ã€ã³PythonåŒã§ãã ã¹ã³ãŒãã«ã¯ããŒã«ã«å€æ°ãšåŒæ°ãå«ãŸããŸãã é¢æ°ã¯ããã®åŒã®çµæãè¿ããŸãã
2ãªã¹ã
2.1ãªã¹ããžã§ãã¬ãŒã¿ãŒ
Pythonãååã«é·ã䜿çšããŠããã°ãå°ãªããšãããªã¹ãå å è¡šèšãã®æŠå¿µã«ã€ããŠèããããšãããã¯ãã§ãã ããã¯ãforã«ãŒããifãããã¯ãããã³å²ãåœãŠãåãè¡ã«åããæ¹æ³ã§ãã
ã€ãŸãã1ã€ã®åŒã§ãªã¹ãã衚瀺ïŒãããïŒããã³ãã£ã«ã¿ãŒã§ããŸãã
2.1.1ãªã¹ã衚瀺
æãç°¡åãªäŸããå§ããŸãããã ãªã¹ãã®ãã¹ãŠã®èŠçŽ ãäºä¹ããå¿ èŠããããšããŸãã çŒãããŠã®Pythonããã°ã©ããŒã¯ã次ã®ãããªã³ãŒããæžãããšãã§ããŸãã
numbers = [1,2,3,4,5] squares = [] for number in numbers: squares.append(number*number) # squares = [1,4,9,16,25]
ãããªã¹ããå¥ã®ãªã¹ãã«ããããã³ã°ãããŸããã ããã¯ããããé¢æ°ã䜿çšããŠè¡ãããšãã§ããŸãã
numbers = [1,2,3,4,5] squares = map(lambda x: x*x, numbers) # squares = [1,4,9,16,25]
ãã®ã³ãŒãã¯ééããªãçãïŒ3è¡ã§ã¯ãªã1è¡ïŒããããã§ãstillãã äžèŠãããšãmapé¢æ°ãäœãããããèšãã®ã¯å°é£ã§ãïŒé¢æ°ãšãªã¹ããåŒæ°ãšããŠåãããªã¹ãã®åèŠçŽ ã«é¢æ°ãé©çšããŸãïŒã ããã«ãé¢æ°ã決å®ããããšãäœåãªããããããã¯ããªãåä»ã«èŠããŸãã ããçŸãããã¹ããã£ãå Žåã®ã¿...äŸãã°ããªã¹ããžã§ãã¬ãŒã¿ïŒ
numbers = [1,2,3,4,5] squares = [number*number for number in numbers] # squares = [1,4,9,16,25]
ãã®ã³ãŒãã¯ãŸã£ããåãããšãè¡ããŸãããæåã®äŸãããçãã2çªç®ã®äŸãããæ確ã§ãã 人ã¯ã³ãŒããäœãããŠããã®ããç°¡åã«å€æã§ããŸãããã®ãããPythonãç¥ãå¿ èŠãããããŸããã
2.1.2ãªã¹ãã®ãã£ã«ã¿ãªã³ã°
ãããããªã¹ãã®ãã£ã«ã¿ãªã³ã°ã«é¢å¿ãããå Žåã¯ã©ãã§ããããïŒ ããšãã°ã4以äžã®èŠçŽ ãåé€ããŸãïŒã¯ããäŸã¯ããŸãçŸå®çã§ã¯ãããŸããããšã«ãã...ïŒ
åå¿è ã¯æ¬¡ã®ããã«æžããŸãã
numbers = [1,2,3,4,5] numbers_under_4 = [] for number in numbers: if number < 4: numbers_under_4.append(number) # numbers_under_4 = [1,4,9]
ãšãŠãç°¡åã§ãããïŒ ããããã³ãŒãã¯4è¡ã§ã2ã€ã®ã¬ãã«ã®ã€ã³ãã³ããå«ã¿ãåæã«äºçŽ°ãªããšãããŠããŸãã ãã£ã«ã¿ãŒé¢æ°ã䜿çšããŠã³ãŒããµã€ãºãçž®å°ã§ããŸãã
numbers = [1,2,3,4,5] numbers_under_4 = filter(lambda x: x < 4, numbers) # numbers_under_4 = [1,2,3]
äžèšã§èª¬æãããããé¢æ°ã®ããã«ããã£ã«ã¿ãŒã¯ã³ãŒããççž®ããŸãããèŠãç®ã¯ããªããã§ãã äžäœäœãèµ·ãã£ãŠããã®ïŒ mapãšåæ§ã«ãfilterã¯é¢æ°ãšãªã¹ããååŸããŸãã èŠçŽ ããã®é¢æ°ãtrueãè¿ãå Žåããã®èŠçŽ ã¯çµæã®ãªã¹ãã«å«ãŸããŸãã ãã¡ããããªã¹ããžã§ãã¬ãŒã¿ãŒã§ãããè¡ãããšãã§ããŸãã
numbers = [1,2,3,4,5] numbers_under_4 = [number for number in numbers if number < 4] # numbers_under_4 = [1,2,3]
ç¹°ãè¿ããŸãããããçããããæ確ã§ãããç解ããããã³ãŒããååŸããŸããã
2.1.3ããããšãã£ã«ã¿ãŒã®åæ䜿çš
ããã§ããªã¹ããžã§ãã¬ãŒã¿ãŒã®å šæ©èœã䜿çšã§ããŸãã ããããšãã£ã«ã¿ãŒãããŸãã«ãå€ãã®æéãè²»ãããŠããããšããŸã çŽåŸããŠããªãå Žåã¯ãç§ã«åæããŠããã ããã°å¹žãã§ãã
ãªã¹ããåæã«è¡šç€ºããã³ãã£ã«ã¿ãªã³ã°ããããšããŸãã ã€ãŸãããªã¹ãé ç®ã®4ããå°ããæ£æ¹åœ¢ã衚瀺ãããã®ã§ããããäžåºŠãåå¿è ã¯æ¬¡ã®ããã«æžããŸãã
numbers = [1,2,3,4,5] squares = [] for number in numbers: if number < 4: squares.append(number*number) # squares = [1,4,9]
æ²ããããªãã³ãŒãã¯å³ã«äŒžã³å§ããŸããã ãã¶ãããã¯ãããç°¡çŽ åããŸããïŒ ããããšãã£ã«ã¿ãŒã䜿çšããŠã¿ãŸãããããç§ã¯ããã«ã€ããŠæªãæ°æã¡ãæã£ãŠããŸã...
numbers = [1,2,3,4,5] squares = map(lambda x: x*x, filter(lambda x: x < 4, numbers)) # squares is = [1,4,9]
以åã¯ããããšãã£ã«ã¿ãŒã®èªã¿åããå°é£ã§ããããçŸåšã¯äžå¯èœã§ãã æããã«ãããã¯è¯ãèãã§ã¯ãããŸããã ãŸãããªã¹ããžã§ãã¬ãŒã¿ãŒã¯ç¶æ³ãä¿åããŸãã
numbers = [1,2,3,4,5] squares = [number*number for number in numbers if number < 4] # square = [1,4,9]
ãªã¹ããžã§ãã¬ãŒã¿ãŒã䜿çšãã以åã®äŸãããå°ãé·ããªããŸããããç§ã®æèŠã§ã¯ãããªãèªã¿ãããã§ãã forã«ãŒãã䜿çšããããããããšãã£ã«ã¿ãŒã䜿çšããããããããééããªãåªããŠããŸãã
ã芧ã®ããã«ããªã¹ããžã§ãã¬ãŒã¿ãŒã¯æåã«ãã£ã«ã¿ãŒããããŠè¡šç€ºããŸãã ããªããééããªãå察ãå¿ èŠãšãããªãã°ãããã¯ããé£ããã§ãããã ã©ã¡ããããåçŽã§ãããã«å¿ããŠããã¹ããããäžä»£ãããããšãã£ã«ã¿ãŒããŸãã¯éåžžã®forã«ãŒãã䜿çšããå¿ èŠããããŸãã ããããããã¯ãã§ã«èšäºã®ç¯å²ãè¶ ããŠããŸãã
ãªã¹ããžã§ãã¬ãŒã¿ãŒæ§æïŒ[æ¡ä»¶ã®å Žåã®ãªã¹ãå ã®å€æ°ã®èŠçŽ ]
ãªã¹ã -å埩å¯èœãªèŠçŽ
å€æ°ïŒsïŒ -forã«ãŒãã«äŒŒããçŸåšã®ãªã¹ãé ç®ã«çãã1ã€ãŸãã¯è€æ°ã®å€æ°
condition-ã€ã³ã©ã€ã³åŒïŒtrueã®å ŽåãèŠçŽ ãçµæã«è¿œå ãããŸã
element-çµæãçµæãªã¹ãã®èŠçŽ ãšããŠäœ¿çšãããã€ã³ã©ã€ã³åŒ
2.1.4ãžã§ãã¬ãŒã¿ãŒåŒ
ãªã¹ããžã§ãã¬ãŒã¿ãŒã«ã¯è£è¿ãããããŸãããªã¹ãå šäœãã¡ã¢ãªå ã«ããå¿ èŠããããŸãã ããã¯ãåã®äŸã®ããã«å°ããªãªã¹ãã§ã¯åé¡ã«ãªãããããã«æ°æ¡ä»¥äžãåé¡ã«ãªããŸããã ããããæçµçã«ã¯å¹æããªããªããŸãã
ãžã§ãã¬ãŒã¿åŒã¯Python 2.4ã§ç»å ŽããŸããã ãã¹ãŠã®Pythonãããã®äžã§ãããããããŸã泚ç®ãããŠããŸããã ãªã¹ããžã§ãã¬ãŒã¿ãŒãšã®éãã¯ããªã¹ãå šäœãã¡ã¢ãªã«ããŒãããã®ã§ã¯ãªã ãããžã§ãã¬ãŒã¿ãŒãªããžã§ã¯ãããäœæããåæç¹ã§ãªã¹ãã®1ã€ã®èŠçŽ ã®ã¿ãããŒããããããšã§ãã
ãã¡ããããªã¹ããäœãã«äœ¿çšãããå Žåãããã¯ããŸã圹ã«ç«ã¡ãŸããã ãã ããå埩å¯èœãªãªããžã§ã¯ããå¿ èŠãªå ŽæïŒforã«ãŒããªã©ïŒã«æž¡ãå Žåã¯ããžã§ãã¬ãŒã¿ãŒé¢æ°ã䜿çšããå¿ èŠããããŸãã
ãžã§ãã¬ãŒã¿åŒã®æ§æã¯ãªã¹ããžã§ãã¬ãŒã¿ãšåãã§ãããè§æ¬åŒ§ã®ä»£ããã«äžžæ¬åŒ§ã䜿çšãããŸãã
numbers = (1,2,3,4,5) # , ;) squares_under_10 = (number*number for number in numbers if number*number < 10) # squares_under_10 - generator object, , .next() for square in squares_under_10: print square, # '1 4 9'
ããã¯ããªã¹ããžã§ãã¬ãŒã¿ãŒã䜿çšãããããå¹ççã§ãã
ãã®ããã倧ããªãªã¹ãã«ã¯ãžã§ãã¬ãŒã¿åŒã䜿çšããã®ãçã«ããªã£ãŠããŸãã ãªã¹ãå šäœãä»ã®ç®çã«å¿ èŠãªå Žåã¯ãä»»æã®ãªãã·ã§ã³ã䜿çšã§ããŸãã ãã ããããã«åè«ããªãå Žåã¯ããžã§ãã¬ãŒã¿åŒã䜿çšããã®ãè¯ãç¿æ £ã§ãã 確ãã«ããªã¹ããå°ããå Žåãå éãæåŸ ããªãã§ãã ããã
æåŸã«ãåŒãžã§ãã¬ãŒã¿ã1ã€ã®æ¬åŒ§ã§å²ãã ãã§ååã§ããããšã«æ³šæããŠãã ããã ããšãã°ã1ã€ã®åŒæ°ã§é¢æ°ãåŒã³åºãå Žåãsome_functionïŒãªã¹ãå ã®ã¢ã€ãã ã®ã¢ã€ãã ïŒãšæžãããšãã§ããŸãã
2.1.5çµè«
ãããèšãã€ããã¯ãããŸããããåŒãžã§ãã¬ãŒã¿ãŒãšãªã¹ããžã§ãã¬ãŒã¿ãŒã§ã§ããããšã«ã€ããŠè§ŠããŸããã ããã§ã¯ãforããã³ifã®ãã¹ãŠã®æ©èœã䜿çšã§ããç¹°ãè¿ãå¯èœãªãªããžã§ã¯ãã§ããã°äœã§ãæäœã§ããŸãã
PDFã®èšäºå šæ