├── README.md ├── codes ├── .DS_Store ├── 10_1_decrypt_message.py ├── 10_2_decrypt_without_key.py ├── 10_find_and_remove.py ├── 11_decrypt_message_with_dict.py ├── 11_exercise_count_numbers.py ├── 12_1_rect_function.py ├── 12_2_find_and_remove_function.py ├── 12_exercise_cum_sum.py ├── 12_exercise_is_prime.py ├── 13_exercise_fibo.py ├── 13_exercise_hano.py ├── 13_exercise_walk.py ├── 1_1_swap_values_of_two_variables.py ├── 1_2_flip_a_number_with_four_digits.py ├── 1_3_pay_the_bill.py ├── 1_4_growing_trees.py ├── 2_1_flip_a_float.py ├── 2_2_radius_of_circle.py ├── 2_exercise_invest.py ├── 3_1_simplified_grade.py ├── 3_2_simple_calculator.py ├── 3_exercise_grade.py ├── 4_execise_year.py ├── 4_exercise_questions.py ├── 5_1_guess_the_number.py ├── 5_2_daffodil.py ├── 5_3_fibo.py ├── 5_exercise_factorial.py ├── 5_exercise_sum.py ├── 6_1_pentangle.py ├── 6_2_regular_polygon.py ├── 6_exercise_max_number.py ├── 7_exercise_prime.py ├── 7_turtle_example_1.py ├── 7_turtle_example_2.py ├── 7_turtle_example_3.py ├── 7_turtle_race_control_with_text.py ├── 7_turtle_race_random_with_text.py ├── 8_midterm.py ├── 9_1_no_list_average.py ├── 9_2_remove_high_and_low.py └── 9_exercise_double.py ├── ppt ├── .DS_Store ├── 00_Introduction.pdf ├── 01_Variable.pdf ├── 02_Data_Type.pdf ├── 03_Condition_1.pdf ├── 04_Condition_2.pdf ├── 05_Loop_1.pdf ├── 06_Loop_2.pdf ├── 07_Loop_3.pdf ├── 08_Midterm.pdf ├── 09_List.pdf ├── 10_String.pdf ├── 11_Dictionary.pdf ├── 12_Function_1.pdf └── 13_Function_2.pdf ├── python-3.6.2-amd64.exe └── python-3.6.2-macosx10.6.pkg /README.md: -------------------------------------------------------------------------------- 1 | ## Python-Basic 2 | 3 | Python基础入门课程,使用Python3.6自带的IDLE编程 4 | 5 | 介绍Python基础语法,包括变量、类型、条件、循环、列表、字符串、字典、函数等内容 6 | 7 | ## 课程链接 8 | 9 | 网易云课堂:[http://study.163.com/course/courseMain.htm?courseId=1005461017](http://study.163.com/course/courseMain.htm?courseId=1005461017) 10 | 11 | ## 文件说明 12 | 13 | - `ppt`:课程课件,共14个pdf; 14 | - `codes`:课程代码,文件名带exercise表示课堂练习,不带表示课后习题; 15 | - `python-3.6.2-amd64.exe`:Windows下Python3.6安装文件; 16 | - `python-3.6.2-macosx10.6.pkg`:Mac下Python3.6安装文件。 -------------------------------------------------------------------------------- /codes/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Honlan/Python-Basic/4ac263fa3b3b3ddd08f7cba389b5c8c183672a13/codes/.DS_Store -------------------------------------------------------------------------------- /codes/10_1_decrypt_message.py: -------------------------------------------------------------------------------- 1 | alphabet = 'abcdefghijklmnopqrstuvwxyz' 2 | 3 | key = int(input('> Please input the Key: ')) 4 | msg = input('> Please input a message: ') 5 | msg = msg.lower() 6 | 7 | new_msg = '' 8 | for c in msg: 9 | pos = alphabet.find(c) 10 | if pos == -1: 11 | new_msg += c 12 | else: 13 | pos = (pos + key) % len(alphabet) 14 | new_msg += alphabet[pos] 15 | 16 | print('The encrypted message is: ', new_msg) 17 | 18 | old_msg = '' 19 | for c in new_msg: 20 | pos = alphabet.find(c) 21 | if pos == -1: 22 | old_msg += c 23 | else: 24 | pos = (pos - key) % len(alphabet) 25 | old_msg += alphabet[pos] 26 | 27 | print('The encrypted message is: ', old_msg) 28 | 29 | -------------------------------------------------------------------------------- /codes/10_2_decrypt_without_key.py: -------------------------------------------------------------------------------- 1 | msg = "Kyv wLIKyvJK uzJKrEtv zE Kyv NFICu, zJ EFK svKNvvE Czwv rEu uvrKy. sLK NyvE z JKrEu zE wIFEK Fw PFL, PvK PFL uFE'K BEFN KyrK z CFMv PFL" 2 | alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' 3 | 4 | for key in range(len(alphabet)): 5 | old_msg = '' 6 | for c in msg: 7 | pos = alphabet.find(c) 8 | if pos == -1: 9 | old_msg += c 10 | else: 11 | pos = (pos - key) % len(alphabet) 12 | old_msg += alphabet[pos] 13 | print('The Key: ', key) 14 | print('The decrypted message is: ', old_msg) 15 | -------------------------------------------------------------------------------- /codes/10_find_and_remove.py: -------------------------------------------------------------------------------- 1 | s1 = '今天天气非常非常的好' 2 | s2 = '非常的' 3 | 4 | while True: 5 | pos = s1.find(s2) 6 | if pos == -1: 7 | break 8 | 9 | s_before = s1[:pos] 10 | s_after = s1[pos + len(s2):] 11 | s1 = s_before + s_after 12 | print(s1) 13 | -------------------------------------------------------------------------------- /codes/11_decrypt_message_with_dict.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | alphabet = 'a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z' 4 | 5 | base = alphabet.split(',') 6 | target = base.copy() 7 | random.shuffle(target) 8 | 9 | cipher = {} 10 | d_cipher = {} 11 | for i in range(len(base)): 12 | cipher[base[i]] = target[i] 13 | d_cipher[target[i]] = base[i] 14 | # d_cipher = {v: k for k, v in cipher.items()} 15 | 16 | msg = input('Please input a message: ') 17 | new_msg = '' 18 | for c in msg: 19 | new_msg = new_msg + cipher.get(c, c) 20 | print('The secret message is: ', new_msg) 21 | 22 | plain_msg = '' 23 | for c in new_msg: 24 | plain_msg = plain_msg + d_cipher.get(c, c) 25 | 26 | print('The plain message is: ', plain_msg) 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /codes/11_exercise_count_numbers.py: -------------------------------------------------------------------------------- 1 | numbers = input('Please input a lots of numbers: ') 2 | count1 = {} 3 | count2 = {} 4 | count2['oushu'] = 0 5 | count2['jishu'] = 0 6 | 7 | for c in numbers: 8 | if c in count1: 9 | count1[c] += 1 10 | else: 11 | count1[c] = 1 12 | 13 | if int(c) % 2 == 0: 14 | count2['oushu'] += 1 15 | else: 16 | count2['jishu'] += 1 17 | 18 | print(count1) 19 | print(count2) 20 | 21 | -------------------------------------------------------------------------------- /codes/12_1_rect_function.py: -------------------------------------------------------------------------------- 1 | def rect_cal(width, height): 2 | area = width * height 3 | zhouchang = (width + height) * 2 4 | return area, zhouchang 5 | 6 | width = float(input('Please input the width: ')) 7 | height = float(input('Please input the height: ')) 8 | 9 | print(rect_cal(width, height)) 10 | -------------------------------------------------------------------------------- /codes/12_2_find_and_remove_function.py: -------------------------------------------------------------------------------- 1 | def find_and_remove(s1, s2, flag): 2 | pos = s1.find(s2) 3 | if pos == -1: 4 | return s1 5 | 6 | if flag: 7 | # 全部删除 8 | while True: 9 | pos = s1.find(s2) 10 | if pos == -1: 11 | break 12 | s1 = s1[:pos] + s1[pos + len(s2):] 13 | return s1 14 | else: 15 | # 只删一次 16 | return s1[:pos] + s1[pos + len(s2):] 17 | 18 | print(find_and_remove('你好啊你好啊', '我不好', True)) 19 | print(find_and_remove('今天天气非常非常非常的好', '非常', False)) 20 | print(find_and_remove('今天天气非常非常非常的好', '非常', True)) 21 | print('=' * 10) 22 | print('今天天气非常非常非常的好'.replace('非常', '')) 23 | print('今天天气非常非常非常的好'.replace('非常', '', 1)) 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /codes/12_exercise_cum_sum.py: -------------------------------------------------------------------------------- 1 | def cum_sum(N): 2 | s = 0 3 | for i in range(N): 4 | s = s + i + 1 5 | print(s) 6 | 7 | cum_sum(5) 8 | cum_sum(10) 9 | cum_sum(15) 10 | -------------------------------------------------------------------------------- /codes/12_exercise_is_prime.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | def is_prime(n): 4 | if n == 1: 5 | return False 6 | 7 | flag = True 8 | for i in range(2, int(math.sqrt(n))): 9 | if n % i == 0: 10 | flag = False 11 | break 12 | if flag: 13 | return True 14 | else: 15 | return False 16 | 17 | def get_primes(N): 18 | result = [] 19 | for i in range(1, N + 1): 20 | if is_prime(i): 21 | result.append(i) 22 | 23 | return result 24 | 25 | 26 | 27 | 28 | print(get_primes(1)) 29 | print(get_primes(20)) 30 | print(get_primes(100)) 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /codes/13_exercise_fibo.py: -------------------------------------------------------------------------------- 1 | def fibo(N): 2 | if N <= 2: 3 | return 1 4 | else: 5 | return fibo(N - 1) + fibo(N - 2) 6 | 7 | print(fibo(8)) 8 | -------------------------------------------------------------------------------- /codes/13_exercise_hano.py: -------------------------------------------------------------------------------- 1 | def hano(N, source, target): 2 | if N == 1: 3 | print(1, source, '=>', target) 4 | else: 5 | pillars = ['A', 'B', 'C'] 6 | pillars.remove(source) 7 | pillars.remove(target) 8 | media = pillars[0] 9 | 10 | hano(N - 1, source, media) 11 | print(N, source, '=>', target) 12 | hano(N - 1, media, target) 13 | 14 | hano(4, 'B', 'C') 15 | -------------------------------------------------------------------------------- /codes/13_exercise_walk.py: -------------------------------------------------------------------------------- 1 | def walk(N): 2 | if N == 1: 3 | return 1 4 | elif N == 2: 5 | return 2 6 | else: 7 | return walk(N - 1) + walk(N - 2) 8 | 9 | print(walk(20)) 10 | -------------------------------------------------------------------------------- /codes/1_1_swap_values_of_two_variables.py: -------------------------------------------------------------------------------- 1 | # swap the values of two variables 2 | num1 = 10 3 | num2 = 5 4 | num3 = num1 5 | num1 = num2 6 | num2 = num3 7 | print(num1, num2) 8 | -------------------------------------------------------------------------------- /codes/1_2_flip_a_number_with_four_digits.py: -------------------------------------------------------------------------------- 1 | # flip a number of four digits 2 | num = 1234 3 | n0 = num % 10 4 | num = (num - n0) / 10 5 | n1 = num % 10 6 | num = (num - n1) / 10 7 | n2 = num % 10 8 | num = (num - n2) / 10 9 | n3 = num % 10 10 | num = (num - n3) / 10 11 | num = n0 * 1000 + n1 * 100 + n2 * 10 + n3 * 1 12 | print(num) 13 | -------------------------------------------------------------------------------- /codes/1_3_pay_the_bill.py: -------------------------------------------------------------------------------- 1 | tudousi = 18 2 | shuizhuroupian = 48 3 | suancaiyu = 68 4 | juice = 10 5 | total = tudousi + shuizhuroupian + suancaiyu + juice * 4 6 | print(total / 3) 7 | -------------------------------------------------------------------------------- /codes/1_4_growing_trees.py: -------------------------------------------------------------------------------- 1 | # problem 1 2 | tree = 1 3 | grow = 0.1 4 | month = 12 5 | height1 = tree + grow * month 6 | print(height1) 7 | 8 | # problem 2 9 | grow = 10 / 100 10 | height2 = tree * (1 + grow) ** month 11 | print(height2) 12 | -------------------------------------------------------------------------------- /codes/2_1_flip_a_float.py: -------------------------------------------------------------------------------- 1 | my_str = input("Please input a number") 2 | my_str = float(my_str[::-1]) 3 | my_str = float(my_str) 4 | print(my_str) 5 | -------------------------------------------------------------------------------- /codes/2_2_radius_of_circle.py: -------------------------------------------------------------------------------- 1 | r = float(input("Please input the radius: ")) 2 | PI = 3.1415926 3 | area = PI * r ** 2 4 | print("The area:", area) 5 | zhouchang = 2 * PI * r 6 | print("zhouchang:", zhouchang) 7 | -------------------------------------------------------------------------------- /codes/2_exercise_invest.py: -------------------------------------------------------------------------------- 1 | money = float(input("How much money")) 2 | month = float(input("How many months")) 3 | rate = float(input("How much rate")) 4 | rate = rate / 100 5 | total = money * (1 + rate) ** month 6 | interest = total - money 7 | print(interest) 8 | -------------------------------------------------------------------------------- /codes/3_1_simplified_grade.py: -------------------------------------------------------------------------------- 1 | score = int(float(input("Please input an integer:"))) 2 | print(score) 3 | 4 | if score > 100 or score < 0: 5 | print("Wrong Score") 6 | elif score >= 90: 7 | print("Excellent") 8 | elif score >= 80: 9 | print("Good") 10 | elif score >= 70: 11 | print("Medium") 12 | elif score >= 60: 13 | print("Passed") 14 | else: 15 | print("Failed") 16 | 17 | -------------------------------------------------------------------------------- /codes/3_2_simple_calculator.py: -------------------------------------------------------------------------------- 1 | error = False 2 | 3 | try: 4 | num1 = float(input("the first number: ")) 5 | except: 6 | print("Please input a number") 7 | error = True 8 | try: 9 | num2 = float(input("the second number: ")) 10 | except: 11 | print("Please input a number") 12 | error = True 13 | 14 | op = input("the operator(+ - * / **):") 15 | 16 | if error: 17 | print("Something Wrong") 18 | else: 19 | if num2 == 0 and op == '/': 20 | print("The division can't be 0") 21 | elif op == '+': 22 | print(num1 + num2) 23 | elif op == '-': 24 | print(num1 - num2) 25 | elif op == '*': 26 | print(num1 * num2) 27 | elif op == '/': 28 | print(num1 / num2) 29 | elif op == '**': 30 | print(num1 ** num2) 31 | else: 32 | print("Unknown Operator") 33 | -------------------------------------------------------------------------------- /codes/3_exercise_grade.py: -------------------------------------------------------------------------------- 1 | score = int(float(input("Please input an integer:"))) 2 | print(score) 3 | 4 | if score > 100: 5 | print("Wrong Score") 6 | elif score >= 90: 7 | print("Excellent") 8 | elif score >= 80: 9 | print("Good") 10 | elif score >= 70: 11 | print("Medium") 12 | elif score >= 60: 13 | print("Passed") 14 | elif score >= 0: 15 | print("Failed") 16 | else: 17 | print("Wrong Score") 18 | 19 | -------------------------------------------------------------------------------- /codes/4_execise_year.py: -------------------------------------------------------------------------------- 1 | year = int(input("输入年份,四位整数:")) 2 | 3 | ''' 4 | year = (year - 1996) % 12 5 | 6 | if year == 0: 7 | print('鼠') 8 | elif year == 1: 9 | print('牛') 10 | elif year == 2: 11 | print('虎') 12 | elif year == 3: 13 | print('兔') 14 | elif year == 4: 15 | print('龙') 16 | elif year == 5: 17 | print('蛇') 18 | elif year == 6: 19 | print('马') 20 | elif year == 7: 21 | print('羊') 22 | elif year == 8: 23 | print('猴') 24 | elif year == 9: 25 | print('鸡') 26 | elif year == 10: 27 | print('狗') 28 | elif year == 11: 29 | print('猪') 30 | ''' 31 | 32 | animals = ['鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪'] 33 | print(animals[(year - 1996) % 12]) 34 | -------------------------------------------------------------------------------- /codes/4_exercise_questions.py: -------------------------------------------------------------------------------- 1 | color = input("我最喜欢的颜色:") 2 | color1 = "蓝色" 3 | gender = input("我的性别:") 4 | gender1 = "男" 5 | fruit = input("我最喜欢的水果:") 6 | fruit1 = "西瓜" 7 | birth = input("我的生日,例如03.26:") 8 | birth1 = "03.26" 9 | game = input("我最喜欢的游戏:") 10 | game1 = "王者荣耀" 11 | 12 | score = 0 13 | 14 | if color == color1: 15 | score = score + 1 16 | # score += 1 17 | if gender == gender1: 18 | score += 1 19 | if fruit == fruit1: 20 | score += 1 21 | if birth == birth1: 22 | score += 1 23 | if game == game1: 24 | score += 1 25 | 26 | print(score) 27 | 28 | if score == 5: 29 | print('5 分') 30 | elif score == 4: 31 | print('4 分') 32 | elif score == 3: 33 | print('3 分') 34 | elif score == 2: 35 | print('2 分') 36 | elif score == 1: 37 | print('1 分') 38 | elif score == 0: 39 | print('0 分') 40 | 41 | -------------------------------------------------------------------------------- /codes/5_1_guess_the_number.py: -------------------------------------------------------------------------------- 1 | import random 2 | print("The number is between 0 and 100") 3 | answer = random.randrange(0, 101) 4 | count = 0 5 | 6 | while True: 7 | count += 1 8 | 9 | n = int(input("Input the number: ")) 10 | if n > answer: 11 | print('too big') 12 | elif n < answer: 13 | print('too small') 14 | else: 15 | print('ok', answer, count) 16 | break 17 | -------------------------------------------------------------------------------- /codes/5_2_daffodil.py: -------------------------------------------------------------------------------- 1 | A = 1 2 | B = 0 3 | C = 0 4 | while True: 5 | res = 100 * A + 10 * B + C 6 | if res == A ** 3 + B ** 3 + C ** 3: 7 | print(res) 8 | C += 1 9 | if C == 10: 10 | C = 0 11 | B += 1 12 | if B == 10: 13 | B = 0 14 | A += 1 15 | if A == 10: 16 | break 17 | 18 | n = 100 19 | while n <= 999: 20 | ans = n 21 | 22 | n0 = ans % 10 23 | ans = (ans - n0) / 10 24 | n1 = ans % 10 25 | ans = (ans - n1) / 10 26 | n2 = ans 27 | 28 | if n0 ** 3 + n1 ** 3 + n2 ** 3 == n: 29 | print(n) 30 | 31 | n += 1 32 | 33 | -------------------------------------------------------------------------------- /codes/5_3_fibo.py: -------------------------------------------------------------------------------- 1 | first = 1 2 | second = 1 3 | print(first) 4 | print(second) 5 | turn = 28 6 | while True: 7 | third =first + second 8 | print(third) 9 | first = second 10 | second = third 11 | turn -= 1 12 | if turn == 0: 13 | break 14 | -------------------------------------------------------------------------------- /codes/5_exercise_factorial.py: -------------------------------------------------------------------------------- 1 | # from big to small 2 | n = int(input("n: ")) 3 | s = 1 4 | while n > 0: 5 | s = s * n 6 | n = n - 1 7 | print(s) 8 | 9 | # from small to big 10 | n = int(input("n: ")) 11 | i = 1 12 | s = 1 13 | while i <= n: 14 | s = s * i 15 | i = i + 1 16 | print(s) 17 | -------------------------------------------------------------------------------- /codes/5_exercise_sum.py: -------------------------------------------------------------------------------- 1 | # from big to small 2 | n = int(input("n: ")) 3 | s = 0 4 | while n > 0: 5 | s = s + n 6 | n = n - 1 7 | print(s) 8 | 9 | # from small to big 10 | n = int(input("n: ")) 11 | i = 1 12 | s = 0 13 | while i <= n: 14 | s = s + i 15 | i = i + 1 16 | print(s) 17 | -------------------------------------------------------------------------------- /codes/6_1_pentangle.py: -------------------------------------------------------------------------------- 1 | import turtle 2 | 3 | t = turtle.Turtle() 4 | for i in range(5): 5 | t.forward(100) 6 | t.left(144) 7 | -------------------------------------------------------------------------------- /codes/6_2_regular_polygon.py: -------------------------------------------------------------------------------- 1 | n = int(input("Please input the edges: ")) 2 | import turtle 3 | 4 | t = turtle.Turtle() 5 | 6 | for i in range(n): 7 | t.forward(50) 8 | # t.left(360 / n) 9 | t.left(180 - 180 * (n - 2) / n) 10 | 11 | -------------------------------------------------------------------------------- /codes/6_exercise_max_number.py: -------------------------------------------------------------------------------- 1 | import math 2 | largest_so_far = -math.inf 3 | 4 | numbers = [3, 14, -5, 42, 98, 5] 5 | for i in numbers: 6 | if i > largest_so_far: 7 | largest_so_far = i 8 | print(largest_so_far) 9 | print(i) 10 | -------------------------------------------------------------------------------- /codes/7_exercise_prime.py: -------------------------------------------------------------------------------- 1 | for N in range(2, 1001): 2 | f = True 3 | for i in range(2, N): 4 | if N % i == 0: 5 | f = False 6 | break 7 | 8 | if f: 9 | print(N, 'is a Prime') 10 | -------------------------------------------------------------------------------- /codes/7_turtle_example_1.py: -------------------------------------------------------------------------------- 1 | import turtle 2 | 3 | t = turtle.Turtle() 4 | 5 | for i in range(100): 6 | t.forward(i * 5) 7 | t.right(144) 8 | -------------------------------------------------------------------------------- /codes/7_turtle_example_2.py: -------------------------------------------------------------------------------- 1 | import turtle 2 | 3 | t = turtle.Turtle() 4 | 5 | t.pencolor("blue") 6 | 7 | for i in range(50): 8 | t.forward(50) 9 | t.left(123) 10 | 11 | t.pencolor("red") 12 | for i in range(50): 13 | t.forward(100) 14 | t.left(123) 15 | -------------------------------------------------------------------------------- /codes/7_turtle_example_3.py: -------------------------------------------------------------------------------- 1 | import turtle 2 | 3 | t = turtle.Turtle() 4 | t.speed(10) 5 | 6 | for i in range(180): 7 | t.forward(100) 8 | t.right(30) 9 | t.forward(20) 10 | t.left(60) 11 | t.forward(50) 12 | t.right(30) 13 | 14 | t.penup() 15 | t.setposition(0, 0) 16 | t.pendown() 17 | 18 | t.right(2) 19 | -------------------------------------------------------------------------------- /codes/7_turtle_race_control_with_text.py: -------------------------------------------------------------------------------- 1 | import turtle 2 | import random 3 | import time 4 | 5 | t = turtle.Turtle() 6 | t.speed(10) 7 | t.penup() 8 | t.goto(-200, 150) 9 | for i in range(21): 10 | t.write(i) 11 | t.right(90) 12 | t.pendown() 13 | t.forward(300) 14 | t.penup() 15 | t.backward(300) 16 | t.left(90) 17 | t.forward(20) 18 | 19 | alice = turtle.Turtle() 20 | alice.shape('turtle') 21 | alice.color('red') 22 | alice.penup() 23 | alice.goto(-220, 100) 24 | 25 | ben = turtle.Turtle() 26 | ben.shape('turtle') 27 | ben.color('blue') 28 | ben.penup() 29 | ben.goto(-220, 0) 30 | 31 | claire = turtle.Turtle() 32 | claire.shape('turtle') 33 | claire.color('green') 34 | claire.penup() 35 | claire.goto(-220, -100) 36 | 37 | global max_speed_a, max_speed_b, max_speed_c, terminal, order 38 | max_speed_a = 10 39 | max_speed_b = 10 40 | max_speed_c = 10 41 | terminal = 200 42 | order = 0 43 | 44 | def key_a(): 45 | global max_speed_a, max_speed_b, max_speed_c, terminal, order 46 | alice.forward(random.randrange(max_speed_a)) 47 | if alice.xcor() >= terminal and max_speed_a > 1: 48 | max_speed_a = 1 49 | t.goto(240, 90) 50 | if order == 0: 51 | t.write('冠军', font=('Arial', 20, 'normal')) 52 | elif order == 1: 53 | t.write('亚军', font=('Arial', 20, 'normal')) 54 | elif order == 2: 55 | t.write('季军', font=('Arial', 20, 'normal')) 56 | order = order + 1 57 | 58 | def key_b(): 59 | global max_speed_a, max_speed_b, max_speed_c, terminal, order 60 | ben.forward(random.randrange(max_speed_b)) 61 | if ben.xcor() >= terminal and max_speed_b > 1: 62 | max_speed_b = 1 63 | t.goto(240, -10) 64 | if order == 0: 65 | t.write('冠军', font=('Arial', 20, 'normal')) 66 | elif order == 1: 67 | t.write('亚军', font=('Arial', 20, 'normal')) 68 | elif order == 2: 69 | t.write('季军', font=('Arial', 20, 'normal')) 70 | order = order + 1 71 | 72 | def key_c(): 73 | global max_speed_a, max_speed_b, max_speed_c, terminal, order 74 | claire.forward(random.randrange(max_speed_c)) 75 | if claire.xcor() >= terminal and max_speed_c > 1: 76 | max_speed_c = 1 77 | t.goto(240, -110) 78 | if order == 0: 79 | t.write('冠军', font=('Arial', 20, 'normal')) 80 | elif order == 1: 81 | t.write('亚军', font=('Arial', 20, 'normal')) 82 | elif order == 2: 83 | t.write('季军', font=('Arial', 20, 'normal')) 84 | order = order + 1 85 | 86 | s = turtle.Screen() 87 | s.onkey(key_a, 'a') 88 | s.onkey(key_b, 'b') 89 | s.onkey(key_c, 'c') 90 | s.listen() 91 | 92 | 93 | -------------------------------------------------------------------------------- /codes/7_turtle_race_random_with_text.py: -------------------------------------------------------------------------------- 1 | import turtle 2 | import random 3 | import time 4 | 5 | t = turtle.Turtle() 6 | t.speed(10) 7 | t.penup() 8 | t.goto(-200, 150) 9 | for i in range(21): 10 | t.write(i) 11 | t.right(90) 12 | t.pendown() 13 | t.forward(300) 14 | t.penup() 15 | t.backward(300) 16 | t.left(90) 17 | t.forward(20) 18 | 19 | alice = turtle.Turtle() 20 | alice.shape('turtle') 21 | alice.color('red') 22 | alice.penup() 23 | alice.goto(-220, 100) 24 | 25 | ben = turtle.Turtle() 26 | ben.shape('turtle') 27 | ben.color('blue') 28 | ben.penup() 29 | ben.goto(-220, 0) 30 | 31 | claire = turtle.Turtle() 32 | claire.shape('turtle') 33 | claire.color('green') 34 | claire.penup() 35 | claire.goto(-220, -100) 36 | 37 | max_speed_a = 10 38 | max_speed_b = 10 39 | max_speed_c = 10 40 | terminal = 200 41 | order = 0 42 | 43 | while True: 44 | alice.forward(random.randrange(max_speed_a)) 45 | ben.forward(random.randrange(max_speed_b)) 46 | claire.forward(random.randrange(max_speed_c)) 47 | time.sleep(0.1) 48 | if alice.xcor() >= terminal and max_speed_a > 1: 49 | max_speed_a = 1 50 | t.goto(240, 90) 51 | if order == 0: 52 | t.write('冠军', font=('Arial', 20, 'normal')) 53 | elif order == 1: 54 | t.write('亚军', font=('Arial', 20, 'normal')) 55 | elif order == 2: 56 | t.write('季军', font=('Arial', 20, 'normal')) 57 | order = order + 1 58 | if ben.xcor() >= terminal and max_speed_b > 1: 59 | max_speed_b = 1 60 | t.goto(240, -10) 61 | if order == 0: 62 | t.write('冠军', font=('Arial', 20, 'normal')) 63 | elif order == 1: 64 | t.write('亚军', font=('Arial', 20, 'normal')) 65 | elif order == 2: 66 | t.write('季军', font=('Arial', 20, 'normal')) 67 | order = order + 1 68 | if claire.xcor() >= terminal and max_speed_c > 1: 69 | max_speed_c = 1 70 | t.goto(240, -110) 71 | if order == 0: 72 | t.write('冠军', font=('Arial', 20, 'normal')) 73 | elif order == 1: 74 | t.write('亚军', font=('Arial', 20, 'normal')) 75 | elif order == 2: 76 | t.write('季军', font=('Arial', 20, 'normal')) 77 | order = order + 1 78 | if order == 3: 79 | break -------------------------------------------------------------------------------- /codes/8_midterm.py: -------------------------------------------------------------------------------- 1 | print('Question 1') 2 | a = int(input('Number A: ')) 3 | b = int(input('Number B: ')) 4 | c = int(input('Number C: ')) 5 | 6 | if a > b: 7 | a, b = b, a 8 | if a > c: 9 | a, c = c, a 10 | if b > c: 11 | b, c = c, b 12 | print(a, b, c) 13 | 14 | 15 | print('Question 2') 16 | s = 0 17 | for i in range(1, 100): 18 | if i % 2 == 1: 19 | s = s + i 20 | else: 21 | s = s - i 22 | print(s) 23 | 24 | 25 | print('Question 3') 26 | number = input('A five digit number: ') 27 | if number[::-1] == number: 28 | print('Yes') 29 | else: 30 | print('No') 31 | 32 | 33 | print('Question 4') 34 | import math 35 | 36 | n = 1 37 | while True: 38 | n1 = n + 100 39 | n2 = n1 + 168 40 | 41 | n1 = math.sqrt(n1) 42 | n2 = math.sqrt(n2) 43 | 44 | if int(n1) == n1 and int(n2) == n2: 45 | print(n) 46 | break 47 | 48 | n = n + 1 49 | 50 | 51 | print('Question 5') 52 | result = 0 53 | for number in range(1, 21): 54 | s = 1 55 | for n in range(1, number + 1): 56 | s = s * n 57 | result = result + s 58 | print(result) 59 | 60 | 61 | print('Question 6') 62 | import turtle 63 | t = turtle.Turtle() 64 | 65 | t.color('red') 66 | t.penup() 67 | t.goto(0, -100) 68 | t.pendown() 69 | t.circle(100) 70 | t.forward(100) 71 | t.left(90) 72 | for i in range(3): 73 | t.forward(200) 74 | t.left(90) 75 | t.forward(100 + 150) 76 | t.right(90) 77 | t.forward(50) 78 | t.right(90) 79 | t.forward(300) 80 | t.right(90) 81 | t.forward(50) 82 | t.right(90) 83 | t.forward(50) 84 | t.penup() 85 | t.goto(25, 100) 86 | t.pendown() 87 | t.seth(90) 88 | t.forward(75) 89 | t.left(90) 90 | t.forward(50) 91 | t.left(90) 92 | t.forward(75) 93 | -------------------------------------------------------------------------------- /codes/9_1_no_list_average.py: -------------------------------------------------------------------------------- 1 | count = 0 2 | s = 0 3 | while True: 4 | inp = input('Enter a number: ') 5 | if inp == 'done': 6 | break 7 | value = float(inp) 8 | count = count + 1 9 | s = s + value 10 | average = s / count 11 | print('Average: ', average) 12 | -------------------------------------------------------------------------------- /codes/9_2_remove_high_and_low.py: -------------------------------------------------------------------------------- 1 | numlist = list() 2 | while True: 3 | inp = input('Enter a number: ') 4 | if inp == 'done': 5 | break 6 | value = float(inp) 7 | numlist.append(value) 8 | 9 | # average = (sum(numlist) - max(numlist) - min(numlist)) / (len(numlist) - 2) 10 | 11 | # numlist.remove(max(numlist)) 12 | # numlist.remove(min(numlist)) 13 | # average = sum(numlist) / len(numlist) 14 | 15 | # numlist.sort() 16 | # numlist = numlist[1:-1] 17 | # average = sum(numlist) / len(numlist) 18 | 19 | print('Average: ', average) 20 | 21 | -------------------------------------------------------------------------------- /codes/9_exercise_double.py: -------------------------------------------------------------------------------- 1 | nums = [5, 4, 3, 2, 1] 2 | 3 | for i in range(len(nums)): 4 | nums[i] = nums[i] * 2 5 | 6 | print(nums) 7 | -------------------------------------------------------------------------------- /ppt/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Honlan/Python-Basic/4ac263fa3b3b3ddd08f7cba389b5c8c183672a13/ppt/.DS_Store -------------------------------------------------------------------------------- /ppt/00_Introduction.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Honlan/Python-Basic/4ac263fa3b3b3ddd08f7cba389b5c8c183672a13/ppt/00_Introduction.pdf -------------------------------------------------------------------------------- /ppt/01_Variable.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Honlan/Python-Basic/4ac263fa3b3b3ddd08f7cba389b5c8c183672a13/ppt/01_Variable.pdf -------------------------------------------------------------------------------- /ppt/02_Data_Type.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Honlan/Python-Basic/4ac263fa3b3b3ddd08f7cba389b5c8c183672a13/ppt/02_Data_Type.pdf -------------------------------------------------------------------------------- /ppt/03_Condition_1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Honlan/Python-Basic/4ac263fa3b3b3ddd08f7cba389b5c8c183672a13/ppt/03_Condition_1.pdf -------------------------------------------------------------------------------- /ppt/04_Condition_2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Honlan/Python-Basic/4ac263fa3b3b3ddd08f7cba389b5c8c183672a13/ppt/04_Condition_2.pdf -------------------------------------------------------------------------------- /ppt/05_Loop_1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Honlan/Python-Basic/4ac263fa3b3b3ddd08f7cba389b5c8c183672a13/ppt/05_Loop_1.pdf -------------------------------------------------------------------------------- /ppt/06_Loop_2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Honlan/Python-Basic/4ac263fa3b3b3ddd08f7cba389b5c8c183672a13/ppt/06_Loop_2.pdf -------------------------------------------------------------------------------- /ppt/07_Loop_3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Honlan/Python-Basic/4ac263fa3b3b3ddd08f7cba389b5c8c183672a13/ppt/07_Loop_3.pdf -------------------------------------------------------------------------------- /ppt/08_Midterm.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Honlan/Python-Basic/4ac263fa3b3b3ddd08f7cba389b5c8c183672a13/ppt/08_Midterm.pdf -------------------------------------------------------------------------------- /ppt/09_List.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Honlan/Python-Basic/4ac263fa3b3b3ddd08f7cba389b5c8c183672a13/ppt/09_List.pdf -------------------------------------------------------------------------------- /ppt/10_String.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Honlan/Python-Basic/4ac263fa3b3b3ddd08f7cba389b5c8c183672a13/ppt/10_String.pdf -------------------------------------------------------------------------------- /ppt/11_Dictionary.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Honlan/Python-Basic/4ac263fa3b3b3ddd08f7cba389b5c8c183672a13/ppt/11_Dictionary.pdf -------------------------------------------------------------------------------- /ppt/12_Function_1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Honlan/Python-Basic/4ac263fa3b3b3ddd08f7cba389b5c8c183672a13/ppt/12_Function_1.pdf -------------------------------------------------------------------------------- /ppt/13_Function_2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Honlan/Python-Basic/4ac263fa3b3b3ddd08f7cba389b5c8c183672a13/ppt/13_Function_2.pdf -------------------------------------------------------------------------------- /python-3.6.2-amd64.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Honlan/Python-Basic/4ac263fa3b3b3ddd08f7cba389b5c8c183672a13/python-3.6.2-amd64.exe -------------------------------------------------------------------------------- /python-3.6.2-macosx10.6.pkg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Honlan/Python-Basic/4ac263fa3b3b3ddd08f7cba389b5c8c183672a13/python-3.6.2-macosx10.6.pkg --------------------------------------------------------------------------------