I have this code in C
int main(){
int t = 1;
while(t != 0)
t = t + 1;
return 0;
}
What would be the equivalent in Python?
In C, although it appears as such, it's not an infinite loop. Would it be similar in Python?
I have this code in C
int main(){
int t = 1;
while(t != 0)
t = t + 1;
return 0;
}
What would be the equivalent in Python?
In C, although it appears as such, it's not an infinite loop. Would it be similar in Python?
Python integers are not limited in constant number of bytes, only in your RAM limits. This loop would go forever, until your computer runs out of memory.
Use sys.getsizeof(int())
and sys.getsizeof(int(2**128))
to test this behaviour.
Anyway, the equivalent that terminates is
t = 1
while t < 2**32:
t += 1
(Considering 32-bit integers)
like this?:
def main():
t = 1
while t != 0:
t += 1
return 0
main()
You can try this with numpy
:
import numpy as np
t = (np.int16)(1)
while t != 0:
print t
t += (np.int16)(1)
print t