Nếu bạn đang làm điều đó trong C, thì phán đoán của bạn có nhiều mây như trong Python :-]
Đối với một vòng lặp thoát ra trên một điều kiện đơn giản kiểm tra khi bắt đầu mỗi lần lặp, theo ý kiến của tôi, thông thường hơn [và rõ ràng hơn] chỉ để làm điều đó trong chính vòng lặp tự xây dựng. Nói cách khác, một cái gì đó giống như [nếu bạn cần i
sau khi kết thúc vòng lặp]:
int i = 0;
while [! thereIsAReasonToBreak[i]] {
// do something
i++;
}
hoặc [nếu i
có thể được giới thiệu chỉ là vòng lặp]:
for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
Điều đó sẽ chuyển thành tương đương Python:
i = 0
while not there_is_a_reason_to_break[i]:
# do something
i += 1
Chỉ khi bạn cần thoát ở giữa vòng lặp ở đâu đó [hoặc nếu điều kiện của bạn đủ phức tạp, nó sẽ khiến câu lệnh lặp của bạn không dễ đọc hơn nhiều], bạn mới cần phải lo lắng về việc phá vỡ.
Khi lối ra tiềm năng của bạn là một lối ra đơn giản khi bắt đầu vòng lặp [vì nó dường như ở đây], thường thì tốt hơn là mã hóa lối ra vào chính vòng lặp.
Bằng cách sử dụng một vòng lặp ..
Thật mỉa mai vì nó có vẻ vô cùng được định nghĩa là một số không xác định có thể là một giá trị tích cực hoặc âm. Tất cả các hoạt động số học được thực hiện trên một giá trị vô hạn luôn dẫn đến một số vô hạn, nói rằng đó là tổng, phép trừ, nhân hoặc bất kỳ hoạt động nào khác. Trong thế giới khoa học máy tính, Infinity thường được sử dụng để đo lường hiệu suất và tối ưu hóa các thuật toán thực hiện tính toán trên Một ứng dụng quy mô lớn.
In the world of computer science, infinity is generally used to measure performance and optimize algorithms that perform computations on a large scale application.
Đại diện cho Infinity như một số nguyên trong khái niệm Pythonthe đại diện cho Infinity như một số nguyên vi phạm định nghĩa của chính vô cực. Tính đến năm 2020, không có cách nào để thể hiện vô cùng như một số nguyên trong bất kỳ ngôn ngữ lập trình nào cho đến nay. Nhưng trong Python, vì nó là một ngôn ngữ động, các giá trị nổi có thể được sử dụng để thể hiện một số nguyên vô hạn. Người ta có thể sử dụng float [‘inf,] như một số nguyên để thể hiện nó là vô cùng. Dưới đây là danh sách các cách người ta có thể đại diện cho vô cực trong Python. & NBSP;
The concept of representing infinity as an integer violates the definition of infinity itself. As of 2020, there is no such way to represent infinity as an integer in any programming language so far. But in python, as it is a dynamic language, float values can be used to represent an infinite integer. One can use float[‘inf’] as an integer to represent it as infinity. Below is the list of ways one can represent infinity in Python.
1. Sử dụng phao [‘inf,] và float [‘-inf,]:
Vì vô cùng có thể là cả tích cực và tiêu cực, chúng có thể được biểu diễn dưới dạng phao [‘inf,] và float [‘-inf,] tương ứng. Mã dưới đây cho thấy việc thực hiện nội dung được thảo luận ở trên:
Python3
for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
1for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
2 for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
3for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
4for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
5for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
6for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
7for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
4for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
9i = 0
while not there_is_a_reason_to_break[i]:
# do something
i += 1
0i = 0
while not there_is_a_reason_to_break[i]:
# do something
i += 1
1for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
2 for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
3for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
4i = 0
while not there_is_a_reason_to_break[i]:
# do something
i += 1
5for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
6for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
7for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
4i = 0
while not there_is_a_reason_to_break[i]:
# do something
i += 1
9Positive Infinity: inf Negative Infinity: -inf0
Output:
Positive Infinity: inf Negative Infinity: -inf
2. Sử dụng mô -đun toán Python từ:
Mô -đun toán học Python cũng có thể được sử dụng để đại diện cho các số nguyên vô hạn. Mã dưới đây cho thấy cách nó được thực hiện: & nbsp;
Python3
Positive Infinity: inf Negative Infinity: -inf1
Positive Infinity: inf Negative Infinity: -inf2
for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
1for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
2 Positive Infinity: inf Negative Infinity: -inf5
for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
7for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
4for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
9i = 0
while not there_is_a_reason_to_break[i]:
# do something
i += 1
0i = 0
while not there_is_a_reason_to_break[i]:
# do something
i += 1
1for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
2 Positive Infinity: inf Negative Infinity: -inf2
Positive Infinity: inf Negative Infinity: -inf5
for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
7for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
4i = 0
while not there_is_a_reason_to_break[i]:
# do something
i += 1
9Positive Infinity: inf Negative Infinity: -inf0
Output:
Positive Infinity: inf Negative Infinity: -inf
3. Sử dụng mô -đun thập phân Python sườn:
Mô-đun thập phân Python sườn cũng có thể được sử dụng để biểu diễn các giá trị nổi vô hạn. & NBSP; Nó được sử dụng làm số thập phân [‘vô cực] cho dương tính và thập phân [‘-vô cực] cho giá trị vô hạn âm. & NBSP;
It is used as Decimal[‘Infinity’] for positive and Decimal[‘-Infinity’] for negative infinite value.
Mã dưới đây cho thấy việc triển khai của nó: & nbsp; & nbsp;
Python3
Positive Infinity: inf Negative Infinity: -inf8
Positive Infinity: inf Negative Infinity: -inf9
Positive Infinity: inf Negative Infinity: -inf1
Positive Infinity: Infinity Negative Infinity: -Infinity1
for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
1for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
2 Positive Infinity: Infinity Negative Infinity: -Infinity4
Positive Infinity: Infinity Negative Infinity: -Infinity5
for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
6for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
7for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
4for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
9i = 0
while not there_is_a_reason_to_break[i]:
# do something
i += 1
0i = 0
while not there_is_a_reason_to_break[i]:
# do something
i += 1
1for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
2 Positive Infinity: Infinity Negative Infinity: -Infinity4
Positive Infinity: inf Negative Infinity: -inf4
for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
6for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
7for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
4i = 0
while not there_is_a_reason_to_break[i]:
# do something
i += 1
9Positive Infinity: inf Negative Infinity: -inf0
Output:
Positive Infinity: Infinity Negative Infinity: -Infinity
4. Sử dụng thư viện Python sườn Numpy:
Mô -đun Numpy Python cũng có thể được sử dụng để biểu diễn các giá trị vô hạn. Nó được sử dụng làm np.inf cho dương và -np.inf cho giá trị vô hạn âm. Việc sử dụng thư viện Numpy để biểu diễn giá trị vô hạn được hiển thị trong mã bên dưới: & nbsp;np.inf for positive and -np.inf for negative infinite value. The use of Numpy library for representing an infinite value is shown in the code below:
Python3
Positive Infinity: inf Negative Infinity: -inf1
True True False1
for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
1for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
2 True True False4
for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
7for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
4for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
9i = 0
while not there_is_a_reason_to_break[i]:
# do something
i += 1
0i = 0
while not there_is_a_reason_to_break[i]:
# do something
i += 1
1for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
2 Positive Infinity: inf Negative Infinity: -inf2
True True False4
for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
7for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
4i = 0
while not there_is_a_reason_to_break[i]:
# do something
i += 1
9Positive Infinity: inf Negative Infinity: -inf0
Output:
Positive Infinity: inf Negative Infinity: -inf
Kiểm tra xem một số là vô hạn trong kiểm tra Pythonto xem một số đã cho có vô hạn hay không, người ta có thể sử dụng phương thức isinf [] của thư viện toán học trả về giá trị boolean. Mã dưới đây cho thấy việc sử dụng phương thức isinf []: & nbsp;
To check if a given number is infinite or not, one can use isinf[] method of the math library which returns a boolean value. The below code shows the use
of isinf[] method:
Python3
Positive Infinity: inf Negative Infinity: -inf1
True True False1
Positive Infinity: inf Negative Infinity: -inf1
Positive Infinity: inf Negative Infinity: -inf2
for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
1for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
2 Positive Infinity: inf Negative Infinity: -inf5
i = 0
while not there_is_a_reason_to_break[i]:
# do something
i += 1
1for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
2 Positive Infinity: inf Negative Infinity: -inf2
Positive Infinity: inf Negative Infinity: -inf5
Positive Infinity: inf Negative Infinity: -inf
for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
7for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
02for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
7for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
04for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
7for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
06Output:
True True False
3. Sử dụng mô -đun thập phân Python sườn:
The concept of comparing an infinite value to finite values is
as simple as it gets. As positive infinity is always bigger than every natural number and negative infinity is always smaller than negative numbers. For better understanding look into the code below:
Python3
Positive Infinity: inf Negative Infinity: -inf1
True True False1
for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
1for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
2 Positive Infinity: inf Negative Infinity: -inf5
i = 0
while not there_is_a_reason_to_break[i]:
# do something
i += 1
1for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
2 Positive Infinity: inf Negative Infinity: -inf2
Positive Infinity: inf Negative Infinity: -inf5
Positive Infinity: inf Negative Infinity: -inf
3. Sử dụng mô -đun thập phân Python sườn:
for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
23 for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
24for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
25for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
26 for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
27for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
28for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
7for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
4for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
31for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
6for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
25for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
34for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
35for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
28for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
7for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
4for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
39for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
6for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
41for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
42for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
43for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
44for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
45Output:
True True True False False