Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Python seminar1

taxio
December 27, 2017

Python seminar1

あくあたん工房で使ったPython基礎講座1回目資料

taxio

December 27, 2017
Tweet

More Decks by taxio

Other Decks in Programming

Transcript

  1. λΫγΦ
 !UBYJP@UFYI  ৘ใճ $$ 1ZUIPO )5.-  4BTT /PEF

    +BWB 3VTU  $ FUDʜ ૊ΈࠐΈ
 ϑϩϯτΤϯυ
  2. ओཁͳσʔλܕ w JOU੔਺ w qPBUුಈখ਺ w TUSจࣈྻ w CPPM࿦ཧ஋ w

    MJTUγʔέϯεܕ w UVQMFมߋෆՄͳϦετΈͨ ͍ͳ΋ͷ w EJDUKTPOͷΑ͏ͳϚοϓܕ TBNQMFTFY@UZQFQZ 1 # int 2 a = 100 3 print(type(a), a) 4 5 # float 6 a = 0.1 7 print(type(a), a) 8 9 # str 10 a = 'hogehoge' 11 print(type(a), a) 12 13 # bool 14 a = True 15 print(type(a), a) 16 17 # list 18 a = [1, 2, 3, 4] 19 print(type(a), a) 20 21 # list multi 22 a = [1, '2', 3.0, [4.1, 4.2]] 23 print(type(a), a) 24 25 # tuple 26 a = (1, 2, 3, 4, 5) 27 print(type(a), a) 28 29 # dict 30 a = {'hoge': 1, 'foo': 'bar'} 31 print(type(a), a)
  3. 㾎ܕΛࢦఆͤͣʹม਺ ΛએݴͰ͖Δ 㾎ผͷܕͷ஋Ͱ্ॻ͖ Ͱ͖ͯ͠·͏ TBNQMFTFY@UZQFQZ 1 # int 2 a

    = 100 3 print(type(a), a) 4 5 # float 6 a = 0.1 7 print(type(a), a) 8 9 # str 10 a = 'hogehoge' 11 print(type(a), a) 12 13 # bool 14 a = True 15 print(type(a), a) 16 17 # list 18 a = [1, 2, 3, 4] 19 print(type(a), a) 20 21 # list multi 22 a = [1, '2', 3.0, [4.1, 4.2]] 23 print(type(a), a) 24 25 # tuple 26 a = (1, 2, 3, 4, 5) 27 print(type(a), a) 28 29 # dict 30 a = {'hoge': 1, 'foo': 'bar'} 31 print(type(a), a)
  4. จࣈྻૢ࡞ w B CͰ࿈݁ w TQMJUࢦఆจࣈͰ෼ׂ w pOEݕࡧ w SFQMBDFஔ׵

    w GPSNBUຒΊࠐΈ w KPJOMJTUΛࢦఆ۠੾ΓจࣈͰ ݁߹ TBNQMFTFY@TUSQZ 1 # ࿈݁ 2 a = 'hoge' 3 b = 'foo' 4 c = a + b 5 print(c) 6 7 a = 'hogehogefoobar' 8 # ෼ׂ 9 print(a.split('foo')) 10 11 # ݕࡧ 12 print(a.find('foo')) 13 14 # ஔ׵ 15 print(a.replace('foo', 'piyo')) 16 17 # ຒΊࠐΈ 18 a = 'pi' 19 b = 3.14 20 print('{} is {}'.format(a, b)) 21 22 # ࢦఆ۠੾ΓจࣈͰ݁߹ 23 a = ['a', 'b', 'c', 'd'] 24 print(','.join(a))
  5. ೖग़ྗ جຊతʹ ˔ ඪ४ग़ྗ͸QSJOU ˔ ඪ४ೖྗ͸JOQVU Ὂ ඞཁʹԠͯ͡Ωϟετ TBNQMFTFY@JOQVU@PVUQVUQZ 1

    print('Hello World!!') 2 print('Hello', 'World', '!!') 3 4 # վߦ࡟আ΋Ͱ͖Δ 5 print('Hello'+'World'+'!!', end='') 6 print('hoge') 7 8 # શͯจࣈྻ(str)Ͱऔಘ͞ΕΔ 9 line = input() 10 print(type(line), line) 11 12 # Ωϟετ 13 line_int = int(line) 14 line_float = float(line)
  6. ੍ޚϑϩʔ ˔ ΠϯσϯτͰϒϩοΫ൑ఆ ˔ SBOHFͰϧʔϓճ਺ࢦఆ ˔ TXJUDIDBTF͸ແ͍ Ὂ JGdFMJGdFMTFͰؤுΔ TBNQMFTFY@DPOUSPMQZ

    1 # xʹ1~10ͷཚ਺ΛೖΕΔ 2 x = random.randint(1, 10) 3 if x < 5: 4 print('LOW') 5 else: 6 print('HIGH') 7 8 # ͜Μͳॻ͖ํ΋Ͱ͖Δ 9 if 4 < x < 6: 10 print('x is 5!!') 11 12 13 for i in range(10): 14 print(i) 15 16 while x > 0: 17 print(x) 18 x -= 1
  7. σʔλߏ଄ γʔέϯεܕ ˔ -JTU Ὂ $ݴޠͷ഑ྻͱ͸ҧ͍ɼಈతʹ ྖҬΛ֬อͰ͖Δ Ὂ ϝιου͕๛෋ ˔

    એݴޙมߋෆՄͳ5VQMFͱ͍͏ ΋ͷ΋͋Δ Ὂ -JTUΑΓগ͠ૣ͍ TBNQMFTFY@MJTUQZ 1 a = [2, 4, 1, 3] 2 print(a) 3 4 # ιʔτ 5 a.sort() 6 print(a) 7 8 # ௥Ճ 9 a.append(4) 10 print(a) 11 12 # ࡟আ 13 a.remove(4) 14 print(a) 15 16 # ௕͞ 17 print(len(a)) 18 19 # ΞΫηε 20 print(a[1]) 21 print(a[-2]) 22 23 # ཁૉ͕͋Δ͔Ͳ͏͔ 24 print(1 in a)
  8. -JTUͱGPS ˔ GPSͱซ༻͢Δ͜ͱͰॱ൪ʹऔಘͰ͖Δ ˔ FOVNFSBUFͰJOEFY΋औಘՄೳ ˔ SBOHF͸࿈ଓͨ͠਺஋Λ࣋ͭϦετΛ࡞੒͍ͯ͠Δ TBNQMFTFY@MJTUQZ 1 #

    forͱซ༻Ͱ͖Δ༏Ε΋ͷ 2 names = ['hoge', 'huga', 'foo', 'bar', 'piyo'] 3 for name in names: 4 print(name) 5 6 for idx, name in enumerate(names): 7 print(idx, name)
  9. Τϥʔͱྫ֎ ˔ USZFYDFQU Ὂ +BWBͷUSZDBUDIͱಉ͡ ˔ ΤϥʔͰϓϩάϥϜΛࢭΊͨ͘ͳ͍ Ὂ ྫ֎ॲཧ TBNQMFTFY@FSSPSQZ

    1 # no error handling 2 x = int(input("Please enter a number :")) 3 print(x) 4 5 # error handling 6 while True: 7 try: 8 x = int(input("Please enter a number :")) 9 print('OK! The number is', x) 10 break 11 12 except ValueError: 13 sys.stderr.write("Oops! That was no valid number.\n") 14 sys.stderr.write("Try again...\n")
  10. ؔ਺ ˔ EFGͰએݴ ˔ جຊతʹࢀর౉͠ w ͨ·ʹ᪴͘ͷͰ
 ಡΜͲ͜͏ ˔ ෳ਺ฦΓ஋Մ

    ˔ ΩʔϫʔυࢦఆՄ ˔ σϑΥϧτҾ਺Մ TBNQMFTFY@GVODUJPOQZ 1 def hello(user, time='morning'): 2 if time == 'morning': 3 return "Good morning {}".format(user) 4 5 elif time == 'night': 6 return "Good night ", user 7 8 else: 9 return "Hello {}".format(user) 10 11 12 if __name__ == '__main__': 13 14 print(hello('aquatan')) 15 16 # Ωʔϫʔυࢦఆ 17 print(hello(user='aquatan')) 18 19 # ෳ਺ฦΓ஋ 20 a, b = hello('aquatan', time='night') 21 print(a, b)
  11. ܕώϯτ ˔ ͔Βಋೖ ˔ ؔ਺ͷҾ਺ɼฦΓ஋ʹ ܕΛࢦఆͰ͖Δ ˔ ࣮ߦʹԿΒӨڹ͸ແ͍ Ὂ ΤσΟλʹΑͬͯ͸

    νΣοΫͯ͘͠ΕΔ TBNQMFTFY@IJOUQZ 1 def hello(user: str, time='morning') -> str: 2 if time == 'morning': 3 return "Good morning {}".format(user) 4 5 elif time == 'night': 6 # ฦΓ஋͸strͳͷͰWarning͕ग़Δ͸ͣ 7 return "Good night ", user 8 9 else: 10 return "Hello {}".format(user) 11 12 13 if __name__ == '__main__': 14 15 print(hello('aquatan')) 16 17 # PycharmͳΒWarning͕ग़Δ͸ͣ 18 print(hello(user=123))