Python Fundamental — Looping

M. Rizky Maulana
2 min readApr 5, 2021

Today we are going to Looping article. But, before that you can check my previous article :

In general, statements in a programming language will be executed sequentially. The first statement in a function executes first, followed by the second, and so on. But there will be situations where you have to write a lot of code, where it is a lot of code. If it is done manually then you will only waste your energy writing hundreds or even thousands of codes. For that you need to use repetition in the Python programming language.

In the Python programming language the repetition is divided into 3 parts, namely:

  • While Loop
  • For Loop
  • Nested Loop

While Loop

While loops in the Python programming language, statements are executed many times as long as the condition is true.

Below is an example of using the While Loop loop.

#Examples of using While Loop#Note: Defining the scope in Python can use tabs instead of bracketscount = 0
while (count <9):
print (“The count is:”, count
count = count + 1
print (“Good bye!”)

For Loop

Python’s for loop has the ability to repeat items of any order, such as lists or strings.

Below is an example of using the For loop.

#Example of a simple for loopnumber = [1,2,3,4,5]
for x in number:
print (x)
#Examples of repetition forfruit = [“pineapple”, “apple”, “orange”]
for food in fruit:
print (“I like to eat”, food)

Nested Loop

The Python programming language allows the use of one loop inside another loop. The following sections provide a few examples to illustrate this concept.

Below is an example of using a Nested Loop.

#Examples of using a Nested Loop#Note: Using modulo on a condition assumes non-zero values ​​as True and zero as Falsei = 2
while (i <100):
j = 2
while (j <= (i / j)):
if not (i% j): break
j = j + 1
if (j> i / j): print (i, “is prime”)
i = i + 1
print (“Good bye!”)

--

--