Open In App

How to use while True in Python

Last Updated : 22 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to use while True in Python.

While loop is used to execute a block of code repeatedly until given boolean condition evaluated to False. If we write while True then the loop will run forever.

Example: While Loop with True

Python3




# Python program to demonstrate
# while loop with True
  
while True:
    pass


If we run the above code then this loop will run infinite number of times. To come out of this loop we will use the break statement explicitly.

Let’s consider the below example, where we want to find the sum of the first N numbers. Let’s see the below code for better understanding.

Example: While Loop with True to find the sum of first N numbers

Python3




# Python program to demonstrate
# while loop with True
  
N = 10
Sum = 0
  
# This loop will run forever
while True:
    Sum += N
    N -= 1
      
    # the below condition will tell
    # the loop to stop
    if N == 0:
        break
          
print(f"Sum of First 10 Numbers is {Sum}")


Output

Sum of First 10 Numbers is 55

In the above example, we have used the while True statement to run the while loop and we have added an if statement that will stop the execution of the loop when the value of N becomes 0 If we do not write this if statement then the loop will run forever and will start adding the negative values of N to the sum.


Similar Reads

What is the difference between null=True and blank=True in Django?
When working with Django models, you may come across two common field options: null=True and blank=True. While they may seem similar at first glance, they serve distinct purposes and have a significant impact on how data is stored and validated in your application. Difference between null=True and blank=True in DjangoUnderstanding the difference be
4 min read
When not to use Recursion while Programming in Python?
To recurse or not to recurse, that is the question. We all know about the fun we have with recursive functions. But it has its own set of demerits too, one of which is going to be explained in this article to help you choose wisely. Say, we need to create a function which when called needs to print the data in a linked list. This can be done in 2 w
3 min read
Python | Get indices of True values in a binary list
Boolean lists are often used by developers to check for True values during hashing. The boolean list is also used in certain dynamic programming paradigms in dynamic programming. Let's discuss certain ways to get indices of true values in a list in Python. Method #1 : Using enumerate() and list comprehension enumerate() can do the task of hashing i
9 min read
Python | First occurrence of True number
Many times we require to find the first occurring non-zero number to begin the processing with. This has mostly use case in Machine Learning domain in which we require to process data excluding None or 0 values. Let's discuss certain ways in which this can be performed. Method #1 : Using next() + enumerate() The next function can be used to iterate
10 min read
Return a boolean array which is True where the string element in array ends with suffix in Python
In this article, we are going to see how we will return a boolean array which is True where the string element in the array ends with a suffix in Python. numpy.char.endswith() numpy.char.endswith() return True if the elements end with the given substring otherwise it will return False. Syntax : np.char.endswith(input_numpy_array,'substring') Parame
2 min read
Python program to fetch the indices of true values in a Boolean list
Given a list of only boolean values, write a Python program to fetch all the indices with True values from given list. Let's see certain ways to do this task. Method #1: Using itertools [Pythonic way] itertools.compress() function checks for all the elements in list and returns the list of indices with True values. C/C++ Code # Python program to fe
5 min read
Python | Count true booleans in a list
Given a list of booleans, write a Python program to find the count of true booleans in the given list. Examples: Input : [True, False, True, True, False] Output : 3 Input : [False, True, False, True] Output : 2 Method #1: Using List comprehension One simple method to count True booleans in a list is using list comprehension. C/C++ Code # Python3 pr
3 min read
Python | Test if any list element returns true for condition
Sometimes, while coding in Python, we can have a problem in which we need to filter a list on basis of condition met by any of the element. This can have it's application in web development domain. Let's discuss a shorthand in which this task can be performed. Method : Using any() + list comprehension The simplest way and shorthand to solve this pr
4 min read
Python | Segregate True and False value indices
Sometimes, while working with Python lists, we can have a problem in which we have boolean list and require to have indices of True and False values separately. This can have a possible use in Data Science domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop This is the one way in which this task can be per
6 min read
Python - Test if a list is completely True
Sometimes, we need to check if a list is completely True, these occurrences come more often in testing purposes after the development phase. Hence, having a knowledge of all this is necessary and useful. Lets discuss certain ways in which this can be performed. Method #1 : Naive Method In the naive method, we just run a loop from beg to end of list
6 min read
Article Tags :
Practice Tags :