Skip to main content

Python Loops

Python Loops

Hi. Guys welcome back to another amazing tutorial on this platform. If you are new here, please check out our previous tutorial on python and subscribe to our YouTube channel for the video tutorials.

Today we are going to look at another interesting program on python called the python loops. To begin with, a loop is a control structure that allows the compiler to repeat the execution of a statement or group of statements. They are two main types of loops which are the ‘for loop’ and the ‘while loop’.

  1. The for loop

We can use the ‘for loop’ to execute programs which are in sequence data types such as lists, strings, or tuples.

Syntax

for val in sequence
statement(s)

The ‘for statement’ stores every value of each elements on the sequence until all the elements in the loop are exhausted. Let’s look at examples of for loop with data types

Loops with string

letter=('p','r','s','o','g','r')
for letter in 'programming':
  print('<', letter, '>')

OUTPUT

Python Loops

For loop with a tuple

color=('red','green','balck')

for x in color:
 print("I'm wearing a", x, "shirt")

print("multi-colored shirts are cool!")

OUTPUT

Python Loops

Using for loop with the range function

We can use the range function to know the exact numbers required by a loop.

x=15

total=0
for number in range(1, x+1):
  total+=number
print("sum of 1 and numbers from 1 to %d:%d"%(x, total))

OUTPUT

Python Loops
  1. The while loop

The “while loop” is used when you need to repeatedly execute a statement or group of statements while the given condition is true.

Syntax

While conditions
     statements

Here is a program that adds number up to the desired number entered by the user. 

i = 2
while i < 8:
  print(i)
  i += 1

OUTPUT

Python Loops

Break statement

We used the break statement to end the present loop while instructing python to execute the first statement. It is commonly used to prevent the else statement.

Syntax 

break

Here is a loop that ends once it reaches the word ‘sloth’

animals=['lion', 'tiger','sloth','elephant']
for name in animals:
 if name=='sloth':
  break
 print('cool animal:', name)
print("Amazing animals!")

output

Python Loops

Continue statement

The compiler skips the specified iteration and takes all the other iterations.

Syntax

continue

Example

animals=['lion', 'tiger','elephant']
for name in animals:
 if name=='sloth':
  continue
 print('cool animal:', name)
print("Amazing animals!")

OUTPUT

Python Loops

Pass statement

It is an empty operation in python. The interpreter reads and executes the pass statements but returns nothing as the result. It is mostly used to mark codes that will eventually be written.

Syntax


Pass

Comments

Popular posts from this blog

Introduction to flask

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session because we are entering into one of python’s web-based application called flask. In this article, we are going to see the following What is flask Prequistes Installation of flask in python Some of flask template engine. What is flask? Flask is one of python-based framework which is used to create web applications. Flight is a very light web framework. During installation, flask comes with pre-installed modules and functions which are used to backup your own web applications. Flask is very easy to understand and perfect go-to for beginners and with flask, a web server can run in less than 3 lines of code. Prequistes Before learning flask, we recommend the reader to have a basic mastery of python concepts. Installation of flask  Before installing flask, we have to checked if python has been installed or. If n...

How to generate random numbers using NumP1

Hello. Welcome to another edition on this platform. For more better understanding, do checkout our YouTube channel to get the video tutorial. In this article of today, we are going to see how to generate random numbers using any of the following methods: Generating a random number Generating a random float and integer Generating random number arrays Generating random number from an array What is a random number? This is a number which cannot be predicted before its occurrence. This number might not different every time. Programmatically, they are two categories of random numbers:     Pseudo-Random numbers       True Random numbers. Just as programs which are written by programmers are a set of instructions, we must follow an algorithm to generate random numbers. Random numbers which are generated using an algorithm are called Pseudo-Random numbers. To generate a true random number, it is important to get the data from sources such as the keyboards, mou...

Introduction to Django

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session. we are entering into one of python’s application called Django. In this article, we are going to discuss the following: What is Django Why must we use Django  A brief history of Django Installation of Django Popularity of Django What is Django? Python has so many framework application and Django happen to be one of them. Being a python-based-framework, it is used to quickly create web applications.  When building websites, django provides similar ready-made components to handle user authentication, forms, a way to upload components. Why use django? Django is very fast. It takes applications from concept to applications very quickly Django has thousand available packages in it when it is installed. With django, we can launch web applications is a matter of hours. Django is a highly is secured and helps...