Important Announcement
PubHTML5 Scheduled Server Maintenance on (GMT) Sunday, June 26th, 2:00 am - 8:00 am.
PubHTML5 site will be inoperative during the times indicated!

Home Explore Python - Tin học 11

Python - Tin học 11

Published by Loan Nguyễn, 2022-08-10 04:13:58

Description: Python - Tin học 11

Search

Read the Text Version

["B\u00c0I T\u1eacP V\u00c0 TH\u1ef0C H\u00c0NH 4 B\u00e0i 1. import random as rd n = int(input(\\\"Nh\u1eadp s\u1ed1 l\u01b0\u1ee3ng ph\u1ea7n t\u1eed c\u1ee7a d\u00e3y s\u1ed1, N = \\\")) a = [] for i in range(n): ai = rd.randint(-300,300) a += [ai] print(\\\"M\u1ea3ng v\u1eeba t\u1ea1o:\\\") print(a) i=n while (i > 1): for j in range(n-1): if a[j] > a[j+1]: tam = a[j] a[j] = a[j+1] a[j+1] = tam i -= 1 print(\\\"D\u00e3 s\u1ed1 \u0111\u00e3 \u0111\u01b0\u1ee3c s\u1eafp x\u1ebfp:\\\") print(a) input() B\u00e0i 2. import random as rd n = int(input(\\\"Nh\u1eadp s\u1ed1 l\u01b0\u1ee3ng ph\u1ea7n t\u1eed c\u1ee7a d\u00e3y s\u1ed1, N = \\\")) a = [] for i in range(n): ai = rd.randint(-300,300) 44","a.append(int(ai)) print(\\\"M\u1ea3ng a:\\\") print(a) b = [a[0]] for i in range(1,n): x = b[i-1]+a[i] b.append(x) print(\\\"M\u1ea3ng b: \\\") print(b) input() 45","\u00a712. KI\u1ec2U X\u00c2U Trong Python, x\u00e2u (string, t\u1eeb kh\u00f3a ki\u1ec3u l\u00e0 str) l\u00e0 m\u1ed9t d\u00e3y c\u00e1c k\u00fd t\u1ef1 Unicode. Unicode bao g\u1ed3m m\u1ecdi k\u00fd t\u1ef1 trong t\u1ea5t c\u1ea3 c\u00e1c ng\u00f4n ng\u1eef v\u00e0 mang l\u1ea1i t\u00ednh \u0111\u1ed3ng nh\u1ea5t trong m\u00e3 h\u00f3a. M\u1ed9t x\u00e2u \u0111\u01b0\u1ee3c \u0111\u1eb7t trong c\u1eb7p nh\u00e1y \u0111\u01a1n ho\u1eb7c nh\u00e1y k\u00e9p ', \\\". X\u00e2u tr\u00ean nhi\u1ec1u d\u00f2ng \u0111\u01b0\u1ee3c \u0111\u1eb7t trong c\u1eb7p ba nh\u00e1y \u0111\u01a1n ho\u1eb7c 3 nh\u00e1y k\u00e9p ''', \\\"\\\"\\\". M\u1ed9t x\u00e2u trong Python \u0111\u01b0\u1ee3c \u0111\u00e1nh ch\u1ec9 s\u1ed5 cho t\u1eebng k\u00fd t\u1ef1 b\u1eaft \u0111\u1ea7u t\u1eeb 0 v\u00e0 \u0111\u1ea3o chi\u1ec1u b\u1eb1ng c\u00e1ch th\u00eam d\u1ea5u \u00e2m - theo chi\u1ec1u ng\u01b0\u1ee3c l\u1ea1i . \u0110\u1ec3 truy c\u1eadp v\u00e0o ph\u1ea7n t\u1eed x\u00e2u ta s\u1eed d\u1ee5ng tham s\u1ed1 ch\u1ec9 s\u1ed1 trong ng\u1eb7c vu\u00f4ng [] P YTHON 012345 -6 -5 -4 -3 -2 -1 Khai b\u00e1o \u0110\u1ec3 t\u1ea1o m\u1ed9t bi\u1ebfn c\u00f3 ki\u1ec3u x\u00e2u ta ch\u1ec9 c\u1ea7n g\u00e1n x\u00e2u cho bi\u1ebfn \u0111\u00f3. V\u00ed d\u1ee5: d = '2021' e = \\\"2021\\\" f = ''' 2021 ''' \u00c9p ki\u1ec3u v\u1ec1 x\u00e2u a = 2021 # bi\u1ebfn a c\u00f3 ki\u1ec3u l\u00e0 int b = str(a) # bi\u1ebfn b, c \u0111\u1ec1u c\u00f3 ki\u1ec3u l\u00e0 str c = str(2021) C\u00e1c thao t\u00e1c x\u1eed l\u00fd x\u00e2u a) Gh\u00e9p x\u00e2u (+) 46","C\u00e1c x\u00e2u c\u00f3 th\u1ec3 gh\u00e9p v\u1edbi nhau b\u1eb1ng to\u00e1n t\u1eed c\u1ed9ng (+) ho\u1eb7c nh\u00e2n l\u00ean b\u1eb1ng to\u00e1n t\u1eed nh\u00e2n (*). V\u00ed d\u1ee5 str1 = 'H\u00e0 N\u1ed9i' str2 ='Vi\u1ec7t Nam' print(str1 + str2) print(str1 * 3) K\u1ebft qu\u1ea3: H\u00e0 N\u1ed9iVi\u1ec7t Nam H\u00e0 N\u1ed9iH\u00e0 N\u1ed9iH\u00e0 N\u1ed9i b) So s\u00e1nh x\u00e2u C\u00e1c n\u1ed9i dung tr\u00ecnh b\u00e0y trong SGK l\u00e0 ph\u00f9 h\u1ee3p v\u1edbi Python. V\u00ed d\u1ee5 a = \\\"C\u00f3 ph\u1ea3i em m\u00f9a thu H\u00e0 N\u1ed9i\\\" b = \\\"C\u00f3 ph\u1ea3i em l\u00e0 m\u00f9a thu H\u00e0 N\u1ed9i\\\" print(a > b) ## True print(a < b) ## False K\u00fd t\u1ef1 kh\u00e1c nhau \u0111\u1ea7u ti\u00ean c\u1ee7a 2 x\u00e2u l\u00e0 ch\u1eef m trong \u201cm\u00f9a\u201d c\u1ee7a x\u00e2u a v\u00e0 ch\u1eef l trong \u201cl\u00e0\u201d c\u1ee7a x\u00e2u b. \u2794 x\u00e2u a > b. c) X\u00f3a k\u00fd t\u1ef1 trong x\u00e2u ? Python kh\u00f4ng cho ph\u00e9p x\u00f3a k\u00fd t\u1ef1 trong x\u00e2u cho d\u00f9 cho ph\u00e9p truy xu\u1ea5t k\u00fd t\u1ef1 theo ch\u1ec9 s\u1ed1. Ta c\u00f3 th\u1ec3 thay th\u1ebf k\u00fd t\u1ef1 ho\u1eb7c x\u00e2u con b\u1eb1ng h\u00e0m replace(). 47","V\u00ed d\u1ee5. a = \\\"C\u00f3 ph\u1ea3i em m\u00f9a thu H\u00e0 N\u1ed9i\\\" b = a.replace('em ','') print(a) print(b) K\u1ebft qu\u1ea3: C\u00f3 ph\u1ea3i em m\u00f9a thu H\u00e0 N\u1ed9i C\u00f3 ph\u1ea3i m\u00f9a thu H\u00e0 N\u1ed9i d) Ch\u00e8n k\u00fd t\u1ef1 v\u00e0o x\u00e2u ? Trong Python kh\u00f4ng th\u1ec3 ch\u00e8n k\u00fd t\u1ef1 v\u00e0o x\u00e2u th\u00f4ng qua ch\u1ec9 s\u1ed1. Ta c\u00f3 th\u1ec3 ch\u00e8n b\u1eb1ng c\u00e1ch thay th\u1ebf v\u1edbi h\u00e0m replace() nh\u01b0 m\u1ee5c tr\u00ean. e) Sao ch\u00e9p Trong Python ta s\u1eed d\u1ee5ng thao t\u00e1c c\u1eaft x\u00e2u \u0111\u1ec3 g\u00e1n cho bi\u1ebfn kh\u00e1c. V\u00ed d\u1ee5: a = \\\"C\u00f3 ph\u1ea3i em m\u00f9a thu H\u00e0 N\u1ed9i\\\" n = a[11:25] print(n) K\u1ebft qu\u1ea3 in ra m\u00e0n h\u00ecnh l\u00e0: m\u00f9a thu H\u00e0 N\u1ed9i f) \u0110\u1ed9 d\u00e0i x\u00e2u (len) \u0110\u1ec3 bi\u1ebft \u0111\u1ed9 d\u00e0i (s\u1ed1 k\u00fd t\u1ef1) c\u1ee7a m\u1ed9t x\u00e2u ta d\u00f9ng h\u00e0m len() V\u00ed d\u1ee5: a = \\\"C\u00f3 ph\u1ea3i em m\u00f9a thu H\u00e0 N\u1ed9i\\\" 48","n = len(a) print(n) K\u1ebft qu\u1ea3: 25 g) T\u00ecm ki\u1ebfm trong x\u00e2u (find) Python cung c\u1ea5p h\u00e0ng find() \u0111\u1ec3 th\u1ef1c hi\u1ec7n t\u00ecm ki\u1ebfm v\u00e0 tr\u1ea3 v\u1ec1 v\u1ecb tr\u00ed (ch\u1ec9 s\u1ed1) t\u00ecm th\u1ea5y. Kh\u00f4ng t\u00ecm th\u1ea5y s\u1ebd tr\u1ea3 v\u1ec1 -1. V\u00ed d\u1ee5: a = \\\"C\u00f3 ph\u1ea3i em m\u00f9a thu H\u00e0 N\u1ed9i\\\" n = 'H\u00e0 N\u1ed9i' k = a.find(n) print(k) K\u1ebft qu\u1ea3: 19 h) In hoa\/ in th\u01b0\u1eddng (upper\/lower) a = \\\"C\u00f3 ph\u1ea3i em m\u00f9a thu H\u00e0 N\u1ed9i\\\" a = a.upper() b = a.lower() print(a) print(b) K\u1ebft qu\u1ea3: C\u00d3 PH\u1ea2I EM M\u00d9A THU H\u00c0 N\u1ed8I c\u00f3 ph\u1ea3i em m\u00f9a thu h\u00e0 n\u1ed9i 49","i) C\u00e1c thao t\u00e1c th\u01b0\u1eddng d\u00f9ng kh\u00e1c 1) \u0110\u01b0a gi\u00e1 tr\u1ecb bi\u1ebfn kh\u00e1c v\u00e0o trong x\u00e2u a=5 b = f\\\"Bi\u1ebfn a c\u00f3 gi\u00e1 tr\u1ecb l\u00e0 {a}\\\" print(b) K\u1ebft qu\u1ea3: Bi\u1ebfn a c\u00f3 gi\u00e1 tr\u1ecb l\u00e0 5 2) T\u00e1ch\/ gh\u00e9p x\u00e2u th\u00e0nh list v\u00e0 ng\u01b0\u1ee3c l\u1ea1i (split\/join) a = \\\"C\u00f3 ph\u1ea3i em m\u00f9a thu H\u00e0 N\u1ed9i\\\" b = a.split(' ') c = ' '.join(b) print(a) print(b) print(c) K\u1ebft qu\u1ea3: C\u00f3 ph\u1ea3i em m\u00f9a thu H\u00e0 N\u1ed9i ['C\u00f3', 'ph\u1ea3i', 'em', 'm\u00f9a', 'thu', 'H\u00e0', 'N\u1ed9i'] C\u00f3 ph\u1ea3i em m\u00f9a thu H\u00e0 N\u1ed9i 3) X\u1eed l\u00fd k\u00fd t\u1ef1 \u0111\u1eb7c bi\u1ec7t trong x\u00e2u C\u00e1c k\u00fd t\u1ef1 l\u00e0 d\u1ea5u nh\u00e1y trong x\u00e2u s\u1ebd khi\u1ebfn ch\u01b0\u01a1ng tr\u00ecnh hi\u1ec3u l\u00e0 m\u1edf ho\u1eb7c \u0111\u00f3ng m\u1ed9t x\u00e2u. V\u00ed d\u1ee5 d\u1ea5u nh\u00e1y, d\u1ea5u ngo\u1eb7c k\u00e9p trong c\u00e2u: He said: \u201cI\u2019m studying Python\u201d. 50","Ta c\u00f3 th\u1ec3 \u0111\u1eb7t c\u00e2u n\u00e0y trong c\u1eb7p ba nh\u00e1y ho\u1eb7c d\u00f9ng \\\\ \u0111\u1ec3 v\u00f4 hi\u1ec7u h\u00f3a d\u1ea5u nh\u00e1y nh\u01b0 sau: a = \\\"He said \\\\\\\"I'm studying Python.\\\\\\\"\\\" b = '''He said \\\"I'm studying Python.\\\" ''' print(a) print(b) 4) Ki\u1ec3m tra s\u1ef1 t\u1ed3n t\u1ea1i c\u1ee7a k\u00fd t\u1ef1\/x\u00e2u con trong x\u00e2u a = \\\"C\u00f3 ph\u1ea3i em m\u00f9a thu H\u00e0 N\u1ed9i\\\" b = \\\"em\\\" print(b in a) K\u1ebft qu\u1ea3: True 5) V\u00f2ng l\u1eb7p tr\u00ean x\u00e2u V\u00ed d\u1ee5, \u0111\u1ebfm s\u1ed1 l\u1ea7n xu\u1ea5t hi\u1ec7n c\u1ee7a ch\u1eef m trong c\u00e2u \u201cC\u00f3 ph\u1ea3i em l\u00e0 m\u00f9a thu h\u00e0 n\u1ed9i\u201d a = \\\"C\u00f3 ph\u1ea3i em l\u00e0 m\u00f9a thu H\u00e0 N\u1ed9i\\\" count = 0 for i in a: if i == \\\"m\\\": count += 1 print(f\\\"Ch\u1eef m xu\u1ea5t hi\u1ec7n {count} l\u1ea7n trong c\u00e2u\\\") K\u1ebft qu\u1ea3: Ch\u1eef m xu\u1ea5t hi\u1ec7n 2 l\u1ea7n trong c\u00e2u 51","M\u1ed9t s\u1ed1 v\u00ed d\u1ee5 V\u00ed d\u1ee5 1 a = input(\\\"Nh\u1eadp t\u00ean 1: \\\") b = input(\\\"Nh\u1eadp t\u00ean 2: \\\") if len(a) > len(b): print(a) else: print(b) V\u00ed d\u1ee5 2 a = input(\\\"Nh\u1eadp x\u00e2u 1: \\\") b = input(\\\"Nh\u1eadp x\u00e2u 2: \\\") if a[0] == b[len(b)-1]: print(\\\"Tr\u00f9ng nhau\\\") else: print(\\\"Kh\u00e1c nhau\\\") V\u00ed d\u1ee5 3 a = input(\\\"Nh\u1eadp x\u00e2u 1: \\\") x = len(a)-1 for i in range(x,-1,-1): print(a[i], end='') Ghi ch\u00fa: N\u1ebfu qu\u00ean c\u00f3 th\u1ec3 xem l\u1ea1i l\u1ec7nh range() b\u00e0i 10 C\u00e1ch 2 52","a = input(\\\"Nh\u1eadp x\u00e2u: \\\") a = a[::-1] ## \u0111\u1ea3o ng\u01b0\u1ee3c x\u00e2u print(a) V\u00ed d\u1ee5 4 a = input(\\\"Nh\u1eadp x\u00e2u: \\\") x='' a = a.replace(' ','') print(a) V\u00ed d\u1ee5 5. a = input(\\\"Nh\u1eadp x\u00e2u: \\\") for i in a: if (i >= '0') and (i <= '9'): print(i, end='') 53","B\u00c0I T\u1eacP V\u00c0 TH\u1ef0C H\u00c0NH 5 B\u00e0i 1 a = input(\\\"Nh\u1eadp x\u00e2u: \\\") b = a[::-1] if a == b: print(\\\"X\u00e2u \u0111\u1ed1i x\u1ee9ng\\\") else: print(\\\"X\u00e2u kh\u00f4ng \u0111\u1ed1i x\u1ee9ng\\\") B\u00e0i 2 a = input(\\\"Nh\u1eadp x\u00e2u: \\\") for i in a: count = 0 for j in a: if i.lower() == j.lower(): count +=1 print(f\\\"Ch\u1eef {i} xu\u1ea5t hi\u1ec7n {count} l\u1ea7n\\\") B\u00e0i 3 a = input(\\\"Nh\u1eadp x\u00e2u: \\\") if \\\"anh\\\" in a: b = a.replace(\\\"anh\\\",\\\"em\\\") print(b) 54","\u00a713. KI\u1ec2U B\u1ea2N GHI Python kh\u00f4ng c\u00f3 ki\u1ec3u b\u1ea3n ghi. \u0110\u1ec3 x\u1eed l\u00fd d\u1eef li\u1ec7u ki\u1ec3u b\u1ea3n ghi ta c\u00f3 th\u1ec3 k\u1ebft h\u1ee3p ki\u1ec3u \u201ct\u1eeb \u0111i\u1ec3n\u201d (dict) v\u00e0 list. Trong b\u00e0i 11 (ki\u1ec3u m\u1ea3ng) ta \u0111\u00e3 t\u00ecm hi\u1ec3u v\u1ec1 ki\u1ec3u list trong Python. \u1ede b\u00e0i n\u00e0y ch\u00fang ta t\u00ecm hi\u1ec3u ki\u1ec3u T\u1eeb \u0111i\u1ec3n (dict). Dictionary l\u00e0 t\u1eadp h\u1ee3p c\u00e1c c\u1eb7p kh\u00f3a gi\u00e1 k\u00e8m tr\u1ecb kh\u00f4ng c\u00f3 th\u1ee9 t\u1ef1. N\u00f3 th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng khi ch\u00fang ta c\u00f3 m\u1ed9t s\u1ed1 l\u01b0\u1ee3ng l\u1edbn d\u1eef li\u1ec7u. C\u00e1c dictionary \u0111\u01b0\u1ee3c t\u1ed1i \u01b0u h\u00f3a \u0111\u1ec3 tr\u00edch xu\u1ea5t d\u1eef li\u1ec7u v\u1edbi \u0111i\u1ec1u ki\u1ec7n b\u1ea1n ph\u1ea3i bi\u1ebft \u0111\u01b0\u1ee3c kh\u00f3a \u0111\u1ec3 l\u1ea5y gi\u00e1 tr\u1ecb. Khai b\u00e1o Kh\u1edfi t\u1ea1o bi\u1ebfn c\u00f3 ki\u1ec3u t\u1eeb \u0111i\u1ec3n r\u1ed7ng (ch\u01b0a g\u00e1n gi\u00e1 tr\u1ecb) d ={} Khai b\u00e1o dict c\u00f3 ch\u1ec9 \u0111\u1ecbnh key diemso = {}.fromkeys(['toan', 'van', 'anh'], 0) L\u00fac n\u00e0y, bi\u1ebfn diemso s\u1ebd c\u00f3 gi\u00e1 tr\u1ecb nh\u01b0 sau: {'toan': 0, 'van': 0, 'anh': 0} G\u00e1n gi\u00e1 tr\u1ecb G\u00e1n gi\u00e1 tr\u1ecb cho m\u1ed9t bi\u1ebfn t\u1eeb \u0111i\u1ec3n (dict) \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n theo c\u00fa ph\u00e1p: <t\u00ean bi\u1ebfn>[<key>] = <Gi\u00e1 tr\u1ecb> V\u00ed d\u1ee5: d = {} d['ten'] = \\\"Python\\\" 55","d['phienban'] = 3.8 d['nam'] = 2020 print(d) Output: {'ten': 'Python', 'phienban': 3.8, 'nam': 2020} \u0110\u1ec3 x\u1eed l\u00fd v\u00ed d\u1ee5 trong SGK trang 76,77. Ta d\u00f9ng k\u1ebft h\u1ee3p list v\u00e0 dict nh\u01b0 sau: n = int(input('Nh\u1eadp s\u1ed1 h\u1ecdc sinh: ')) ma=[] for i in range(n): b = {} b['ten'] = input('T\u00ean: ') b['ngs'] = input('Ng\u00e0y sinh: ') b['dc'] = input('\u0110\u1ecba ch\u1ec9: ') b['toan'] = int(input('\u0110i\u1ec3m To\u00e1n: ')) b['van'] = int(input('\u0110i\u1ec3m V\u0103n: ')) t = b['toan']+b['van'] if t >= 18: b['xl'] = 'A' elif t >= 14: b['xl'] = 'B' elif t >= 10: b['xl'] = 'C' else: b['xl'] = 'D' ma.append(b) 56","for i in range(n): print(ma[i]['ten'], '\\\\t\\\\t- X\u1ebfp lo\u1ea1i: ', ma[i]['xl']) input() C\u00e1c thao t\u00e1c c\u01a1 b\u1ea3n v\u1edbi ki\u1ec3u dict 3.1 Truy xu\u1ea5t ph\u1ea7n t\u1eed c\u1ee7a dict V\u00ed d\u1ee5 bien_dict = {'ten': 'Nguy\u1ec5n L\u00ea', 'tuoi': 2021} print(bien_dict['ten']) K\u1ebft qu\u1ea3: Nguy\u1ec5n L\u00ea 3.2 Th\u00eam, s\u1eeda gi\u00e1 tr\u1ecb ph\u1ea7n t\u1eed V\u00ed d\u1ee5 d = {'ten': 'Nguy\u1ec5n V\u0103n V\u0103n', 'tuoi': 26} print('Ban \u0111\u1ea7u:', d) d['tuoi'] = 27 print('Sau khi s\u1eeda tu\u1ed5i:', d) d['diachi'] = 'B\u00ecnh Ph\u01b0\u1edbc' print('Sau khi th\u00eam \u0111\u1ecba ch\u1ec9: ', d) K\u1ebft qu\u1ea3: Ban \u0111\u1ea7u: {'ten': 'Nguy\u1ec5n V\u0103n V\u0103n', 'tuoi': 26} Sau khi s\u1eeda tu\u1ed5i: {'ten': 'Nguy\u1ec5n V\u0103n V\u0103n', 'tuoi': 27} 57","Sau khi th\u00eam \u0111\u1ecba ch\u1ec9: {'ten': 'Nguy\u1ec5n V\u0103n V\u0103n', 'tuoi': 27, 'diachi': 'B\u00ecnh Ph\u01b0\u1edbc'} 3.3 X\u00f3a ph\u1ea7n t\u1eed Ch\u1ea1y ch\u01b0\u01a1ng tr\u00ecnh d\u01b0\u1edbi \u0111\u00e2y v\u00e0 quan s\u00e1t: d = {1: 1, 2: 4, 3: 9, 4: 16, 5: 25} # x\u00f3a v\u00e0 l\u1ea5y gi\u00e1 tr\u1ecb ph\u1ea7n t\u1eed \u0111\u01b0\u1ee3c x\u00f3a print(d.pop(2)) print(d) # x\u00f3a ph\u1ea7n t\u1eed cu\u1ed1i v\u00e0 tr\u1ea3 v\u1ec1 c\u1eb7p key,value print(d.popitem()) print(d) # x\u00f3a t\u1ea5t c\u1ea3 c\u00e1c ph\u1ea7n t\u1eed, tr\u1ea3 v\u1ec1 dict r\u1ed7ng d.clear() print(d) K\u1ebft qu\u1ea3: 4 {1: 1, 3: 9, 4: 16, 5: 25} (5, 25) {1: 1, 3: 9, 4: 16} {} Ki\u1ec3u d\u1eef li\u1ec7u dict c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng c\u00e1c h\u00e0m len(), sorted() nh\u01b0 list. Ngo\u00e0i ra dict c\u00f2n c\u00f3 nhi\u1ec1u ph\u01b0\u01a1ng th\u1ee9c, ph\u00e9p to\u00e1n kh\u00e1c \u2013 trong ph\u1ea1m vi th\u1eddi l\u01b0\u1ee3ng b\u00e0i n\u00e0y kh\u00f4ng th\u1ec3 tr\u00ecnh b\u00e0y h\u1ebft. Ta c\u00f3 th\u1ec3 t\u1ef1 t\u00ecm hi\u1ec3u th\u00eam. 58","C\u00c2U H\u1eceI V\u00c0 B\u00c0I T\u1eacP C\u00f3 th\u1ec3 thay c\u00e2u h\u1ecfi 1,2 b\u1eb1ng c\u00e2u h\u1ecfi: Trong Python d\u00f9ng ki\u1ec3u d\u1eef li\u1ec7u n\u00e0o \u0111\u1ec3 x\u1eed l\u00fd d\u1eef li\u1ec7u nh\u01b0 ki\u1ec3u m\u1ea3ng? C\u00e2u 3. C\u00e1c ph\u1ea7n t\u1eed c\u1ee7a list\u2026 C\u00e2u 4. Tham chi\u1ebfu \u0111\u1ebfn ph\u1ea7n t\u1eed list, ph\u1ea7n t\u1eed c\u1ee7a list l\u1ed3ng list (d\u1ea1ng thay th\u1ebf m\u1ea3ng 2 chi\u1ec1u). C\u00e2u 5. n = int(input('Nh\u1eadp n: ')) a = [] for i in range(n): nhap = True while nhap: t = int(input(f\\\"Nh\u1eadp gi\u00e1 tr\u1ecb a[{i}]: \\\")) if abs(t) <= 1000: a.append(t) nhap = False else: nhap = True print(a) d = a[1]-a[0] for i in range(n-1): if a[i+1] - a[i] != d: tb = 'D\u00e3y kh\u00f4ng ph\u1ea3i l\u00e0 c\u1ea5p s\u1ed1 c\u1ed9ng' else: tb = 'd\u00e3y l\u00e0 c\u1ea5p s\u1ed1 c\u1ed9ng' print(tb) 59","C\u00e2u 6. n = int(input(\\\"Nh\u1eadp s\u1ed1 ph\u1ea7n t\u1eed: \\\")) ma =[] for i in range(n): nhap = True while nhap: t = int(input(f\\\"Nh\u1eadp ph\u1ea7n t\u1eed ma[{i}]: \\\")) if abs(t) <= 1000: ma.append(t) nhap = False else: nhap = True chan=le=nt=0 snt = False for i in ma: ## ki\u1ec3m tra t\u00ednh ch\u1eb5n l\u1ebb if i % 2 == 0: chan += 1 else: le += 1 ## ki\u1ec3m tra s\u1ed1 nguy\u00ean t\u1ed1 if i > 2: for j in range(2,i): if i % j == 0: snt = False break else: snt = True if snt: nt +=1 60","print(f\\\"D\u00e3y s\u1ed1 c\u00f3 {le} s\u1ed1 l\u1ebb, {chan} s\u1ed1 ch\u1eb5n v\u00e0 {nt} s\u1ed1 nguy\u00ean t\u1ed1. \\\") B\u00e0i 7. n = int(input(\\\"Nh\u1eadp s\u1ed1 ph\u1ea7n t\u1eed: \\\")) ma =[1,1] for i in range(2,n): t = ma[i-1] + ma[i-2] ma.append(t) print(ma) print(ma[n-1]) B\u00e0i 8. import random as rd n = int(input(\\\"Nh\u1eadp s\u1ed1 ph\u1ea7n t\u1eed: \\\")) a=[] for i in range(n): a.append([]) for j in range(n): x = rd.randint(1,100) a[i].append(x) for i in range(n): for j in range(n): print(\\\"\\\\t\\\", a[i][j], end=' ') print() print('-'*20) 61","for i in range(n): for j in range(n): tem = a[i][j] a[i][j] = a[n-i-1][j] a[n-i-1][j] = tem for i in range(n): for j in range(n): print(\\\"\\\\t\\\", a[i][j], end=' ') print() B\u00e0i 9 import random as rd n = int(input('Nh\u1eadp n: ')) a = [] for i in range(n): a.append([]) for j in range(n): t = rd.randint(-100,100) a[i].append(t) for i in range(n): mx = max(a[i]) for j in range(n): if a[i][j] == mx: a[i][j] = a[i][i] a[i][i] = mx 62","for i in range(n): for j in range(n): print('\\\\t',a[i][j], end='') print() B\u00e0i 10 s = input(\\\"Nh\u1eadp x\u00e2u s: \\\") count = 0 for i in s: if (i >= '0') and (i <= '9'): count += 1 print(f\\\"C\u00f3 {count} ch\u1eef s\u1ed1 trong s\u00e2u s\\\") B\u00e0i 11. \u0110\u1eb7t \u0111i\u1ec1u ki\u1ec7n khi print() 63","CH\u01af\u01a0NG V. T\u1ec6P V\u00c0 THAO T\u00c1C V\u1edaI T\u1ec6P \u00a714. KI\u1ec2U D\u1eee LI\u1ec6U T\u1ec6P Vai tr\u00f2 c\u1ee7a ki\u1ec3u t\u1ec7p Ph\u00e2n lo\u1ea1i t\u1ec7p v\u00e0 thao t\u00e1c v\u1edbi t\u1ec7p T\u1ec7p (file) hay c\u00f2n g\u1ecdi l\u00e0 t\u1eadp tin. File l\u00e0 t\u1eadp h\u1ee3p c\u1ee7a c\u00e1c th\u00f4ng tin \u0111\u01b0\u1ee3c \u0111\u1eb7t t\u00ean v\u00e0 l\u01b0u tr\u1eef tr\u00ean b\u1ed9 nh\u1edb m\u00e1y t\u00ednh nh\u01b0 \u0111\u0129a c\u1ee9ng, \u0111\u0129a m\u1ec1m, CD, DVD,... K\u00fd t\u1ef1 l\u01b0u trong t\u1ec7p kh\u00f4ng ch\u1ec9 theo m\u00e3 ASCII m\u00e0 c\u00f2n \u0111\u01b0\u1ee3c ghi th\u1ebd m\u00e3 UNICODE. Khi mu\u1ed1n \u0111\u1ecdc ho\u1eb7c ghi file, ch\u00fang ta c\u1ea7n ph\u1ea3i m\u1edf file tr\u01b0\u1edbc. Khi ho\u00e0n th\u00e0nh, file c\u1ea7n ph\u1ea3i \u0111\u01b0\u1ee3c \u0111\u00f3ng l\u1ea1i \u0111\u1ec3 c\u00e1c t\u00e0i nguy\u00ean \u0111\u01b0\u1ee3c g\u1eafn v\u1edbi file \u0111\u01b0\u1ee3c gi\u1ea3i ph\u00f3ng. Do \u0111\u00f3, trong Python, m\u1ed9t thao t\u00e1c v\u1edbi file di\u1ec5n ra theo th\u1ee9 t\u1ef1 sau. 1. M\u1edf t\u1ec7p tin 2. \u0110\u1ecdc ho\u1eb7c ghi 3. \u0110\u00f3ng t\u1ec7p. 64","\u00a715. THAO T\u00c1C V\u1edaI T\u1ec6P Khai b\u00e1o Vi\u1ec7c khai b\u00e1o \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n trong thao t\u00e1c m\u1edf t\u1ec7p \u0111\u1ec3 \u0111\u1ecdc\/ghi d\u1eef li\u1ec7u. Thao t\u00e1c v\u1edbi t\u1ec7p \u0110\u1ec3 thao t\u00e1c v\u1edbi t\u1ec7p (\u0111\u1ecdc ho\u1eb7c ghi d\u1eef li\u1ec7u) trong Python c\u00f3 nhi\u1ec1u c\u00e1ch kh\u00e1c nhau. Trong \u0111\u00f3 s\u1eed d\u1ee5ng c\u00e2u l\u1ec7n with l\u00e0 \u0111\u01a1n gi\u1ea3n v\u00e0 hi\u1ec7u qu\u1ea3, ng\u1eafn g\u1ecdn v\u00e0 an to\u00e0n. C\u00fa ph\u00e1p nh\u01b0 sau: with open(\\\"tenfile.txt\\\",\\\"mode\\\") as tenbien: # Th\u1ef1c hi\u1ec7n c\u00e1c thao t\u00e1c v\u1edbi file V\u1edbi c\u00e2u l\u1ec7nh tr\u00ean file \u0111\u01b0\u1ee3c m\u1edf \u0111\u1ec3 thao t\u00e1c \u0111\u1ed3ng th\u1eddi s\u1ebd t\u1ef1 \u0111\u1ed9ng \u0111\u00f3ng file sau khi thao t\u00e1c. C\u00e1c ch\u1ebf \u0111\u1ed9 m\u1edf file (mode) th\u01b0\u1eddng d\u00f9ng nh\u01b0 sau: Mode T\u00e1c \u0111\u1ed9ng r M\u1edf ch\u1ec9 \u0111\u1ec3 \u0111\u1ecdc. (m\u1eb7c \u0111\u1ecbnh) M\u1edf \u0111\u1ec3 ghi, n\u1ebfu \u0111\u00e3 c\u00f3 n\u1ed9i dung th\u00ec s\u1ebd ghi \u0111\u00e8. w N\u1ebfu file ch\u01b0a t\u1ed3n t\u1ea1i th\u00ec t\u1ea1o file m\u1edbi. x T\u1ea1o file \u0111\u1ed9c quy\u1ec1n m\u1edbi (exclusive creation) v\u00e0 ghi n\u1ed9i dung, n\u1ebfu file \u0111\u00e3 t\u1ed3n t\u1ea1i th\u00ec ch\u01b0\u01a1ng tr\u00ecnh s\u1ebd b\u00e1o l\u1ed7i. M\u1edf file ch\u1ebf \u0111\u1ed9 ghi ti\u1ebfp. a N\u1ebfu file \u0111\u00e3 t\u1ed3n t\u1ea1i r\u1ed3i th\u00ec n\u00f3 s\u1ebd ghi ti\u1ebfp n\u1ed9i dung v\u00e0o cu\u1ed1i file, n\u1ebfu file kh\u00f4ng t\u1ed3n t\u1ea1i th\u00ec t\u1ea1o m\u1ed9t file m\u1edbi. t M\u1edf file \u1edf ch\u1ebf \u0111\u1ed9 v\u0103n b\u1ea3n (m\u1eb7c \u0111\u1ecbnh) b M\u1edf file \u1edf ch\u1ebf \u0111\u1ed9 nh\u1ecb ph\u00e2n 2.1 \u0110\u1ecdc file V\u00ed d\u1ee5, Ta c\u00f3 file \u2018vao.txt\u2019 \u0111\u01b0\u1ee3c l\u01b0u c\u00f9ng th\u01b0 m\u1ee5c v\u1edbi file Python. 65","M\u1edf file \u0111\u1ec3 \u0111\u1ecdc ta d\u00f9ng ph\u01b0\u01a1ng th\u1ee9c with open(\u2018tenfile\u2019, \u2018mode\u2019) as tenbien. Ta c\u00f3 th\u1ec3 g\u00e1n mode ho\u1eb7c b\u1ecf qua v\u00ec Python m\u1eb7c \u0111\u1ecbnh m\u1edf file l\u00e0 \u0111\u1ec3 \u0111\u1ecdc. \u0110\u1ecdc to\u00e0n b\u1ed9 file with open(\\\"vao.txt\\\") as f: print(f.read()) \u0110\u1ecdc t\u1eebng d\u00f2ng (1 d\u00f2ng) with open(\\\"vao.txt\\\") as f: print(f.readline()) \u0110\u1ecdc t\u1eebng d\u00f2ng (2 d\u00f2ng) with open(\\\"vao.txt\\\") as f: print(f.readline()) print(f.readline()) \u0110\u1ecdc t\u1eebng d\u00f2ng \u0111\u1ebfn h\u1ebft file with open(\\\"vao.txt\\\") as f: for i in f: print(i, end='') L\u01b0u \u00fd: T\u1ea5t c\u1ea3 d\u1eef li\u1ec7u \u0111\u1ecdc t\u1eeb file text \u0111\u1ec1u c\u00f3 ki\u1ec3u string. V\u00ec v\u1eady n\u1ebfu l\u00e0 c\u00e1c ch\u1eef s\u1ed1, khi \u0111\u1ecdc v\u00e0o ph\u1ea3i \u00e9p ki\u1ec3u th\u00e0nh number m\u1edbi c\u00f3 th\u1ec3 th\u1ef1c hi\u1ec7n c\u00e1c ph\u00e9p to\u00e1n s\u1ed1 h\u1ecdc. 66","2.2 M\u1edf file \u0111\u1ec3 ghi M\u1edf file \u0111\u1ec3 ghi ta d\u00f9ng ph\u01b0\u01a1ng th\u1ee9c with open(\u2018tenfile\u2019, \u2018mode\u2019) as tenbien. Mode th\u01b0\u1eddng d\u00f9ng l\u00e0 w (ghi \u0111\u00e8) ho\u1eb7c a (ghi ti\u1ebfp). C\u1ea3 mode a v\u00e0 w \u0111\u1ec1u s\u1ebd t\u1ea1o file m\u1edbi \u0111\u1ec3 ghi n\u1ebfu file m\u1edf \u0111\u1ec3 ghi ch\u01b0a t\u1ed3n t\u1ea1i. V\u00ed d\u1ee5: with open(\\\"ra.txt\\\",'w') as f: f.write(\\\"Ghi dong chu nay vao file ra.txt\\\") L\u01b0u \u00fd: \u0110\u1ec3 ghi d\u1eef li\u1ec7u v\u00e0o file text ta chuy\u1ec3n v\u1ec1 ki\u1ec3u x\u00e2u tr\u01b0\u1edbc khi ghi (n\u1ebfu d\u1eef li\u1ec7u l\u00e0 s\u1ed1). Ch\u01b0\u01a1ng tr\u00ecnh sau s\u1ebd b\u00e1o l\u1ed7i: a = \\\"Ghi dong chu nay vao file ra.txt\\\" b = 994 with open(\\\"ra.txt\\\",'w') as f: f.write(a + '\\\\n') f.write(b) Ta s\u1eeda l\u1ed7i nh\u01b0 sau: a = \\\"Ghi dong chu nay vao file ra.txt\\\" b = 994 with open(\\\"ra.txt\\\",'w') as f: f.write(a + '\\\\n') f.write(str(b)) 67","\u00a716. V\u00cd D\u1ee4 L\u00c0M VI\u1ec6C V\u1edaI T\u1ec6P V\u00ed d\u1ee5 1 from math import sqrt with open(\\\"TRAI.txt\\\") as f: a = f.read() a = a.split(' ') _len = len(a) tr = 0 for i in range(0,_len,2): d = sqrt(int(a[i])**2+int(a[i+1])**2) _d = round(d,2) tr += 1 print(f\\\"Kho\u1ea3ng c\u00e1ch \u0111\u1ebfn c\u00e1c tr\u1ea1i {tr} l\u00e0: {_d}\\\") V\u00ed d\u1ee5 2 with open(\\\"RESIST.DAT\\\") as f1: tem = f1.read() #print(tem) a = tem.split('\\\\n') # c\u1eaft x\u00e2u theo d\u00f2ng \u0111\u1ec3 t\u1ea1o list lena = len(a) for i in range(lena): #c\u1eaft x\u00e2u theo kho\u1ea3ng tr\u1eafng t\u1ea1o list: a[i] = a[i].split(' ') #print(a) 68","lenai = len(a[1]) with open('RESIST.EQU','w') as f2: for i in range(lena): for j in range(lenai): a[i][j] = int(a[i][j]) # chuy\u1ec3n str th\u00e0nh int \u0111\u1ec3 t\u00ednh to\u00e1n r1 = a[i][0] r2 = a[i][1] r3 = a[i][2] r01 = r1*r2*r3\/(r1*r2+r1*r3+r2*r3) r02 = r1*r2\/(r1+r3) +r2 r03 = r1*r3\/(r1+r3) +r2 r04 = r2*r3\/(r2+r3) +r1 r05 = r1+r2+r3 # ghi v\u00e0o file f2.write(str(r01)+' ') f2.write(str(r02)+' ') f2.write(str(r03)+' ') f2.write(str(r04)+' ') f2.write(str(r05)+'\\\\n') print('Ho\u00e0n th\u00e0nh ghi d\u1eef li\u1ec7u v\u00e0o file!') 69","CH\u01af\u01a0NG VI. CH\u01af\u01a0NG TR\u00ccNH CON V\u00c0 L\u1eacP TR\u00ccNH C\u00d3 C\u1ea4U TR\u00daC \u00a717. CH\u01af\u01a0NG TR\u00ccNH CON V\u00c0 PH\u00c2N LO\u1ea0I Kh\u00e1i ni\u1ec7m ch\u01b0\u01a1ng tr\u00ecnh con Ch\u01b0\u01a1ng tr\u00ecnh con trong Python g\u1ed3m c\u00e1c packpage, module v\u00e0 c\u00e1c h\u00e0m \u0111\u01b0\u1ee3c x\u00e2y d\u1ef1ng s\u1eb5n (build-in) ho\u1eb7c do ng\u01b0\u1eddi l\u1eadp tr\u00ecnh t\u1ef1 x\u00e2y d\u1ef1ng. Packpage l\u00e0 t\u1eadp h\u1ee3p nhi\u1ec1u module, m\u1ed9t module l\u00e0 t\u1eadp h\u1ee3p nhi\u1ec1u h\u00e0m. (m\u1ed9t packpage c\u00f3 th\u1ec3 ch\u1ec9 c\u00f3 1 module, m\u1ed9t module c\u00f3 th\u1ec3 ch\u1ec9 c\u00f3 1 h\u00e0m \u2013 nh\u01b0ng kh\u00f4ng ai l\u00e0m th\u1ebf) Module trong Python t\u01b0\u01a1ng t\u1ef1 nh\u01b0 th\u01b0 vi\u1ec7n trong Pascal. Module l\u00e0 m\u1ed9t file ch\u1ee9a c\u00e1c h\u00e0m li\u00ean quan. V\u00ed d\u1ee5, module math l\u00e0 m\u1ed9t file c\u00f3 t\u00ean math.py ch\u1ee9a c\u00e1c h\u00e0m to\u00e1n h\u1ecdc chu\u1ea9n; module random l\u00e0 file random.py ch\u1ee9a c\u00e1c h\u00e0m sinh d\u1eef li\u1ec7u ng\u1eabu nhi\u00ean. Ch\u01b0\u01a1ng tr\u00ecnh v\u00ed d\u1ee5 trang 92 SGK. Thay v\u00ec nh\u1eadp c\u00e1c s\u1ed1 t\u1eeb b\u00e0n ph\u00edm, ta \u0111i\u1ec1u ch\u1ec9nh y\u00eau c\u1ea7u \u0111\u1ec1 b\u00e0i \u0111\u1ec3 \u1ee9ng d\u1ee5ng k\u1ef9 thu\u1eadt thao t\u00e1c v\u1edbi file, string, list nh\u01b0 sau: Cho file input.txt c\u00f3 2 d\u00f2ng, d\u00f2ng 1 g\u1ed3m c\u00e1c bi\u1ebfn: a,b,c,d; d\u00f2ng 2 g\u1ed3m c\u00e1c bi\u1ebfn: m,n,p,q. C\u00e1c bi\u1ebfn c\u00e1ch nhau b\u1edfi m\u1ed9t d\u1ea5u ph\u1ea9y. def luythua(a,b): return a**b with open('input.txt') as f: l1 = f.readline().replace('\\\\n','') l2 = f.readline().replace('\\\\n','') a = l1.split(',') 70","b = l2.split(',') tong = 0 for i in range(len(a)): a[i] = float(a[i]) b[i] = int(b[i]) tong += luythua(a[i],b[i]) print(tong) Ph\u00e2n lo\u1ea1i v\u00e0 c\u1ea5u tr\u00fac c\u1ee7a ch\u01b0\u01a1ng tr\u00ecnh con 2.1 Ph\u00e2n lo\u1ea1i Python kh\u00f4ng ph\u00e2n bi\u1ec7t th\u1ee7 t\u1ee5c v\u1edbi h\u00e0m. Trong Python c\u00f3 th\u1ec3 ph\u00e2n l\u00e0m 2 lo\u1ea1i ch\u01b0\u01a1ng tr\u00ecnh con. (1) l\u00e0 c\u00e1c ch\u01b0\u01a1ng tr\u00ecnh con x\u00e2y d\u1ef1ng s\u1eb5n (build-in) v\u00e0 (2) ch\u01b0\u01a1ng tr\u00ecnh con do ng\u01b0\u1eddi l\u1eadp tr\u00ecnh t\u1ef1 x\u00e2y d\u1ef1ng. V\u1ec1 c\u1ea5u tr\u00fac, h\u00e0m trong Python c\u0169ng chia l\u00e0m 2 lo\u1ea1i l\u00e0 (1) h\u00e0m th\u00f4ng th\u01b0\u1eddng, \u0111\u01b0\u1ee3c \u0111\u1eb7t t\u00ean v\u00e0 (2) \u201ch\u00e0m \u1ea9n danh\u201d, kh\u00f4ng c\u00f3 t\u00ean (h\u00e0m lambda). V\u1ec1 quy m\u00f4 ta c\u00f3 th\u1ec3 ph\u00e2n th\u00e0nh 3 lo\u1ea1i: h\u00e0m, module v\u00e0 packpage. 2.2 C\u1ea5u tr\u00fac C\u1ea5u tr\u00fac h\u00e0m trong Python: def <tenham>(<bi\u1ebfn\/danh s\u00e1ch bi\u1ebfn>): \\\"\\\"\\\"docstring (ghi ch\u00fa v\u1ec1 ch\u1ee9c n\u0103ng c\u1ee7a h\u00e0m\/ h\u00e0m kh\u00f4ng nh\u1ea5t thi\u1ebft ph\u1ea3i c\u00f3 docstring)\\\"\\\"\\\" <l\u1ec7nh\/ kh\u1ed1i l\u1ec7nh th\u1ef1c hi\u1ec7n nhi\u1ec7m v\u1ee5> H\u00e0m \u1ea9n danh <t\u00ean bi\u1ebfn> = lambda < tham bi\u1ebfn>: <bi\u1ec3u th\u1ee9c, gi\u00e1 tr\u1ecb tr\u1ea3 v\u1ec1> 71","V\u00ed d\u1ee5. H\u00e0m t\u00ednh t\u1ed5ng 3 s\u1ed1 def tong1(x,y,z): t=x+y+z return t def tong2(x,y,z): return x + y + z tong3 = lambda x,y,z: x + y + z a=5 b=6 c=7 print(tong1(a,b,c)) print(tong2(a,b,c)) print(tong3(a,b,c)) K\u1ebft qu\u1ea3: c\u1ea3 3 l\u1ec7nh print \u0111\u1ec1u c\u00f3 k\u1ebft qu\u1ea3 l\u00e0 18 2.3 Th\u1ef1c hi\u1ec7n ch\u01b0\u01a1ng tr\u00ecnh con \u0110\u1ed1i v\u1edbi h\u00e0m: <t\u00ean h\u00e0m>(<tham s\u1ed1>) Nh\u1eefng h\u00e0m kh\u00f4ng c\u00f3 tham s\u1ed1 th\u00ec kh\u00f4ng c\u1ea7n truy\u1ec1n tham s\u1ed1. V\u00ed d\u1ee5: sum(a,b) # t\u00ednh t\u1ed5ng a, b abs(a) # l\u1ea5y gi\u00e1 tr\u1ecb tuy\u1ec7t \u0111\u1ed1i c\u1ee7a a \u0110\u1ed1i v\u1edbi module 72","import <t\u00ean module> # ho\u1eb7c from <t\u00ean module> import <t\u00ean h\u00e0m> # ho\u1eb7c from <t\u00ean module> import * # import t\u1ea5t c\u1ea3 c\u00e1c h\u00e0m V\u00ed d\u1ee5, \u0111\u1ec3 s\u1eed d\u1ee5ng h\u00e0m khai c\u0103n b\u1eadc hai c\u1ee7a m\u1ed9t s\u1ed1 ta ph\u1ea3i d\u00f9ng h\u00e0m sqrt() t\u1eeb module math, ta khai b\u00e1o tr\u01b0\u1edbc khi s\u1eed d\u1ee5ng nh\u01b0 sau: import math # ho\u1eb7c from math import sqrt \u0110\u1ed1i v\u1edbi Packpage V\u00ed d\u1ee5 t\u1ea1o v\u00e0 s\u1eed d\u1ee5ng package v\u1edbi c\u00e1c module\/h\u00e0m trong module: 1. T\u1ea1o th\u01b0 m\u1ee5c import os os.mkdir(\\\"dnx\\\") 2. \u0110\u1eb7t c\u00e1c module kh\u00e1c nhau trong package: module1.py def inra(): print(\\\"\u0110\u01b0\u1ee3c in t\u1eeb h\u00e0m inra trong module 1\\\") module9.py def msg(): print(\\\"D\u00f2ng n\u00e0y in t\u1eeb h\u00e0m msg c\u1ee7a module 9\\\") def msg2(): 73","print(\\\"D\u00f2ng n\u00e0y in t\u1eeb h\u00e0m msg2 c\u1ee7a module 9\\\") 3. T\u1ea1o m\u1ed9t __init__.py file. # file __init__ tr\u1ed1ng import dnx.module1 import dnx.module9 4. Import package n\u00e0y v\u00e0 g\u1ecdi h\u00e0m t\u1eeb c\u00e1c module. from dnx import * module1.inra() module9.msg2() module9.msg() K\u1ebft qu\u1ea3 l\u00e0: \u0110\u01b0\u1ee3c in t\u1eeb h\u00e0m inra trong module 1 D\u00f2ng n\u00e0y in t\u1eeb h\u00e0m msg2 c\u1ee7a module 9 D\u00f2ng n\u00e0y in t\u1eeb h\u00e0m msg c\u1ee7a module 9 L\u01b0u \u00fd th\u00eam: Ta c\u0169ng c\u00f3 th\u1ec3 vi\u1ebft h\u00e0m tr\u1ef1c ti\u1ebfp trong file __init__.py. 74","\u00a718. V\u00cd D\u1ee4 C\u00c1CH VI\u1ebeT V\u00c0 S\u1eec D\u1ee4NG CH\u01af\u01a0NG TR\u00ccNH CON Ch\u01b0\u01a1ng tr\u00ecnh v\u1ebd h\u00ecnh ch\u1eef nh\u1eadt (SGK,Tr.96) def vehcn(): print('*'*50) for i in range(10): print('*'+' '*48+'*') print('*'*50) vehcn() # g\u1ecdi h\u00e0m Ch\u01b0\u01a1ng tr\u00ecnh v\u1ebd h\u00ecnh ch\u1eef nh\u1eadt c\u00f3 tham s\u1ed1 (SGK.Tr.98) def vehcn(a,b): print('*'*a) for i in range(b): print('*' + ' '*(a-2) + '*') print('*'*a) a = int(input('Nh\u1eadp chi\u1ec1u d\u00e0i: ')) b = int(input('Nh\u1eadp chi\u1ec1u r\u1ed9ng: ')) vehcn(a,b) Ch\u01b0\u01a1ng tr\u00ecnh ho\u00e1n \u0111\u1ed5i 2 bi\u1ebfn (SGK. Tr.100) a = int(input('Nh\u1eadp a: ')) b = int(input('Nh\u1eadp b: ')) print('Tr\u01b0\u1edbc khi \u0111\u1ed5i:', a, b) 75","a,b = b,a print('Sau khi \u0111\u1ed5i :', a, b) V\u00ed d\u1ee5 1 (SGK, Tr.101) a = int(input('Nh\u1eadp t\u1eed : ')) b = int(input('Nh\u1eadp m\u1eabu: ')) def ucln(a,b): while b != 0: t=a%b a=b b=t return a x = ucln(a,b) if x >1: a = a \/\/ x b = b \/\/ x print(f'Ph\u00e2n s\u1ed1 \u0111\u00e3 r\u00fat g\u1ecdn l\u00e0 {a}\/{b}') V\u00ed d\u1ee5 2 (SGK, Tr.102) # Python cung c\u1ea5p s\u1eb5n h\u00e0m min(). a = int(input('Nh\u1eadp a: ')) b = int(input('Nh\u1eadp b: ')) c = int(input('Nh\u1eadp c: ')) m = min(a,b,c) print(f'Gi\u00e1 tr\u1ecb nh\u1ecf nh\u1ea5t c\u1ee7a {a}, {b}, {c} l\u00e0 {m}') 76","B\u00c0I T\u1eacP V\u00c0 TH\u1ef0C H\u00c0NH 6 M\u1ee5c \u0111\u00edch, y\u00eau c\u1ea7u B\u1ecf y\u00eau c\u1ea7u ch\u1eef ch\u1ea1y tr\u00ean m\u00e0n h\u00ecnh. N\u1ed9i dung Python cung c\u1ea5p s\u1eb5n ph\u01b0\u01a1n th\u1ee9c \u0111\u1ea3o ng\u01b0\u1ee3c x\u00e2u, list. V\u00ed d\u1ee5: a = \\\"H\u1eccC L\u1eacP TR\u00ccNH PYTHON\\\" b = a[::-1] print(a) print(b) K\u1ebft qu\u1ea3: H\u1eccC L\u1eacP TR\u00ccNH PYTHON NOHTYP HN\u00ccRT P\u1eacL C\u1eccH Vi\u1ebft v\u00e0 ch\u1ea1y th\u1eed c\u00e1c v\u00ed d\u1ee5 thao t\u00e1c x\u1eed l\u00fd x\u00e2u trong ph\u1ea7n \u00a712.2 77","B\u00c0I T\u1eacP V\u00c0 TH\u1ef0C H\u00c0NH 7 M\u1ee5c \u0111\u00edch, y\u00eau c\u1ea7u N\u1ed9i dung Ch\u01b0\u01a1ng tr\u00ecnh SGK, Tr.106 from math import sqrt def nhapdiem(): ma = [] for i in range(3): ma.append([]) x = int(input('Nh\u1eadp x: ')) y = int(input('Nh\u1eadp y: ')) ma[i].append(x) ma[i].append(y) return ma def kh_cach(a,b): return sqrt((a[0]-b[0])**2 + (a[1]-b[1])**2) def daicanh(x): a = kh_cach(x[0],x[1]) b = kh_cach(x[1],x[2]) c = kh_cach(x[0],x[2]) return a, b, c def chuvi(x): cv = 0 for i in daicanh(x): cv += i 78","return cv def dientich(x): p = chuvi(x)\/2 dc = daicanh(x) s = sqrt(p*(p-dc[0])*(p-dc[1])*(p-dc[2])) return s def tinhchat(x): tc = '' d = daicanh(x) if (d[0] == d[1]) and (d[1] == d[2]): tc = 'Tam gi\u00e1c \u0111\u1ec1u' elif (d[0] == d[1]) or (d[1] == d[2]): tc = 'c\u00e2n' elif (d[0]**2+ d[1]**2 == d[2]**2) or (d[0]**2+ d[2]**2 == d[1]**2) or (d[1]**2+ d[2]**2 == d[0]**2): tc = 'Tam gi\u00e1c vu\u00f4ng' else: tc = 'Tam gi\u00e1c th\u01b0\u1eddng' return tc tg = nhapdiem() print('T\u1ecda \u0111\u1ed9 2 \u0111\u1ec9nh c\u1ee7a tam gi\u00e1c l\u00e0: ', tg) print('T\u00ednh ch\u1ea5t:', tinhchat(tg)) print('Chu vi tam gi\u00e1c = ', round(chuvi(tg),3)) print('Di\u1ec7n t\u00edch c\u1ee7a tam gi\u00e1c =', round(dientich(tg),3)) input() C\u00e2u c, SGK, Tr.108 79","from math import sqrt with open('TAMGIAC.DAT') as f: n = int(f.readline()) a = f.read() a = a.split('\\\\n') for i in range(n): a[i] = a[i].split(' ') for j in range(6): a[i][j] = int(a[i][j]) def kh_cach(xa,ya,xb,yb): return sqrt((xa-xb)**2 + (ya-yb)**2) def daicanh(x): a = kh_cach(x[0],x[2],x[1],x[3]) b = kh_cach(x[2],x[4],x[3],x[5]) c = kh_cach(x[0],x[2],x[3],x[5]) return a, b, c def chuvi(x): cv = 0 for i in daicanh(x): cv += i return cv def dientich(x): p = chuvi(x)\/2 dc = daicanh(x) s = sqrt(p*(p-dc[0])*(p-dc[1])*(p-dc[2])) return s def tinhchat(x): tc = '' 80","d = daicanh(x) if (d[0] == d[1]) and (d[1] == d[2]): tc = '\u0111\u1ec1u' elif (d[0] == d[1]) or (d[1] == d[2]): tc = 'c\u00e2n' elif (d[0]**2+ d[1]**2 == d[2]**2) or (d[0]**2+ d[2]**2 == d[1]**2) or (d[1]**2+ d[2]**2 == d[0]**2): tc = 'vu\u00f4ng' else: tc = 'th\u01b0\u1eddng' return tc deu = can = vuong = thuong = 0 for i in range(n): tg = a[i] t = tinhchat(tg) if t == '\u0111\u1ec1u': deu += 1 elif t == 'c\u00e2n': can += 1 elif t == 'vu\u00f4ng': vuong += 1 else: thuong += 1 with open('TAMGIAC.OUT','w') as fo: fo.writelines(str(deu)) fo.write('\\\\n') fo.write(str(can)) fo.write('\\\\n') fo.write(str(vuong)) fo.write('\\\\n') fo.write(str(thuong)) 81","\u00a719. TH\u01af VI\u1ec6N CH\u01af\u01a0NG TR\u00ccNH CON CHU\u1ea8N Module Ngo\u00e0i vi\u1ec7c x\u00e2y d\u1ef1ng ch\u01b0\u01a1ng tr\u00ecnh con (h\u00e0m) tr\u1ef1c ti\u1ebfp trong ch\u01b0\u01a1ng tr\u00ecnh. Python cung c\u1ea5p h\u1ec7 th\u1ed1ng ch\u01b0\u01a1ng tr\u00ecnh con d\u01b0\u1edbi d\u1ea1ng module. M\u1ed7i Module l\u00e0 m\u1ed9t file, trong \u0111\u00f3 c\u00e1c l\u1edbp, h\u00e0m v\u00e0 bi\u1ebfn \u0111\u01b0\u1ee3c \u0111\u1ecbnh ngh\u0129a. T\u1ea5t nhi\u00ean, m\u1ed9t Module c\u0169ng c\u00f3 th\u1ec3 bao g\u1ed3m code c\u00f3 th\u1ec3 ch\u1ea1y. Ch\u01b0\u01a1ng tr\u00ecnh con chu\u1ea9n c\u00f3 th\u1ec3 hi\u1ec3u l\u00e0 c\u00e1c Module \u0111\u00e3 \u0111\u01b0\u1ee3c x\u00e2y d\u1ef1ng s\u1eb5n trong Python. \u0110\u00f3 l\u00e0 math, random, threading, collections, os, mailbox, string, time, \u2026 M\u1ed7i Module n\u00e0y \u0111\u00e3 \u0111\u1ecbnh ngh\u0129a s\u1eb5n r\u1ea5t nhi\u1ec1u h\u00e0m \u0111\u1ec3 b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng \u0111\u1ec3 th\u1ef1c hi\u1ec7n c\u00e1c t\u00ednh n\u0103ng kh\u00e1c nhau. V\u00ed d\u1ee5 s\u1eed d\u1ee5ng math Module: import math a=4.6 print math.ceil(a) print math.floor(a) b=9 print math.sqrt(b) print math.exp(3.0) print math.log(2.0) print math.pow(2.0,3.0) print math.sin(0) print math.cos(0) print math.tan(45) V\u00ed d\u1ee5 s\u1eed d\u1ee5ng random Module: import random print random.random() print random.randint(2,8) 82","Package M\u1ed9t Package l\u00e0 m\u1ed9t t\u1eadp h\u1ee3p c\u00e1c Module, sub-package, \u2026 t\u01b0\u01a1ng t\u1ef1 nhau. \u0110\u00f3 l\u00e0 m\u1ed9t c\u1ea5u tr\u00fac c\u00f3 th\u1ee9 b\u1eadc c\u1ee7a th\u01b0 m\u1ee5c v\u00e0 file. Th\u01b0 vi\u1ec7n \u0111\u1ed3 h\u1ecda Turtle Turtle Graphics l\u00e0 m\u1ed9t ch\u01b0\u01a1ng tr\u00ecnh con c\u00f3 s\u1eb5n trong Python h\u1ed7 tr\u1ee3 hi\u1ec3n th\u1ecb giao di\u1ec7n \u0111\u1ed3 h\u1ecda. C\u00f9ng kh\u1ea3o s\u00e1t m\u1ed9t s\u1ed1 v\u00ed d\u1ee5 sau \u0111\u00e2y. #v\u1ebd h\u00ecnh vu\u00f4ng import turtle t = turtle.Pen() for x in range(100) : t.forward(x) t.left(90) # v\u1ebd h\u00ecnh xo\u1eafn \u1ed1c cu\u1ed1n import turtle t = turtle.Pen() for x in range(100) : t.forward(x) t.left(91) # h\u00ecnh xo\u1eafn \u1ed1c cu\u1ed1n tr\u00f2n import turtle t = turtle.Pen() for x in range(100) : 83","t.circle(x) t.left(91) #v\u1ebd xo\u1eafn \u1ed1c tr\u00f2n 4 m\u00e0u n\u1ec1n \u0111en import turtle t = turtle.Pen() turtle.bgcolor(\\\"black\\\") colors = [\\\"red\\\", \\\"yellow\\\", \\\"blue\\\", \\\"green\\\"] for x in range(100): t.pencolor(colors[x%4]) t.circle(x) t.left(91) import turtle t = turtle.Pen() t.speed(0) turtle.bgcolor(\\\"black\\\") colors = [\\\"red\\\", \\\"yellow\\\", \\\"blue\\\", \\\"green\\\"] for x in range(100): t.pencolor(colors[x%4]) t.circle(x) t.left(45) 84","B\u00c0I T\u1eacP V\u00c0 TH\u1ef0C H\u00c0NH 7 M\u1ee5c \u0111\u00edch, y\u00eau c\u1ea7u Nh\u01b0 SGK N\u1ed9i dung 2.1 C\u00e2u a # \u0111\u01b0\u1eddng g\u1ea5p kh\u00fac ng\u1eabu nhi\u00ean import random as rd import turtle as t t.bgcolor('black') color = ['red','white','yellow','blue'] for i in range(100): t.speed(0) t.fd(50) n = rd.randint(i,360) if n%2 == 0: t.left(n) else: t.right(n) t.pencolor(color[i%4]) 2.2 C\u00e2u b import turtle as t t.bgcolor('black') def vuong(d): t.pencolor('white') t.pendown() 85","for i in range(4): t.fd(d) t.left(90) t.penup() def chunhat(d,r): t.pendown() t.pencolor('yellow') for i in range(2): t.fd(d) t.left(90) t.fd(r) t.left(90) t.penup() def tron(r): t.pendown() t.pencolor('green') t.circle(r) t.penup() def elip(r): t.pendown() t.pencolor('red') for i in range(2): t.circle(r,90) t.circle(r\/\/2,90) t.seth(-45) t.penup() t.penup() 86","t.goto(250,200) tron(100) t.goto(-250,200) vuong(200) t.goto(150,-150) chunhat(200,150) t.goto(-200,-150) elip(120) 87","C\u00c2U H\u1eceI V\u00c0 B\u00c0I T\u1eacP C\u00e2u 1. Python kh\u00f4ng ph\u00e2n bi\u1ec7t C\u00e2u 2. H\u00e0m in ra m\u00e0n h\u00ecnh m\u1ed9t x\u00e2u C\u00e2u 3. H\u00e0m tr\u1ea3 v\u1ec1 nhi\u1ec1u gi\u00e1 tr\u1ecb C\u00e2u 4. from math import sqrt def ucln(a,b): while b > 0: t=a%b a=b b=t return a def bcnn(a,b): t = a*b \/\/ ucln(a,b) return t a = int(input('Nh\u1eadp a: ')) b = int(input('Nh\u1eadp b: ')) print(f'\u01af\u1edbc chung l\u1edbn nh\u1ea5t c\u1ee7a {a} v\u00e0 {b} l\u00e0 {ucln(a,b)}') print(f'B\u1ed9i chung nh\u1ecf nh\u1ea5t c\u1ee7a {a} v\u00e0 {b} l\u00e0 {bcnn(a,b)}') 88","T\u00ecm hi\u1ec3u th\u00eam v\u1ec1 Python N\u1ed9i dung tr\u00ecnh b\u00e0y trong t\u00e0i li\u1ec7u n\u00e0y ch\u1ec9 l\u00e0 ph\u1ea7n b\u00e1m s\u00e1t S\u00e1ch gi\u00e1o khoa Tin h\u1ecdc 11 nh\u1eb1m ph\u1ee5c v\u1ee5 th\u1ea7y c\u00f4 gi\u00e1o v\u00e0 c\u00e1c b\u1ea1n h\u1ecdc sinh l\u1edbp 11 chuy\u1ec3n \u0111\u1ed5i ng\u00f4n ng\u1eef l\u1eadp tr\u00ecnh, ti\u1ebfp c\u1eadn ch\u01b0\u01a1ng tr\u00ecnh Gi\u00e1o d\u1ee5c ph\u1ed5 th\u00f4ng 2018. V\u00ec v\u1eady, \u0111\u1ec3 c\u00f3 hi\u1ec3u th\u1ea5u \u0111\u00e1o v\u00e0 s\u1eed d\u1ee5ng Python trong l\u1eadp tr\u00ecnh th\u00ec ch\u00fang ta c\u1ea7n ti\u1ebfp t\u1ee5c t\u00ecm hi\u1ec3u c\u00e1c t\u00e0i li\u1ec7u kh\u00e1c. T\u00e0i li\u1ec7u ti\u1ebfng Anh https:\/\/www.python.org\/ https:\/\/www.w3schools.com\/python\/default.asp https:\/\/www.programiz.com\/python-programming C\u00e1c t\u00e0i li\u1ec7u c\u00f9ng t\u00e1c gi\u1ea3 c\u00f3 tr\u00ean website: www.dainganxanh.com Li\u00ean h\u1ec7 t\u00e1c gi\u1ea3: dainganxanh Email: [email protected] 89"]


Like this book? You can publish your book online for free in a few minutes!
Create your own flipbook