Share bài tập thực hành 2 tin học 11 python

truong147258

New member
## Bài tập thực hành 2: Tin học 11 Python

Bài đăng trên blog này sẽ cung cấp cho bạn các bài tập thực hành cho Tin học 11 Python.Các bài tập được thiết kế để giúp bạn củng cố sự hiểu biết của bạn về các tài liệu được đề cập trong khóa học.

### Bài tập 1: Viết một chương trình để in 100 số Fibonacci đầu tiên.

Trình tự Fibonacci là một loạt các số trong đó mỗi số là tổng của hai số trước.Hai số đầu tiên trong chuỗi là 0 và 1.

`` `Python
def fibonacci (n):
"" "
Hàm này trả về số Fibonacci thứ n.

Args:
N: Chỉ số của số Fibonacci để trả về.

Trả lại:
Số Fibonacci thứ n.
"" "

Nếu n <2:
trả lại n
khác:
Trả về Fibonacci (N - 1) + Fibonacci (N - 2)


# In 100 số Fibonacci đầu tiên.

Đối với tôi trong phạm vi (100):
in (fibonacci (i))
`` `

### Bài tập 2: Viết một chương trình để kiểm tra xem một số là chính.

Một số nguyên tố là một số tự nhiên lớn hơn 1 không phải là sản phẩm của hai số tự nhiên nhỏ hơn.

`` `Python
def is_prime (n):
"" "
Hàm này kiểm tra nếu một số là số nguyên tố.

Args:
N: Số để kiểm tra.

Trả lại:
Đúng nếu số là số nguyên tố, sai nếu không.
"" "

Nếu n <2:
trả lại sai

Đối với i trong phạm vi (2, int (n ** 0,5) + 1):
Nếu n % i == 0:
trả lại sai

trả về đúng


# Kiểm tra xem 101 là Prime.

in (is_prime (101))
`` `

### Bài tập 3: Viết một chương trình để đảo ngược một chuỗi.

Để đảo ngược một chuỗi, bạn chỉ cần lặp lại qua chuỗi ngược và nối từng ký tự vào một chuỗi mới.

`` `Python
Def Reverse_String (S):
"" "
Hàm này đảo ngược một chuỗi.

Args:
S: Chuỗi để đảo ngược.

Trả lại:
Chuỗi đảo ngược.
"" "

new_string = ''
Đối với I trong phạm vi (Len (S) -1, -1, -1):
new_string += s

trả lại new_string


# Đảo ngược chuỗi "Xin chào".

in (Reverse_String ('Hello')))
`` `

### Bài tập 4: Viết một chương trình để sắp xếp một danh sách các số.

Có nhiều cách khác nhau để sắp xếp một danh sách các số.Một cách phổ biến là sử dụng [thuật toán sắp xếp bong bóng] (Bubble sort - Wikipedia).

`` `Python
def bubble_sort (nums):
"" "
Hàm này sắp xếp một danh sách các số bằng thuật toán sắp xếp bong bóng.

Args:
NUMS: Danh sách các số để sắp xếp.

Trả lại:
Danh sách các số được sắp xếp.
"" "

Đối với I trong phạm vi (Len (Nums) - 1):
Đối với J trong phạm vi (LEN (NUMS) - 1 - I):
Nếu nums [j]> nums [j + 1]:
nums [j], nums [j + 1] = nums [j + 1], nums [j]

trả lại num


# Sắp xếp danh sách [5, 3, 1, 2, 4].

In (Bubble_Sort ([5, 3, 1, 2, 4])))
`` `

### Bài tập 5: Viết một chương trình để tạo một số ngẫu nhiên.

Để tạo một số ngẫu nhiên, bạn có thể sử dụng [mô -đun ngẫu nhiên] (3.12.0 Documentation
=======================================
## Practice Exercises 2: Informatics 11 Python

This blog post will provide you with practice exercises for Informatics 11 Python. The exercises are designed to help you reinforce your understanding of the material covered in the course.

### Exercise 1: Write a program to print the first 100 Fibonacci numbers.

The Fibonacci sequence is a series of numbers where each number is the sum of the two previous numbers. The first two numbers in the sequence are 0 and 1.

```python
def fibonacci(n):
"""
This function returns the nth Fibonacci number.

Args:
n: The index of the Fibonacci number to return.

Returns:
The nth Fibonacci number.
"""

if n < 2:
return n
else:
return fibonacci(n - 1) + fibonacci(n - 2)


# Print the first 100 Fibonacci numbers.

for i in range(100):
print(fibonacci(i))
```

### Exercise 2: Write a program to check if a number is prime.

A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers.

```python
def is_prime(n):
"""
This function checks if a number is prime.

Args:
n: The number to check.

Returns:
True if the number is prime, False otherwise.
"""

if n < 2:
return False

for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False

return True


# Check if 101 is prime.

print(is_prime(101))
```

### Exercise 3: Write a program to reverse a string.

To reverse a string, you simply need to iterate over the string backwards and append each character to a new string.

```python
def reverse_string(s):
"""
This function reverses a string.

Args:
s: The string to reverse.

Returns:
The reversed string.
"""

new_string = ''
for i in range(len(s) - 1, -1, -1):
new_string += s

return new_string


# Reverse the string "hello".

print(reverse_string('hello'))
```

### Exercise 4: Write a program to sort a list of numbers.

There are many different ways to sort a list of numbers. One common way is to use the [bubble sort algorithm](https://en.wikipedia.org/wiki/Bubble_sort).

```python
def bubble_sort(nums):
"""
This function sorts a list of numbers using the bubble sort algorithm.

Args:
nums: The list of numbers to sort.

Returns:
The sorted list of numbers.
"""

for i in range(len(nums) - 1):
for j in range(len(nums) - 1 - i):
if nums[j] > nums[j + 1]:
nums[j], nums[j + 1] = nums[j + 1], nums[j]

return nums


# Sort the list [5, 3, 1, 2, 4].

print(bubble_sort([5, 3, 1, 2, 4]))
```

### Exercise 5: Write a program to generate a random number.

To generate a random number, you can use the [random module](https://docs.python.org/3/
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top