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