Python Exercises - TA Version

Bài Tập 1: Đếm từ a đến b (Count from a to b)

Đề bài: Nhập hai số a và b. In các số từ a đến b.

Problem: Enter two numbers a and b. Print numbers from a to b.

Ví dụ:

Enter a: 3
Enter b: 7

Output:

3
4
5
6
7

Bài Tập 2: In Số Lẻ từ 1 đến n (Print Odd Numbers from 1 to n)

Đề bài: Nhập một số n. In tất cả các số lẻ từ 1 đến n.

Problem: Enter a number n. Print all odd numbers from 1 to n.

Ví dụ:

Enter n: 10

Output:

1
3
5
7
9

Bài Tập 3: Tính n Giai Thừa Bằng Vòng Lặp (Calculate n Factorial with Loop)

Đề bài: Nhập số n. Tính n! (n giai thừa) = 1 × 2 × 3 × ... × n.

Problem: Enter n. Calculate n! (n factorial) = 1 × 2 × 3 × ... × n.

Ví dụ:

Enter n: 5

Output:

Factorial: 120

Bài Tập 4: Đếm Ngược (Countdown)

Đề bài: Nhập một số n. Đếm ngược từ n về 0 và in mỗi số trên một dòng.

Problem: Enter a number n. Count down from n to 0 and print each number on a line.

Ví dụ:

Enter n: 5

Output:

5
4
3
2
1
0

Bài Tập 5: Tính Lũy Thừa Bằng Vòng Lặp (Calculate Power with Loop)

Đề bài: Nhập hai số a và n. Tính a^n (a mũ n) bằng vòng lặp (không dùng **).

Problem: Enter two numbers a and n. Calculate a^n (a to power n) using loop (no **).

Ví dụ:

Enter a: 2
Enter n: 5

Output:

Result: 32

Bài Tập 6: Đếm Số Âm và Số 0 (Count Negative and Zero)

Đề bài: Nhập n số nguyên. Đếm có bao nhiêu số âm và bao nhiêu số 0.

Problem: Enter n integers. Count how many are negative and how many are zero.

Ví dụ:

Enter count: 6
Numbers: -3, 0, 7, -5, 0, 12

Output:

Negative: 2
Zero: 2

Bài Tập 7: Đếm Số Chia Hết cho 2 HOẶC 3 (Count Numbers Divisible by 2 OR 3)

Đề bài: Nhập n số nguyên. Đếm có bao nhiêu số chia hết cho 2 hoặc 3.

Problem: Enter n integers. Count how many are divisible by 2 or 3.

Ví dụ:

Enter count: 5
Numbers: 10, 13, 9, 7, 12

Output:

Count: 3

Bài Tập 8: Tổng Số Chẵn (Sum of Even Numbers)

Đề bài: Nhập n số nguyên. Tính tổng CHỈ các số chẵn.

Problem: Enter n integers. Calculate sum of ONLY even numbers.

Ví dụ:

Enter count: 5
Numbers: 10, 15, 22, 7, 8

Output:

Sum: 40

Bài Tập 9: Số Lớn Hơn 10 (Numbers Greater Than 10)

Đề bài: Nhập n số nguyên. Chỉ in ra các số lớn hơn 10.

Problem: Enter n integers. Print only numbers greater than 10.

Ví dụ:

Enter count: 5
Numbers: 5, 15, 8, 22, 3

Output:

15
22

Bài Tập 10: In Số Trong Khoảng (Print Numbers in Range)

Đề bài: Nhập n số nguyên. Chỉ in các số trong khoảng 10-50 (bao gồm 10 và 50).

Problem: Enter n integers. Print only numbers in range 10-50 (inclusive).

Ví dụ:

Enter count: 6
Numbers: 5, 25, 60, 10, 50, 3

Output:

25
10
50

Bài Tập 11: In Số Lớn Hơn Trung Bình (Print Numbers Greater Than Average)

Đề bài: Nhập n số nguyên. Tính trung bình, sau đó nhập lại n số và chỉ in các số lớn hơn trung bình.

Problem: Enter n integers. Calculate average, then enter n numbers again and print only those greater than average.

Ví dụ:

Enter count: 5
Numbers: 10, 20, 30, 40, 50
Enter numbers again:
10, 20, 30, 40, 50

Output:

40
50

Bài Tập 12: Tính Tiền Điện (Electricity Bill Calculator)

Đề bài: Nhập n số kWh. Tính tiền: 0-50 kWh: 1000đ/kWh, 51-100: 1500đ/kWh, >100: 2000đ/kWh.

Problem: Enter n kWh values. Calculate bill: 0-50 kWh: 1000/kWh, 51-100: 1500/kWh, >100: 2000/kWh.

Ví dụ:

Enter count: 3
kWh: 30, 75, 120

Output:

30000
67500
170000

Bài Tập 13: Hình Chữ Nhật Rỗng (Hollow Rectangle)

Đề bài: Nhập chiều rộng w và chiều cao h. In hình chữ nhật rỗng bằng dấu sao.

Problem: Enter width w and height h. Print hollow rectangle with stars.

Ví dụ:

Width: 5
Height: 4

Output:

* * * * *
*       *
*       *
* * * * *

Bài Tập 14: Đếm Số Theo Điều Kiện Phức Tạp (Count with Complex Condition)

Đề bài: Nhập n số nguyên. Đếm số lượng số chia hết cho 3 NHƯNG KHÔNG chia hết cho 6.

Problem: Enter n integers. Count numbers divisible by 3 BUT NOT divisible by 6.

Ví dụ:

Enter count: 6
Numbers: 3, 6, 9, 12, 15, 18

Output:

Count: 3

Bài Tập 15: Xác Thực Mật Khẩu Đơn Giản (Simple Password Validation)

Đề bài: Người dùng có 3 lần thử để nhập đúng mật khẩu. Mật khẩu là 'python123'. In 'Access granted' hoặc 'Access denied'.

Problem: User has 3 attempts to enter correct password. Password is 'python123'. Print 'Access granted' or 'Access denied'.

Ví dụ:

Password: abc
Wrong! Try again
Password: xyz
Wrong! Try again
Password: python123
Access granted

Output:

Access granted

Bài Tập 16: Đếm Chữ Số (Count Digits)

Đề bài: Nhập một số nguyên. Đếm số lượng chữ số của số đó (bao gồm cả số âm).

Problem: Enter an integer. Count the number of digits (including negatives).

Ví dụ:

Enter number: -4567

Output:

Digits: 4

Bài Tập 17: Tổng Các Chữ Số (Sum of Digits)

Đề bài: Nhập một số nguyên dương. Tính tổng các chữ số của số đó.

Problem: Enter a positive integer. Calculate sum of its digits.

Ví dụ:

Enter number: 12345

Output:

Sum: 15

Bài Tập 18: Số Chính Phương (Perfect Square)

Đề bài: Nhập một số nguyên dương. Kiểm tra số đó có phải số chính phương không (tồn tại số i mà i×i = số đó).

Problem: Enter a positive integer. Check if it's a perfect square (exists number i where i×i equals that number).

Ví dụ:

Enter number: 144

Output:

Perfect square

Bài Tập 19: Hình Kim Cương Số (Number Diamond)

Đề bài: Nhập n (lẻ). In hình kim cương bằng số: nửa trên tăng 1,3,5..., nửa dưới giảm.

Problem: Enter n (odd). Print diamond with numbers: upper half increases 1,3,5..., lower half decreases.

Ví dụ:

Enter n: 5

Output:

1
123
12345
123
1

Bài Tập 20: Dãy Fibonacci (Fibonacci Sequence)

Đề bài: Nhập số n (n > 2). In n số Fibonacci đầu tiên. Fibonacci: F(0)=0, F(1)=1, F(n)=F(n-1)+F(n-2).

Problem: Enter n (n > 2). Print first n Fibonacci numbers. Fibonacci: F(0)=0, F(1)=1, F(n)=F(n-1)+F(n-2).

Ví dụ:

Enter n: 8

Output:

0 1 1 2 3 5 8 13

ĐÁP ÁN / SOLUTIONS

Solution 1: Đếm từ a đến b

Giải thích: range(a, b+1) tạo dãy từ a đến b.

Explanation: range(a, b+1) creates sequence from a to b.

Code:

python
a = int(input("Enter a: ")) b = int(input("Enter b: ")) for i in range(a, b + 1): print(i)

Solution 2: In Số Lẻ từ 1 đến n

Giải thích: range(1, n+1, 2) tạo dãy số bắt đầu từ 1, tăng 2 mỗi lần (chỉ số lẻ).

Explanation: range(1, n+1, 2) creates sequence starting at 1, incrementing by 2 (odd numbers only).

Code:

python
n = int(input("Enter n: ")) for i in range(1, n + 1, 2): print(i)

Solution 3: Tính n Giai Thừa Bằng Vòng Lặp

Giải thích: Nhân liên tiếp từ 1 đến n.

Explanation: Multiply consecutively from 1 to n.

Code:

python
n = int(input("Enter n: ")) result = 1 for i in range(1, n + 1): result *= i print(f"Factorial: {result}")

Solution 4: Đếm Ngược

Giải thích: Vòng lặp for đếm ngược từ n về 0. Mỗi lần lặp in ra số i.

Explanation: For loop counts down from n to 0. Each iteration prints number i.

Code:

python
n = int(input("Enter n: ")) for i in range(n, -1, -1): print(i)

Solution 5: Tính Lũy Thừa Bằng Vòng Lặp

Giải thích: Nhân a với chính nó n lần.

Explanation: Multiply a by itself n times.

Code:

python
a = int(input("Enter a: ")) n = int(input("Enter n: ")) result = 1 for i in range(n): result *= a print(f"Result: {result}")

Solution 6: Đếm Số Âm và Số 0

Giải thích: Dùng if-elif để phân loại và đếm.

Explanation: Use if-elif to categorize and count.

Code:

python
n = int(input("Enter count: ")) negative = 0 zero = 0 for i in range(n): number = int(input("Enter number: ")) if number < 0: negative += 1 elif number == 0: zero += 1 print(f"Negative: {negative}") print(f"Zero: {zero}")

Solution 7: Đếm Số Chia Hết cho 2 HOẶC 3

Giải thích: Dùng OR để kiểm tra một trong hai điều kiện.

Explanation: Use OR to check one of two conditions.

Code:

python
n = int(input("Enter count: ")) count = 0 for i in range(n): number = int(input("Enter number: ")) if number % 2 == 0 or number % 3 == 0: count += 1 print(f"Count: {count}")

Solution 8: Tổng Số Chẵn

Giải thích: Chỉ cộng số chẵn vào tổng.

Explanation: Only add even numbers to sum.

Code:

python
n = int(input("Enter count: ")) total = 0 for i in range(n): number = int(input("Enter number: ")) if number % 2 == 0: total += number print(f"Sum: {total}")

Solution 9: Số Lớn Hơn 10

Giải thích: Nhập từng số, kiểm tra điều kiện lớn hơn 10, in ngay nếu đúng.

Explanation: Input each number, check if greater than 10, print immediately if true.

Code:

python
n = int(input("Enter count: ")) for i in range(n): number = int(input("Enter number: ")) if number > 10: print(number)

Solution 10: In Số Trong Khoảng

Giải thích: Kiểm tra số nằm trong khoảng bằng toán tử so sánh kết hợp.

Explanation: Check if number is in range using combined comparison operators.

Code:

python
n = int(input("Enter count: ")) for i in range(n): number = int(input("Enter number: ")) if 10 <= number <= 50: print(number)

Solution 11: In Số Lớn Hơn Trung Bình

Giải thích: Vòng lặp đầu tính trung bình, vòng lặp thứ hai so sánh và in.

Explanation: First loop calculates average, second loop compares and prints.

Code:

python
n = int(input("Enter count: ")) total = 0 # First pass: calculate average for i in range(n): number = int(input("Enter number: ")) total += number average = total / n # Second pass: print numbers greater than average print("Enter numbers again:") for i in range(n): number = int(input("Enter number: ")) if number > average: print(number)

Solution 12: Tính Tiền Điện

Giải thích: Tính tiền theo bậc giá lũy tiến.

Explanation: Calculate bill using progressive pricing tiers.

Code:

python
n = int(input("Enter count: ")) for i in range(n): kwh = int(input("kWh: ")) bill = 0 if kwh <= 50: bill = kwh * 1000 elif kwh <= 100: bill = 50 * 1000 + (kwh - 50) * 1500 else: bill = 50 * 1000 + 50 * 1500 + (kwh - 100) * 2000 print(bill)

Solution 13: Hình Chữ Nhật Rỗng

Giải thích: Kiểm tra vị trí để in sao hoặc khoảng trắng.

Explanation: Check position to print star or space.

Code:

python
w = int(input("Width: ")) h = int(input("Height: ")) for i in range(h): for j in range(w): if i == 0 or i == h - 1 or j == 0 or j == w - 1: print("*", end=" ") else: print(" ", end=" ") print()

Solution 14: Đếm Số Theo Điều Kiện Phức Tạp

Giải thích: Kết hợp hai điều kiện bằng AND và NOT.

Explanation: Combine two conditions using AND and NOT.

Code:

python
n = int(input("Enter count: ")) count = 0 for i in range(n): number = int(input("Enter number: ")) if number % 3 == 0 and number % 6 != 0: count += 1 print(f"Count: {count}")

Solution 15: Xác Thực Mật Khẩu Đơn Giản

Giải thích: Vòng lặp 3 lần, break khi đúng, kiểm tra trạng thái cuối.

Explanation: Loop 3 times, break when correct, check final status.

Code:

python
correct_password = "python123" granted = False for i in range(3): password = input("Password: ") if password == correct_password: print("Access granted") granted = True break else: if i < 2: print("Wrong! Try again") if not granted: print("Access denied")

Solution 16: Đếm Chữ Số

Giải thích: Lấy giá trị tuyệt đối, chia 10 cho đến hết và đếm.

Explanation: Take absolute value, divide by 10 until zero and count.

Code:

python
num = int(input("Enter number: ")) num = abs(num) # Take absolute value if num == 0: count = 1 else: count = 0 while num > 0: count += 1 num = num // 10 print(f"Digits: {count}")

Solution 17: Tổng Các Chữ Số

Giải thích: Lấy từng chữ số và cộng vào tổng.

Explanation: Extract each digit and add to sum.

Code:

python
num = int(input("Enter number: ")) digit_sum = 0 while num > 0: digit = num % 10 digit_sum += digit num = num // 10 print(f"Sum: {digit_sum}")

Solution 18: Số Chính Phương

Giải thích: Vòng lặp kiểm tra xem có số nào bình phương bằng số cần kiểm tra không.

Explanation: Loop checks if any number squared equals the number to check.

Code:

python
number = int(input("Enter number: ")) is_perfect = False i = 1 while i * i <= number: if i * i == number: is_perfect = True break i += 1 if is_perfect: print("Perfect square") else: print("Not perfect square")

Solution 19: Hình Kim Cương Số

Giải thích: In nửa trên tăng dần, nửa dưới giảm dần, mỗi dòng in số liên tiếp.

Explanation: Print upper half increasing, lower half decreasing, each row prints consecutive numbers.

Code:

python
n = int(input("Enter n (odd): ")) mid = (n + 1) // 2 # Upper half for i in range(1, mid + 1): for j in range(1, 2 * i): print(j, end="") print() # Lower half for i in range(mid - 1, 0, -1): for j in range(1, 2 * i): print(j, end="") print()

Solution 20: Dãy Fibonacci

Giải thích: Dùng 2 biến để tính số tiếp theo từ 2 số trước.

Explanation: Use 2 variables to calculate next number from previous 2.

Code:

python
n = int(input("Enter n: ")) a, b = 0, 1 print(a, end=" ") print(b, end=" ") for i in range(2, n): c = a + b print(c, end=" ") a, b = b, c print()