bài tập học python python3 ipynb python-crash-course eric-matthes python-crash-course-book python-crash-course-solutions
- Cập nhật ngày 6 tháng 6 năm 2021
- Máy tính xách tay Jupyter
aayussraut / File_Handling_in_Python
Sao 1
- Mã số
- Vấn đề
- Yêu cầu kéo
Bài tập và ví dụ về xử lý tệp từ Python Crash Course Book của Eric Matthes
python3 python-crash-course eric-matthes python-crash-course-book-xử lý tệp-trong-python python-crash-course-solutions
- Cập nhật ngày 3 tháng 6 năm 2021
- Máy tính xách tay Jupyter
mohanadtalat91 / Crash-Course-on-Python
Sao 1
- Mã số
- Vấn đề
- Yêu cầu kéo
Giải pháp của tôi cho "Khóa học cấp tốc trên python" Khóa học @coursera
python google oop Coursera python3 lập trình hướng đối tượng google-python-bài tập python-crash-course-coursera crash-course-python crash-course-on-python python-coursera python-crash-course-solutions python-crash-course-
Cam kết này không thuộc về bất kỳ nhánh nào trên kho lưu trữ này và có thể thuộc về một nhánh bên ngoài kho lưu trữ
Bạn không thể thực hiện hành động đó vào lúc này
Bạn đã đăng nhập bằng tab hoặc cửa sổ khác. Tải lại để làm mới phiên của bạn. Bạn đã đăng xuất trong một tab hoặc cửa sổ khác. Tải lại để làm mới phiên của bạn
Sử dụng từ điển để lưu trữ thông tin về một người mà bạn biết. Lưu tên, họ, tuổi và thành phố nơi họ sinh sống. Bạn nên có các khóa như
favorite_numbers = {
'mandy': 42,
'micah': 23,
'gus': 7,
'hank': 1000_000,
'maggie': 0,
}
num = favorite_numbers['mandy']
print[f"Mandy's favorite number is {num}."]
num = favorite_numbers['micah']
print[f"Micah's favorite number is {num}."]
num = favorite_numbers['gus']
print[f"Gus's favorite number is {num}."]
num = favorite_numbers['hank']
print[f"Hank's favorite number is {num}."]
num = favorite_numbers['maggie']
print[f"Maggie's favorite number is {num}."]
2, favorite_numbers = {
'mandy': 42,
'micah': 23,
'gus': 7,
'hank': 1000_000,
'maggie': 0,
}
num = favorite_numbers['mandy']
print[f"Mandy's favorite number is {num}."]
num = favorite_numbers['micah']
print[f"Micah's favorite number is {num}."]
num = favorite_numbers['gus']
print[f"Gus's favorite number is {num}."]
num = favorite_numbers['hank']
print[f"Hank's favorite number is {num}."]
num = favorite_numbers['maggie']
print[f"Maggie's favorite number is {num}."]
3, favorite_numbers = {
'mandy': 42,
'micah': 23,
'gus': 7,
'hank': 1000_000,
'maggie': 0,
}
num = favorite_numbers['mandy']
print[f"Mandy's favorite number is {num}."]
num = favorite_numbers['micah']
print[f"Micah's favorite number is {num}."]
num = favorite_numbers['gus']
print[f"Gus's favorite number is {num}."]
num = favorite_numbers['hank']
print[f"Hank's favorite number is {num}."]
num = favorite_numbers['maggie']
print[f"Maggie's favorite number is {num}."]
4 và favorite_numbers = {
'mandy': 42,
'micah': 23,
'gus': 7,
'hank': 1000_000,
'maggie': 0,
}
num = favorite_numbers['mandy']
print[f"Mandy's favorite number is {num}."]
num = favorite_numbers['micah']
print[f"Micah's favorite number is {num}."]
num = favorite_numbers['gus']
print[f"Gus's favorite number is {num}."]
num = favorite_numbers['hank']
print[f"Hank's favorite number is {num}."]
num = favorite_numbers['maggie']
print[f"Maggie's favorite number is {num}."]
5. In từng mẩu thông tin được lưu trữ trong từ điển của bạnperson = {
'first_name': 'eric',
'last_name': 'matthes',
'age': 43,
'city': 'sitka',
}
print[person['first_name']]
print[person['last_name']]
print[person['age']]
print[person['city']]
đầu ra
eric
matthes
43
sitka
6-2. số yêu thích
Sử dụng từ điển để lưu trữ các số yêu thích của mọi người. Nghĩ về năm tên và sử dụng chúng làm chìa khóa trong từ điển của bạn. Hãy nghĩ về một số yêu thích cho mỗi người và lưu trữ từng số dưới dạng một giá trị trong từ điển của bạn. In tên của mỗi người và số yêu thích của họ. Để thú vị hơn nữa, hãy thăm dò ý kiến của một vài người bạn và nhận một số dữ liệu thực tế cho chương trình của bạn
favorite_numbers = {
'mandy': 42,
'micah': 23,
'gus': 7,
'hank': 1000_000,
'maggie': 0,
}
num = favorite_numbers['mandy']
print[f"Mandy's favorite number is {num}."]
num = favorite_numbers['micah']
print[f"Micah's favorite number is {num}."]
num = favorite_numbers['gus']
print[f"Gus's favorite number is {num}."]
num = favorite_numbers['hank']
print[f"Hank's favorite number is {num}."]
num = favorite_numbers['maggie']
print[f"Maggie's favorite number is {num}."]
đầu ra
Mandy's favorite number is 42.
Micah's favorite number is 23.
Gus's favorite number is 7.
Hank's favorite number is 1000000.
Maggie's favorite number is 0.
6-3. Bảng chú giải
Một từ điển Python có thể được sử dụng để mô hình hóa một từ điển thực tế. Tuy nhiên, để tránh nhầm lẫn, hãy gọi nó là bảng thuật ngữ
- Nghĩ về năm từ lập trình mà bạn đã học trong các chương trước. Sử dụng những từ này làm khóa trong bảng thuật ngữ của bạn và lưu ý nghĩa của chúng dưới dạng giá trị
- In từng từ và nghĩa của nó dưới dạng đầu ra được định dạng gọn gàng. Bạn có thể in từ theo sau là dấu hai chấm rồi đến nghĩa của từ đó hoặc in từ đó trên một dòng rồi in nghĩa của từ được thụt vào dòng thứ hai. Sử dụng ký tự xuống dòng [
6] để chèn một dòng trống giữa mỗi cặp từ-nghĩa trong đầu ra của bạnfavorite_numbers = { 'mandy': 42, 'micah': 23, 'gus': 7, 'hank': 1000_000, 'maggie': 0, } num = favorite_numbers['mandy'] print[f"Mandy's favorite number is {num}."] num = favorite_numbers['micah'] print[f"Micah's favorite number is {num}."] num = favorite_numbers['gus'] print[f"Gus's favorite number is {num}."] num = favorite_numbers['hank'] print[f"Hank's favorite number is {num}."] num = favorite_numbers['maggie'] print[f"Maggie's favorite number is {num}."]
glossary = {
'string': 'A series of characters.',
'comment': 'A note in a program that the Python interpreter ignores.',
'list': 'A collection of items in a particular order.',
'loop': 'Work through a collection of items, one at a time.',
'dictionary': "A collection of key-value pairs.",
}
word = 'string'
print[f"\n{word.title[]}: {glossary[word]}"]
word = 'comment'
print[f"\n{word.title[]}: {glossary[word]}"]
word = 'list'
print[f"\n{word.title[]}: {glossary[word]}"]
word = 'loop'
print[f"\n{word.title[]}: {glossary[word]}"]
word = 'dictionary'
print[f"\n{word.title[]}: {glossary[word]}"]
đầu ra
String: A series of characters.
Comment: A note in a program that the Python interpreter ignores.
List: A collection of items in a particular order.
Loop: Work through a collection of items, one at a time.
Dictionary: A collection of key-value pairs.
6-4. Thuật ngữ 2
Bây giờ bạn đã biết cách lặp qua một từ điển, hãy làm sạch mã từ Bài tập 6-3 [trang 99] bằng cách thay thế chuỗi câu lệnh
favorite_numbers = {
'mandy': 42,
'micah': 23,
'gus': 7,
'hank': 1000_000,
'maggie': 0,
}
num = favorite_numbers['mandy']
print[f"Mandy's favorite number is {num}."]
num = favorite_numbers['micah']
print[f"Micah's favorite number is {num}."]
num = favorite_numbers['gus']
print[f"Gus's favorite number is {num}."]
num = favorite_numbers['hank']
print[f"Hank's favorite number is {num}."]
num = favorite_numbers['maggie']
print[f"Maggie's favorite number is {num}."]
7 của bạn bằng một vòng lặp chạy qua các khóa và giá trị của từ điển. Khi bạn chắc chắn rằng vòng lặp của mình hoạt động, hãy thêm năm thuật ngữ Python nữa vào bảng thuật ngữ của bạn. Khi bạn chạy lại chương trình của mình, những từ và nghĩa mới này sẽ tự động được đưa vào đầu raglossary = {
'string': 'A series of characters.',
'comment': 'A note in a program that the Python interpreter ignores.',
'list': 'A collection of items in a particular order.',
'loop': 'Work through a collection of items, one at a time.',
'dictionary': "A collection of key-value pairs.",
'key': 'The first item in a key-value pair in a dictionary.',
'value': 'An item associated with a key in a dictionary.',
'conditional test': 'A comparison between two values.',
'float': 'A numerical value with a decimal component.',
'boolean expression': 'An expression that evaluates to True or False.',
}
for word, definition in glossary.items[]:
print[f"\n{word.title[]}: {definition}"]
đầu ra
Dictionary: A collection of key-value pairs.
String: A series of characters.
Boolean Expression: An expression that evaluates to True or False.
Comment: A note in a program that the Python interpreter ignores.
Value: An item associated with a key in a dictionary.
Loop: Work through a collection of items, one at a time.
List: A collection of items in a particular order.
Conditional Test: A comparison between two values.
Key: The first item in a key-value pair in a dictionary.
Float: A numerical value with a decimal component.
6-5. sông
Lập từ điển về ba con sông lớn và quốc gia mà mỗi con sông chảy qua. Một cặp khóa-giá trị có thể là
favorite_numbers = {
'mandy': 42,
'micah': 23,
'gus': 7,
'hank': 1000_000,
'maggie': 0,
}
num = favorite_numbers['mandy']
print[f"Mandy's favorite number is {num}."]
num = favorite_numbers['micah']
print[f"Micah's favorite number is {num}."]
num = favorite_numbers['gus']
print[f"Gus's favorite number is {num}."]
num = favorite_numbers['hank']
print[f"Hank's favorite number is {num}."]
num = favorite_numbers['maggie']
print[f"Maggie's favorite number is {num}."]
8- Sử dụng một vòng lặp để in một câu về mỗi con sông, chẳng hạn như sông Nile chảy qua Ai Cập
- Sử dụng vòng lặp để in tên của từng con sông có trong từ điển
- Sử dụng vòng lặp để in tên của từng quốc gia có trong từ điển
rivers = {
'nile': 'egypt',
'mississippi': 'united states',
'fraser': 'canada',
'kuskokwim': 'alaska',
'yangtze': 'china',
}
for river, country in rivers.items[]:
print[f"The {river.title[]} flows through {country.title[]}."]
print["\nThe following rivers are included in this data set:"]
for river in rivers.keys[]:
print[f"- {river.title[]}"]
print["\nThe following countries are included in this data set:"]
for country in rivers.values[]:
print[f"- {country.title[]}"]
Đầu ra*
The Mississippi flows through United States.
The Yangtze flows through China.
The Fraser flows through Canada.
The Nile flows through Egypt.
The Kuskokwim flows through Alaska.
The following rivers are included in this data set:
- Mississippi
- Yangtze
- Fraser
- Nile
- Kuskokwim
The following countries are included in this data set:
- United States
- China
- Canada
- Egypt
- Alaska
*Đôi khi chúng tôi muốn coi Alaska là một quốc gia riêng biệt của chúng tôi
6-6. bỏ phiếu
Sử dụng mã trong favorite_languages. py [trang 104]
- Lập danh sách những người nên tham gia cuộc thăm dò ngôn ngữ yêu thích. Bao gồm một số tên đã có trong từ điển và một số chưa
- Lặp lại danh sách những người nên tham gia cuộc thăm dò ý kiến. Nếu họ đã thực hiện cuộc thăm dò ý kiến, hãy in một tin nhắn cảm ơn họ đã trả lời. Nếu họ chưa tham gia cuộc thăm dò, hãy in một thông báo mời họ tham gia cuộc thăm dò
eric
matthes
43
sitka
0đầu ra
eric
matthes
43
sitka
16-7. Người
Bắt đầu với chương trình bạn đã viết cho Bài tập 6-1 [trang 102]. Tạo hai từ điển mới đại diện cho những người khác nhau và lưu trữ cả ba từ điển trong một danh sách có tên là
favorite_numbers = {
'mandy': 42,
'micah': 23,
'gus': 7,
'hank': 1000_000,
'maggie': 0,
}
num = favorite_numbers['mandy']
print[f"Mandy's favorite number is {num}."]
num = favorite_numbers['micah']
print[f"Micah's favorite number is {num}."]
num = favorite_numbers['gus']
print[f"Gus's favorite number is {num}."]
num = favorite_numbers['hank']
print[f"Hank's favorite number is {num}."]
num = favorite_numbers['maggie']
print[f"Maggie's favorite number is {num}."]
9. Lặp lại danh sách những người của bạn. Khi bạn duyệt qua danh sách, hãy in mọi thứ bạn biết về từng ngườieric
matthes
43
sitka
2đầu ra
eric
matthes
43
sitka
36-8. Vật nuôi
Tạo một số từ điển, trong đó tên của mỗi từ điển là tên của một con vật cưng. Trong mỗi từ điển, bao gồm các loại động vật và tên của chủ sở hữu. Lưu trữ những từ điển này trong một danh sách có tên là
Mandy's favorite number is 42.
Micah's favorite number is 23.
Gus's favorite number is 7.
Hank's favorite number is 1000000.
Maggie's favorite number is 0.
0. Tiếp theo, lặp lại danh sách của bạn và khi bạn in mọi thứ bạn biết về từng con vật cưngGhi chú. Khi tôi quyết định đăng các giải pháp và viết các chương trình hoàn chỉnh để giải từng bài tập, tôi nhận ra rằng vấn đề này không được diễn đạt tốt như lẽ ra nó phải như vậy. Thực sự không hợp lý khi đặt tên cho từng từ điển cho con vật cưng mà nó mô tả; . Giải pháp này phản ánh cách tiếp cận đó
eric
matthes
43
sitka
4đầu ra
eric
matthes
43
sitka
56-9. Những nơi yêu thích
Tạo một từ điển có tên là
Mandy's favorite number is 42.
Micah's favorite number is 23.
Gus's favorite number is 7.
Hank's favorite number is 1000000.
Maggie's favorite number is 0.
2. Sử dụng tên của ba thành phố làm chìa khóa trong từ điển của bạn. Tạo một từ điển thông tin về từng thành phố và bao gồm quốc gia có thành phố đó, dân số gần đúng của thành phố đó và một thông tin thực tế về thành phố đó. Các chìa khóa cho từ điển của mỗi thành phố phải giống như Mandy's favorite number is 42.
Micah's favorite number is 23.
Gus's favorite number is 7.
Hank's favorite number is 1000000.
Maggie's favorite number is 0.
3, Mandy's favorite number is 42.
Micah's favorite number is 23.
Gus's favorite number is 7.
Hank's favorite number is 1000000.
Maggie's favorite number is 0.
4 và Mandy's favorite number is 42.
Micah's favorite number is 23.
Gus's favorite number is 7.
Hank's favorite number is 1000000.
Maggie's favorite number is 0.
5. In tên của từng thành phố và tất cả thông tin bạn đã lưu trữ về nó