If you're doing that in C, then your judgement there is as cloudy as it would be in Python :-]
For a loop that exits on a simple condition check at the start of each iteration, it's more usual [and clearer, in my opinion] to just do that in the looping construct itself. In other words, something like [if you need i
after loop end]:
int i = 0;
while [! thereIsAReasonToBreak[i]] {
// do something
i++;
}
or [if i
can be scoped to just the loop]:
for [int i = 0; ! thereIsAReasonToBreak[i]; ++i] {
// do something
}
That would translate to the Python equivalent:
i = 0
while not there_is_a_reason_to_break[i]:
# do something
i += 1
Only if you need to exit in the middle of the loop somewhere [or if your condition is complex enough that it would render your looping statement far less readable] would you need to worry about breaking.
When your potential exit is a simple one at the start of the loop [as it appears to be here], it's usually better to encode the exit into the loop itself.
No, range[] can't. But itertools.count[] can. For your problem, you could do from itertools import count for i in count[]: do_something_with[i] You can also specify from which number you want it to start counting and by how many steps you want it to increment each time. For example, to get all odd numbers, just use count[1, 2]. Full documentation here: //docs.python.org/3/library/itertools.html#itertools.count
In this article, we show how to create an infinite for loop in Python.
We will show how to create an infinite loop for a range of values, a single value, and for a string.
How to Create an Infinite Loop for a Range of Values
Using the the range[] function in Python, we can set up a range that goes from one value to a certain value, such as 1 to 3, and then have this repeat infinitely using the cycle[] function from the itertool module.
This way, we can create an infinite loop between a certain range in Python.
So below we have a program that cycles between the values 1-9 infinitely.
So the first thing we must do is import is import the itertools module.
We then create a range that we want to loop through. In this case, we want to loop repeatedly through the numbers 1-3. So we use the range[] function and pass in 1-4 as the parameter. Remember that the first parameter of a range[] function is inclusive while the second parameter is excluded. Therefore, it doesn't display the number 4 but goes up to the number 3.
We then use a for loop with the cycle[] function to loop repeatedly through this range.
When you run this program, the numbers 1,2,3 show repeatedly without end in an infinite loop.
You would have to interrupt the program by pressing Ctrl+C to break the infinite loop.
How to Create an Infinite Loop for a Single Value
What if you didn't want to repeat a range of values but instead wanted to wanted to repeat a single value infinitely?
For example, let's say you want to repeat the number 100 infinitely.
There are 2 ways of doing this.
One way is to again use the range function.
This is shown in the code below.
Another way and perhaps better way of doing it is to create a list composed of that element as the only constituent of that list.
Below is another way of repeating the number 100 infinitely in Python.
In this manner, we create a list composed of a single integer, 100.
We then cycle through this list of only one element, which repeats 100 infinitely.
How to Create an Infinite Loop for a String
In the same manner as above, we can create an infinite loop for a string.
We do this by creating a list composed of the string we want to repeat.
This is shown in the code below.
Make sure that to repeat a string with the cycle[] function, put the string within a list by itself.
If you set the variable equal to a string not in a list, the cycle[] function separates each character from the string, which is probably not what you want. So to repeat an entire string, it must be within a list with that string being the only element of a list.
And this is how you can create an infinite for loop in Python using the cycle[] function of the itertools module.
Related Resources
Infinity is an undefined number which can be negative or positive. A number is used as infinity; sometimes, the sum of two numeric values may be a numeric but different pattern; it may be a negative or positive value.
It is used to compare the solution in algorithms for the best solution. Generally, a value set at initial may be positive or negative infinity; we have to take care that no input value is bigger or smaller.
Infinity in Python
In Python, there is no way or method to represent infinity as an integer. This matches the fundamental characteristic of many other popular programming languages. But due to python being dynamically typed language, you can use float[inf] as an integer to represent it as infinity.
Therefore in python, we cannot represent infinity, or we can say that there is no way to show the infinity as an integer. But we can use float [inf] as an integer.
In Python, positive infinity and negative infinity can be represented by:
- Positive infinity: inf
- Negative infinity: -inf
Python Program to Define Positive and Negative Infinity Number
Example
# Define Positive infinity number
ptive_inf = float['inf']
print['Positive Infinity: ',ptive_inf]
# Define Negative infinity number
ntive_inf = float['-inf']
print['Negative Infinity: ',ntive_inf]
Output
Positive Infinity: inf
Negative Infinity: -inf
Represent Positive and Negative Infinity Using the Math Module
To determine the infinity number, you can use the python math module.
Note: This will work with only python 3.5 or higher version of python.
Syntax:
- Positive infinity: math.inf
- Negative infinity: -math.inf
Example:
# Import math module
import math
# Positive infinity
ptive_inf = math.inf
print['Positive Infinity: ',ptive_inf]
# Negative infinity
ntive_inf = -math.inf
print['Negative Infinity: ',ntive_inf]
Output
Positive Infinity: inf
Negative Infinity: -inf
Positive infinity number is greatest, and the negative infinity number is the smallest of all numbers.
In comparison, positive infinity is always bigger than every natural number.
[i.e. 0,1,2,.......... +∞. Positive integer and +infinity.]
In comparison, negative infinity is smaller than the negative number.
[i.e., ∞-.......-2,-1,0, 1,.2,3......... – negative infinity and -integer.]
Example:
# Define Positive infinity number
ptive_inf = float['inf']
if 99999999999999999 > ptive_inf:
print['Numer is greater than Positive infinity']
else:
print['Positive infinity is greater']
# Define Negative infinity number
ntive_inf = float['-inf']
if -99999999999999999 > ptive_inf:
print['Numer is smaller than Negative infinity']
else:
print['Negative infinity is smaller']
Output
Positive infinity is greater
Negative infinity is smaller
Python Program to Check If the Number Is Infinite
Example:
import math
# Define positive infinity number
ptive_inf = float['inf']
print['Variable is Infinity: ',math.isinf[ptive_inf]]
# Define negative infinity number
ntive_inf = float['-inf']
print['Variable is Infinity: ',math.isinf[ntive_inf]]
Click and drag to move
Output:
Variable is Infinity: True
Variable is Infinity: True
In the above example, we are using the using isinf method of math library to check if the number is infinity or not.
Represent Positive and Negative Infinity Using the Decimal Module
You can define positive and negative Infinity by using the decimal module.
Syntax:
- Positive Infinity: Decimal['Infinity']
- Negative Infinity: Decimal['-Infinity']
Example:
from decimal import Decimal
import math
# Define positive infinity
ptive_inf = Decimal['Infinity']
print['Variable is Infinity: ',math.isinf[ptive_inf]]
# Define negative infinity
ntive_inf = Decimal['-Infinity']
print['Variable Infinity: ',math.isinf[ntive_inf]]
Click and drag to move
Output:
Variable is Infinity: True
Variable Infinity: True
Represent Positive and Negative Infinity Using Numpy Library
You can define positive and negative Infinity by using the inf module of the NumPy library
Syntax:
- Positive Infinity: np.inf
- Negative Infinity: -np.inf
Example:
import numpy as np
import math
# Define positive Infinity number
ptive_inf = np.inf
# Check is infinity number
print['Positive infinity number: ',ptive_inf]
# Define negative Infinity number
ntive_inf = -np.inf
# Check is infinity number
print['Negative infinity number: ',ntive_inf]
Output:
Positive infinity number: inf
Negative infinity number: -inf
Check If Positive Infinity Number Is Equal to Negative Infinity Number
You can simple check if the positive infinity number is equal to negative infinity number using simple “==”operator, and the output is always false.
Example:
import math
# Define positive infinity number
ptive_inf = float['inf']
# Define negative infinity number
ntive_inf = float['-inf']
print['Positive infinity equal to Negative infinity: ',ptive_inf == ntive_inf]
Output:
Positive infinity equal to Negative infinity: False
Arithmetic operations on infinity number will give an infinite number
If you perform any arithmetic operation with positive or negative infinity numbers, the result will always be an endless number.
Example:
# Define positive infinity number
ptive_inf = float['inf']
# Multiply Positive infinity number by 5
print['Multiplication : ',ptive_inf * 5]
# Addition to Positive infinity Number
print['Addition : ',ptive_inf + 5]
# Subtraction to Positive infinity Number
print['Subtraction : ',ptive_inf - 5]
# Division to Positive infinity Number
print['Division : ',ptive_inf / 5]
# Define Negative infinity number
ntive_inf = float['-inf']
# Multiply Negative infinity number by 5
print['Multiplication : ',ntive_inf * 5]
# Addition to Negative infinity Number
print['Addition : ',ntive_inf + 5]
# Subtraction to Negative infinity Number
print['Subtraction : ',ntive_inf - 5]
# Division to Negative infinity Number
print['Division : ',ntive_inf / 5]
Output:
Multiplication : inf
Addition : inf
Subtraction : inf
Division : inf
Multiplication : -inf
Addition : -inf
Subtraction : -inf
Division : -inf