■■■1.5. コードセルを使ってみよう---------------------------- [図1.10] print("Hello Python") [図1.11] print("おはよう") print("こんにちは") ●練習問題1 和歌山で全国生産量1位の果物を出力するプログラム print("みかん") ■■■2.1. 数値型-------------------------------------------------------- [図2.1] print(10) [図2.2] print(2+5) print(10/5) print(10%3) print(2*(1+2)) ■■■2.2. 文字列型----------------------------------------------------- [図2.3] print("Apple") [図2.4] print("和歌山"+"県") ■■■2.3. 文字列型と数値型の違い-------------------------------- [図2.5] print(1+2) print("1"+"2") ●練習問題2 print(895931/4724.68) print(int(895931/4724.68),”人/平方キロメートル”) ■■■3.1. 変数を用いた値の記憶----------------------------------- [図3.1] a = 10 print(a) [図3.3] b = 3 b = 6 print(b) [図3.5] sentence = "今日は快晴だ" print(sentence) [図3.6] a = 2 b = 3 print(a * 2) print(a * b) [図3.8] a = 10 a += 1 print(a) ●練習問題3 speed = 90 distance = 3600 print(distance / speed) ■■■4.1. if文-------------------------------------------------------- [図4.2] a = 10 if a > 5: print("aは5よりも大きい") [図4.3] a = 1 if a > 5: print("aは5よりも大きい") [図4.4] a = 10 if a > 5: print("条件に当てはまる時に実行されるプログラム") print("条件の正誤に関わらず実行されるプログラム") [図4.5] a = 4 if a < 9: a += 10 print(a) [図4.6] a = 10 b = 20 c = 30 if a + b == c: print("aとbの和がcと等しい") [図4.7] a = 26 if a % 2 == 0: print("aは偶数です") ●練習問題4-1 [図4.8] temperature = 30 if temperature >= 25: print("夏日です") ●練習問題4-2 [図4.10] a = 3 if a > 5: print("aは5よりも大きい") else: print("aは5よりも大きくない") ■■■4.2. else文-------------------------------------------------------- ●練習問題4-2 [図4.11] temperature = 20 if temperature >= 25: print("夏日です") else: print("夏日ではありません") ■■■ 4.3. elif文-------------------------------------------------------- [図4.13] a = 4 if a > 5: print("aは5よりも大きい") elif a > 3: print("aは5よりも大きくないが3よりも大きい") else: print("aは3よりも大きくない") [図4.14] a = 6 if a > 5: print("aは5よりも大きい") elif a > 3: print("aは5よりも大きくないが3よりも大きい") else: print("aは3よりも大きくない") [図4.15] a = 7 if a >= 10: print("aは10以上") elif a >= 8: print("aは8以上10未満") elif a >= 6: print("aは6以上8未満") elif a >= 4: print("aは4以上6未満") elif a >= 2: print("aは2以上4未満") else: print("aは2未満") ●練習問題4-3 [図4.16] score = 75 if score >= 90: print("S") elif score >= 80: print("A") elif score >= 70: print("B") elif score >= 60: print("C") else: print("不合格") [図4.18] a = 1 b = 3 if a == 1: if b == 1: print("aとbはどちらも1") else: print("aは1だがbは1でない") else: if b == 1: print("bは1だがaは1でない") else: print("aもbも1でない") ●練習問題4-4 [図4.19] score_a = 58 score_b = 75 if score_a < 60: if score_b < 60: print("AさんもBさんも再試験を受ける") else: print("Aさんのみ再試験を受ける") else: if score_b < 60: print("Bさんのみ再試験を受ける") else: print("AさんもBさんも再試験を受けない") ■■■5.1. 配列とは---------------------------------------------------- [図5.2] a = ["apple","banana","orange"] print(a) print(a[1]) [図5.3] a = ["apple",1,"orange"] print(a) print(a[1]) [図5.4] a = ["apple","banana","orange"] a[1] = "pineapple" print(a) [図5.5] a = [20,3,4,5] print(a) a.append(10) print(a) [図5.6] a = ["egg","chicken","tomato"] print(a) a.pop(1) print(a) ■■■6.ループ------------------------------------------------------- ■■■ 6.1. for文------------------------------------------------------- [図6.2] for i in range(3): print("Hello World") [図6.3] for i in range(3): print(i) ■■■ 6.2. 配列の各要素の参照------------------------------------------------------- [図6.5] a = ["egg","chicken","tomato"] for i in a: print(i) ■■■6.3. while文------------------------------------------------------- [図6.7] a = 0 while a < 3: print(a) a = a + 1 [図6.8] n = 5 a = 0 while a < n: print(a) a += 1 [図6.10] a = ["apple","banana","orange","grape","peach"] i = 0 while i < len(a): print(a[i]) i += 1 [図6.11] a = ["apple","banana","orange","grape","peach"] for i in range(len(a)): print(a[i]) ●練習問題6 [図6.12] numbers = [10,2,6,250,1000] for i in range(len(numbers)): numbers[i] = numbers[i] * 2 print(numbers) ■■■7.関数-------------------------------------------------------- ■■■ 7.1. 関数とは-------------------------------------------------------- [図7.2] def print_hello(): print("Hello!") print_hello() [図7.3] def print_hello(): print("Hello!") print_hello() print_hello() print_hello() [図7.5] def print_sentence(sentence): print(sentence) print_sentence("Hello!") [図7.6] def plus(a,b,c): print(a+b+c) plus(5,8,9) [図7.8] def test(): a = 10 print(a) a = 3 test() print(a) [図7.10] def plus(a,b,c): return a+b+c print(plus(2,3,10)) [図7.11] def even_odd(a): if a % 2 == 0: return "偶数" else: return "奇数" print(even_odd(10)) [図7.12] def div_check(a, b): if a % b == 0: return "割り切れます" else: return "割り切れません" print(div_check(10, 2)) print(div_check(10, 3)) ■■■8.グラフィックス------------------------------------------------------- ■■■ 8.1. Turtle Graphics------------------------------------------------------- [図8.1] !pip3 install ColabTurtle [図8.2] from ColabTurtle.Turtle import * [図8.3] initializeTurtle() [図8.4] initializeTurtle() forward(100) [図8.5] initializeTurtle() forward(100) right(90) forward(100) [図8.6] initializeTurtle() goto(100,200) goto(700,400) [図8.8] initializeTurtle() color("red") forward(100) color("blue") forward(100) [図8.9] initializeTurtle() forward(50) penup() forward(50) pendown() forward(50) [図8.10] initializeTurtle() forward(100) right(90) forward(100) right(90) forward(100) right(90) forward(100) right(90) [図8.11] initializeTurtle() for i in range(4): forward(100) right(90) [図8.12] def drawSquare(): for i in range(4): forward(100) right(90) initializeTurtle() drawSquare() penup() left(90) forward(200) pendown() drawSquare() [図8.15] initializeTurtle() right(30) for i in range(3): forward(100) right(120) left(120) backward(100) color("red") forward(200) backward(100) [図8.16] def drawTriangle(): for i in range(3): forward(100) right(120) initializeTurtle() drawTriangle() def drawTriangle(size): for i in range(3): forward(size) right(120) initializeTurtle() drawTriangle(200) drawTriangle(100) drawTriangle(50) ■■■8.グラフィックス------------------------------------------------------- [図8.18] initializeTurtle() speed(13) penup() center_x = 400 center_y = 250 radius = 50 for i in range(361): r = math.radians(i) x = center_x + radius * math.cos(r) y = center_y + radius * math.sin(r) goto(x,y) pendown() penup() [図8.21] initializeTurtle() speed(13) penup() def drawCircle(radius, center_x, center_y): for i in range(361): r = math.radians(i) x = center_x + radius * math.cos(r) y = center_y + radius * math.sin(r) goto(x,y) pendown() penup() drawCircle(100,150,150) drawCircle(200,400,250) drawCircle(50,150,300) ■■■9.幾何学模様-------------------------------------------------------- [図9.1] def drawCircle(radius, center_x, center_y): for i in range(46): r = math.radians(8 * i) x = center_x + radius * math.cos(r) y = center_y + radius * math.sin(r) goto(x,y) pendown() penup() [表9.1] initializeTurtle() speed(13) penup() radius = 40 center_x = 400 center_y = 250 for i in range(8): r = math.radians(i * 45) x = center_x + radius * math.cos(r) y = center_y + radius * math.sin(r) drawCircle(50,x,y) [図9.4] def drawCircle(radius, center_x, center_y): for i in range(46): r = math.radians(8 * i) x = center_x + radius * math.cos(r) y = center_y + radius * math.sin(r) goto(x,y) pendown() penup() def drawGeometory(radius, center_x, center_y): for i in range(8): r = math.radians(i * 45) x = center_x + radius * math.cos(r) y = center_y + radius * math.sin(r) drawCircle(50,x,y) initializeTurtle() speed(13) penup() drawGeometory(40,400,250) color("blue") drawGeometory(30,150,150) color("yellow") drawGeometory(60,600,300) ■■■10.デジタル単語帳--------------------------------------------------- [図10.2 ] user_text = input("テキストを入力してください:") print(user_text) [図10.3] import random print(random.randint(1,4)) [図10.4] import random words = ["リンゴ","みかん","バナナ"] answers = ["apple","orange","banana"] while len(words) >= 1: index = random.randint(0, len(words) - 1) print(words[index] + "を英語に訳してください") your_answer = input("答え:") if answers[index] == your_answer: print("正解!") words.pop(index) answers.pop(index) else: print("不正解...") print("全問正解!") [図10.10] import random words = ["リンゴ","みかん","バナナ"] answers = ["apple","orange","banana"] while len(words) >= 1: index = random.randint(0, len(words) - 1) print(words[index] + "を英語に訳してください") your_answer = input("答え:") if answers[index] == your_answer: print("正解!") words.pop(index) answers.pop(index) else: print("不正解です。" + answers[index] + "です") print("全問正解!") ■■■12.迷路-------------------------------------------------------- [図12.2] a = [[1,2,3],[4,5,6]] for i in range(len(a)): for j in range(len(a[i])): print(a[i][j]) print("---") [図12.3 ] def sigma(n): result = 0 for i in range(n+1): result = result + i return result print(sigma(5)) [図12.4 ] def sigma(n): if n == 0: return 0 else: return n + sigma(n-1) print(sigma(5)) [図12.12] import random words = ["apple","orange","banana"] print(random.choice(words)) [プログラム例] import random def display_maze(maze): for i in range(len(maze)): for j in range(len(maze[i])): if maze[i][j] == 3: print("S", end="") elif maze[i][j] == 4: print("G", end="") elif maze[i][j] == 2: print("■", end="") elif maze[i][j] == 1: print("■", end="") elif maze[i][j] == 0: print(" ", end="") print() def grid(): maze = [] for i in range(HEIGHT): row = [] for j in range(WIDTH): if i == 1 and j == 1: row.append(3) elif i == HEIGHT - 2 and j == WIDTH - 2: row.append(4) elif i == 0 or j == 0 or i == HEIGHT - 1 or j == WIDTH - 1: row.append(2) elif i % 2 == 0 and j % 2 == 0: row.append(1) else: row.append(0) maze.append(row) return maze HEIGHT = 11 WIDTH = 11 display_maze(grid()) [12.6迷路生成プログラムの作成] import random def display_maze(maze): for i in range(len(maze)): for j in range(len(maze[i])): if maze[i][j] == 3: print("S", end="") elif maze[i][j] == 4: print("G", end="") elif maze[i][j] == 2: print("■", end="") elif maze[i][j] == 1: print("■", end="") elif maze[i][j] == 0: print(" ", end="") print() def grid(): maze = [] for i in range(HEIGHT): row = [] for j in range(WIDTH): if i == 1 and j == 1: row.append(3) elif i == HEIGHT - 2 and j == WIDTH - 2: row.append(4) elif i == 0 or j == 0 or i == HEIGHT - 1 or j == WIDTH - 1: row.append(2) elif i % 2 == 0 and j % 2 == 0: row.append(1) else: row.append(0) maze.append(row) return maze def create_maze(): maze = grid() for i in range(HEIGHT - 1): for j in range(WIDTH - 1): if i % 2 == 0 and j % 2 == 0 and i != 0 and j != 0: directions = [] if i == 2: directions.append([i - 1, j]) if maze[i+1][j] == 0: directions.append([i + 1, j]) if maze[i][j-1] == 0: directions.append([i, j - 1]) if maze[i][j+1] == 0: directions.append([i, j + 1]) wall = random.choice(directions) maze[wall[0]][wall[1]] = 1 return maze HEIGHT = 11 WIDTH = 11 display_maze(create_maze()) [12.8迷路の解法プログラム] import random def display_maze(maze, path): for i in range(len(maze)): for j in range(len(maze[i])): if [i, j] in path: print("#", end="") elif maze[i][j] == 3: print("S", end="") elif maze[i][j] == 4: print("G", end="") elif maze[i][j] == 2: print("■", end="") elif maze[i][j] == 1: print("■", end="") elif maze[i][j] == 0: print(" ", end="") print() def grid(): maze = [] for i in range(HEIGHT): row = [] for j in range(WIDTH): if i == 1 and j == 1: row.append(3) elif i == HEIGHT - 2 and j == WIDTH - 2: row.append(4) elif i == 0 or j == 0 or i == HEIGHT - 1 or j == WIDTH - 1: row.append(2) elif i % 2 == 0 and j % 2 == 0: row.append(1) else: row.append(0) maze.append(row) return maze def create_maze(): maze = grid() for i in range(HEIGHT - 1): for j in range(WIDTH - 1): if i % 2 == 0 and j % 2 == 0 and i != 0 and j != 0: directions = [] if i == 2: directions.append([i - 1, j]) if maze[i+1][j] == 0: directions.append([i + 1, j]) if maze[i][j-1] == 0: directions.append([i, j - 1]) if maze[i][j+1] == 0: directions.append([i, j + 1]) wall = random.choice(directions) maze[wall[0]][wall[1]] = 1 return maze def search(maze, maze_search, pos): d = [[0, 1], [1, 0], [0, -1], [-1, 0]] for i in d: next_pos = [pos[0] + i[0], pos[1] + i[1]] maze_condition = maze[next_pos[0]][next_pos[1]] if maze_condition == 4: maze_search[next_pos[0]][next_pos[1]] = 1 return [1, []] if maze_condition == 0 and maze_search[next_pos[0]][next_pos[1]] == 0: maze_search[next_pos[0]][next_pos[1]] = 1 result = search(maze, maze_search, [next_pos[0], next_pos[1]]) if result[0] == 1: path = result[1] path.insert(0, next_pos) return [1, path] return def solve_maze(maze): maze_search = [] for i in range(HEIGHT): row = [] for j in range(WIDTH): row.append(0) maze_search.append(row) result = search(maze, maze_search, [1, 1]) if result[0] == 0: return "cannot_goal" return result[1] HEIGHT = 11 WIDTH = 11 maze = create_maze() path = solve_maze(maze) display_maze(maze, solve_maze)