Bài tập lập trình
Bài tập Dictionary Comprehension - Cơ bản

Bài tập Dictionary Comprehension - Cơ bản

  1. Viết dictionary comprehension tạo dictionary từ 1 đến 5, với key là số và value là bình phương của số đó.
# Kết quả: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
squares = # Code của bạn ở đây
print(squares)
  1. Viết dictionary comprehension tạo dictionary từ list tên, với key là tên và value là độ dài của tên.
names = ["Alice", "Bob", "Charlie", "David"]
# Kết quả: {'Alice': 5, 'Bob': 3, 'Charlie': 7, 'David': 5}
name_lengths = # Code của bạn ở đây
print(name_lengths)
  1. Viết dictionary comprehension chuyển đổi Celsius sang Fahrenheit.
celsius = {"Ha Noi": 30, "Da Nang": 28, "TP HCM": 32}
# Công thức: F = C * 9/5 + 32
fahrenheit = # Code của bạn ở đây
print(fahrenheit)
  1. Viết dictionary comprehension tạo dictionary từ hai lists (names và ages).
names = ["Alice", "Bob", "Charlie"]
ages = [25, 30, 35]
# Kết quả: {'Alice': 25, 'Bob': 30, 'Charlie': 35}
students = # Code của bạn ở đây
print(students)
  1. Viết dictionary comprehension đảo ngược key và value.
original = {"a": 1, "b": 2, "c": 3}
# Kết quả: {1: 'a', 2: 'b', 3: 'c'}
reversed_dict = # Code của bạn ở đây
print(reversed_dict)
  1. Viết dictionary comprehension tạo dictionary với key là số từ 1-10, value là "even" hoặc "odd".
# Kết quả: {1: 'odd', 2: 'even', 3: 'odd', ...}
number_types = # Code của bạn ở đây
print(number_types)
  1. Viết dictionary comprehension chỉ lấy học sinh có điểm >= 70.
scores = {"Alice": 85, "Bob": 60, "Charlie": 92, "David": 55}
# Kết quả: {'Alice': 85, 'Charlie': 92}
passed = # Code của bạn ở đây
print(passed)
  1. Viết dictionary comprehension đếm số lần xuất hiện của mỗi ký tự trong chuỗi.
text = "hello"
# Kết quả: {'h': 1, 'e': 1, 'l': 2, 'o': 1}
char_count = # Code của bạn ở đây
print(char_count)
  1. Viết dictionary comprehension tạo dictionary từ list, với index làm key.
fruits = ["apple", "banana", "cherry"]
# Kết quả: {0: 'apple', 1: 'banana', 2: 'cherry'}
fruit_dict = # Code của bạn ở đây
print(fruit_dict)
  1. Viết dictionary comprehension viết hoa tất cả values.
names = {"first": "alice", "last": "smith"}
# Kết quả: {'first': 'ALICE', 'last': 'SMITH'}
upper_names = # Code của bạn ở đây
print(upper_names)
  1. Viết dictionary comprehension tính lập phương của các số từ 1-5.
# Kết quả: {1: 1, 2: 8, 3: 27, 4: 64, 5: 125}
cubes = # Code của bạn ở đây
print(cubes)
  1. Viết dictionary comprehension chỉ lấy các từ dài hơn 5 ký tự.
words = ["apple", "banana", "kiwi", "cherry", "date"]
# Kết quả: {'banana': 6, 'cherry': 6}
long_words = # Code của bạn ở đây
print(long_words)
  1. Viết dictionary comprehension nhân tất cả giá trị với 2.
prices = {"apple": 10, "banana": 5, "orange": 8}
# Kết quả: {'apple': 20, 'banana': 10, 'orange': 16}
doubled_prices = # Code của bạn ở đây
print(doubled_prices)
  1. Viết dictionary comprehension tạo bảng cửu chương cho số 5.
# Kết quả: {1: 5, 2: 10, 3: 15, 4: 20, 5: 25, 6: 30, 7: 35, 8: 40, 9: 45, 10: 50}
table_5 = # Code của bạn ở đây
print(table_5)
  1. Viết dictionary comprehension lấy chữ cái đầu của mỗi tên.
names = {"Alice": 25, "Bob": 30, "Charlie": 35}
# Kết quả: {'Alice': 'A', 'Bob': 'B', 'Charlie': 'C'}
initials = # Code của bạn ở đây
print(initials)
  1. Viết dictionary comprehension chuyển đổi inches sang cm (1 inch = 2.54 cm).
measurements = {"height": 60, "width": 40, "depth": 20}
# Kết quả: {'height': 152.4, 'width': 101.6, 'depth': 50.8}
cm_measurements = # Code của bạn ở đây
print(cm_measurements)
  1. Viết dictionary comprehension tạo dictionary với key là ký tự, value là mã ASCII.
letters = "ABC"
# Kết quả: {'A': 65, 'B': 66, 'C': 67}
ascii_dict = # Code của bạn ở đây
print(ascii_dict)
  1. Viết dictionary comprehension chỉ lấy số chẵn và bình phương của chúng.
# Từ 1 đến 10, chỉ lấy số chẵn
# Kết quả: {2: 4, 4: 16, 6: 36, 8: 64, 10: 100}
even_squares = # Code của bạn ở đây
print(even_squares)
  1. Viết dictionary comprehension áp dụng giảm giá 20% cho tất cả sản phẩm.
products = {"Laptop": 1000, "Mouse": 20, "Keyboard": 50}
# Kết quả: {'Laptop': 800.0, 'Mouse': 16.0, 'Keyboard': 40.0}
discounted = # Code của bạn ở đây
print(discounted)
  1. Viết dictionary comprehension thêm prefix "Mr." vào tên nam và "Ms." vào tên nữ.
people = {"Alice": "female", "Bob": "male", "Charlie": "male", "Diana": "female"}
# Kết quả: {'Alice': 'Ms. Alice', 'Bob': 'Mr. Bob', 'Charlie': 'Mr. Charlie', 'Diana': 'Ms. Diana'}
titled_names = # Code của bạn ở đây
print(titled_names)

Lập trình Python - Bumbii Academy