hướng dẫn tin học lớp 11 với python bài 4

quangthaimuffy

New member
** #Hướng dẫn thông tin #Lớp11 #Python #Bài học4 #Coding **

## Bài học 4: Báo cáo lưu lượng kiểm soát

Trong bài học này, chúng ta sẽ tìm hiểu về các câu lệnh kiểm soát trong Python.Kiểm soát các câu lệnh cho phép chúng tôi kiểm soát thứ tự trong đó mã được thực thi.

### Câu điều kiện

Câu lệnh dòng điều khiển cơ bản nhất là câu lệnh `if`.Câu lệnh `if` chỉ cho phép chúng tôi thực thi mã nếu một điều kiện nhất định được đáp ứng.Cú pháp của câu lệnh `if` như sau:

`` `Python
Nếu điều kiện:
# mã sẽ được thực thi nếu điều kiện là đúng
`` `

Ví dụ: mã sau in "Hello World" nếu biến `x` bằng 1:

`` `Python
x = 1
Nếu x == 1:
In ("Hello World")
`` `

Chúng ta cũng có thể sử dụng câu lệnh `other` để thực thi mã nếu điều kiện không được đáp ứng.Cú pháp của câu lệnh `other` như sau:

`` `Python
Nếu điều kiện:
# mã sẽ được thực thi nếu điều kiện là đúng
khác:
# mã được thực thi nếu điều kiện không đúng
`` `

Ví dụ: mã sau in "Hello World" nếu biến `x` bằng 1 và in" Goodbye World "nếu biến` x` không bằng 1:

`` `Python
x = 1
Nếu x == 1:
In ("Hello World")
khác:
In ("Goodbye World")
`` `

### Vòng lặp

Các vòng lặp cho phép chúng tôi thực thi mã nhiều lần.Vòng lặp cơ bản nhất là vòng `while.Cú pháp của vòng `while` như sau:

`` `Python
Trong khi điều kiện:
# mã sẽ được thực thi trong khi điều kiện là đúng
`` `

Ví dụ: mã sau in các số từ 1 đến 10:

`` `Python
i = 1
Trong khi tôi <= 10:
in (i)
i += 1
`` `

Chúng ta cũng có thể sử dụng câu lệnh `break` để thoát khỏi vòng lặp sớm.Câu lệnh `break` được sử dụng khi chúng ta không còn muốn thực hiện vòng lặp.Ví dụ: mã sau in các số từ 1 đến 10, nhưng nó dừng in các số sau khi số 5 được in:

`` `Python
i = 1
Trong khi tôi <= 10:
Nếu i == 5:
phá vỡ
in (i)
i += 1
`` `

Một loại vòng lặp khác là vòng `for`.Vòng lặp `for` lặp qua một chuỗi các mục.Cú pháp của vòng `for` như sau:

`` `Python
Đối với mục theo trình tự:
# Mã được thực thi cho từng mục trong chuỗi
`` `

Ví dụ: mã sau in các số từ 1 đến 10:

`` `Python
Đối với tôi trong phạm vi (1, 11):
in (i)
`` `

### Bài tập

1. Viết một câu lệnh `if` in" Hello World "nếu biến` x` bằng 1.
2
3. Viết một vòng lặp `while` in các số từ 1 đến 10.
4. Viết vòng `for` in các số từ 1 đến 10.
5. Viết một chương trình yêu cầu người dùng cho một số, sau đó in các số từ 1 đến số đó.
=======================================
**#InstructionInformatics #grade11 #Python #Lesson4 #Coding**

## Lesson 4: Control Flow Statements

In this lesson, we will learn about control flow statements in Python. Control flow statements allow us to control the order in which code is executed.

### Conditional Statements

The most basic control flow statement is the `if` statement. The `if` statement allows us to execute code only if a certain condition is met. The syntax of an `if` statement is as follows:

```python
if condition:
# code to be executed if condition is true
```

For example, the following code prints "Hello world" if the variable `x` is equal to 1:

```python
x = 1
if x == 1:
print("Hello world")
```

We can also use the `else` statement to execute code if the condition is not met. The syntax of the `else` statement is as follows:

```python
if condition:
# code to be executed if condition is true
else:
# code to be executed if condition is not true
```

For example, the following code prints "Hello world" if the variable `x` is equal to 1, and prints "Goodbye world" if the variable `x` is not equal to 1:

```python
x = 1
if x == 1:
print("Hello world")
else:
print("Goodbye world")
```

### Loops

Loops allow us to execute code multiple times. The most basic loop is the `while` loop. The syntax of a `while` loop is as follows:

```python
while condition:
# code to be executed while condition is true
```

For example, the following code prints the numbers from 1 to 10:

```python
i = 1
while i <= 10:
print(i)
i += 1
```

We can also use the `break` statement to exit a loop early. The `break` statement is used when we no longer want to execute the loop. For example, the following code prints the numbers from 1 to 10, but it stops printing the numbers after the number 5 is printed:

```python
i = 1
while i <= 10:
if i == 5:
break
print(i)
i += 1
```

Another type of loop is the `for` loop. The `for` loop iterates over a sequence of items. The syntax of a `for` loop is as follows:

```python
for item in sequence:
# code to be executed for each item in the sequence
```

For example, the following code prints the numbers from 1 to 10:

```python
for i in range(1, 11):
print(i)
```

### Exercises

1. Write an `if` statement that prints "Hello world" if the variable `x` is equal to 1.
2. Write an `if` statement that prints "Hello world" if the variable `x` is equal to 1, and prints "Goodbye world" if the variable `x` is not equal to 1.
3. Write a `while` loop that prints the numbers from 1 to 10.
4. Write a `for` loop that prints the numbers from 1 to 10.
5. Write a program that asks the user for a number, and then prints the numbers from 1 to that number.
 
Join ToolsKiemTrieuDoGroup
Back
Top
AdBlock Detected

We get it, advertisements are annoying!

Sure, ad-blocking software does a great job at blocking ads, but it also blocks useful features of our website. For the best site experience please disable your AdBlocker.

I've Disabled AdBlock