Skip to main content

Loops in PYTHON (For loop and While loop)

Why do we need loops in python ? And how it makes our codes more simpler and shorter ? 

Sometimes we want to repeat a set of statements in our programme for instance.

If we want to print 1 - 1000 in our python programme. Then it is not easy to print every number separately. Thus we need loops because using only 2-3 lines of code we can do this.

Loops make it easy for a programmer to tell the computer, which set of instruction to repeat as much as times we want.


Types of loops in Python :-

1.) While loop

2.) For loop

We will look into these one by one !

1.) WHILE LOOP :-

The syntax of a while loop looks like this :

while(condition):
    #code accordingly
    #this code will keeps working until the condition is satisfied

In while loops, the condition is checked first. If comes true, the body code will starts running else not.
If the loop is started it will runs until the condition becomes false.

Now lets write a code to print all the numbers from 1 - 100 :-



Now another code to print all the numbers from 1-100 which are divisible 3 :-






2.) FOR LOOP :-

A for loop is used to iterate through a sequence like list, tuple, or string.
The syntax of the for loop is :-

mylist  =  [4, 2, 8, 4, 78]
for i in range(0, len(mylist)+1):
     print(mylist[i])

Now we will try the same programmes for For loop which were used in the while loop section.

First code to print the number from 1-100 :-



Now the second code is to print all the numbers from 1-100 which are divisible by 3 :-




Now we have the basic knowledge of the loops and rest depends on your practice.

What are break statements in python loops ? And to use them in our code ?

Break is used to come out of the loop when encountered. It instructs the programme to exit the loop now.

Use in code :-





Hope you like this tutorial. Please share it with your friends and help them learning python. In the next tutorial we will learn about function and recursion in python.



John Veer
Contact mail id  -  john.veer.utube@gmail.com
Thanks for reading !
                                                                         

Comments

Popular posts from this blog

Bubble Sort Algorithm in PYTHON

What is Bubble Sort ? And why do we need this ? Bubble sort is the simplest algorithm to sort any array. Due to its simplicity, bubble sort is often used to introduce the concept of a sorting algorithm.  In computer graphics it is popular for its capability to detect a very small error (like swap of just two elements) in almost-sorted arrays and fix it with just linear complexity (2n). For example, it is used in a polygon filling algorithm, where bounding lines are sorted by their x coordinate at a specific scan line (a line parallel to x axis) and with incrementing y their order changes . ALGORITHM :- 1.) Firstly we have an unsorted list, which is to be sorted. 2.) Consider the first element of the list. Then compare it with the next element. 3.) If the second element is greator than the first element than swap them. 4.) If the second element is smaller than the first element, then we don't have to do anything. 5.) Going ahead, now after swap (may or may not), look for second...

Merge Sort Algorithm in PYTHON

  First of all, Why do we need to learn so many sorting algorithms? We need to learn so many sorting algorithms because there are cases in which which algorithm fits best. According to the time complexity, we have to choose an algorithm of our use. That's why we are learning so many sorting algorithms. SO LETS START WITH THE MERGE SORT In computer science, merge sort (also commonly spelled as mergesort) is an efficient, general-purpose, and comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the order of equal elements is the same in the input and output. It is one of the most popular algorithms and one of the most stable sorting algorithm. ALGORITHM 1.) First of all take a list and put it in a function (recursion) that it breaks itself into two halves. 2.) When we get the two halves, the recursion code will automatically breaks the two halves into four             parts and then into 8 parts until each...

Queues in PYTHON

  The Python Code for Queue is here :- class Node: def __init__(self, data): self.data = data self.next = None class Queue: def __init__(self): self.front = self.rear = None def enqueue(self, data): newnode = Node(data) newnode.next = self.rear self.rear = newnode if self.front is None: self.front = newnode def dequeue(self): temp = self.rear while temp.next is not self.front: temp = temp.next self.front = temp temp.next = None def traverse(self): temp = self.rear while temp is not None: print(temp.data, end = " --> ") temp = temp.next print("None") new = Queue() new.enqueue(4) new.enqueue(1) new.enqueue(0) new.enqueue(2) new.traverse() new.dequeue() new.traverse() new.dequeue() new.traverse() 2 --> 0 --> 1 --> 4 --> None 2 --> 0 --> 1 --> None 2 ...