Loops in Python - ProgrammerTech
Language:

Search

Loops in Python

  • Share this:
Loops in Python

Loops

Loops Iterative loops are used in programming in general to not repeat code or anything we want multiple times, when we want to repeat this code several times we just put it within the repetition loop and implement it and set conditions according to the progress of the program we are working with.

 

while loop in Python

We use a while loop when we want to execute a code an unlimited number of times, and the loop is stopped when the condition we set while writing the code is met and the condition is in parentheses.

Example 1

a = 5
while a < 12:
. . . print("a")

In this example, we set the value of a to 5 and set the while condition, as long as a is less than 12, it will still print an infinite number for us, it will print a and not stop.

Example 2

a = 0
while a < 10
    a += 1
    print (a)

In the previous example, we set the value of a to 0 and we set a condition if a is less than 10 and we told him a += 1 keep adding 1 until it reaches the number 10 and stops.

Example 3

a = 0
while a <= 10:
    a += 1
    if a == 3:
        print ("3")
    print(a)

We notice in the example that we set the if condition if a equals 3 prints us the number 3 and actually prints the number 3 for us twice because we set the first print function for the condition equal to 3 and the second condition to print the lope numbers.

We can skip one of the values ​​using the continue function, as in the following example

Example 4

a = 0
while a <= 10:
    a += 1
    if a == 3:
        continue
    print(a)

We will also notice when running that we skipped the 3, and we can write the pass function also, which means a pass without skipping anything, and we can also stop the loop using a break.

Example 5

a = 0
while a <= 10:
    a += 1
    if a == 4:
        break
    print(a)

We'll notice that it prints for us up to 3 and stops because we used break .

Example 6

Write a program that prints the word Programmer Tech five times using a while loop in Python.

counter=1
while counter<=5:
    print('Programmer Tech')
    counter=counter+1

We created the counter and its value is 1, that is, it will start from the number 1, and we put a while loop and we said if the counter value is less than or equal to 5, print the word Programmer Tech and increase it in each cycle one until it reaches the number 5 and stops.

When we ran the above code, the result was printing the sentence five times.

Programmer Tech
Programmer Tech
Programmer Tech
Programmer Tech
Programmer Tech

Example 7

Write a Python program that prints numbers from 1 to 5 using a while loop.

counter=1
while counter<=5:
    print(counter)
    counter=counter+1

We created the counter and its value is 1, that is, it will start from the number 1, and we put a while loop and we said if the value of the counter is less than or equal to 5, print the counter and increase it in every one cycle until it reaches the number 5 and stops.

When we ran the previous code, it printed numbers from 1 to 5 and stopped.

1
2
3
4
5

Example 8

Write a Python program that prints even numbers from 1 to 20 using a while loop in Python.

x=1
while x<=20:
    if x % 2==0:
        print(x)
    x = x + 1
print("Ok")

We created a variable x and its value is 1, then we entered a while loop and wrote x is less than or equal to 20. Then the condition was if x %2, which numbers are even, print the value of x in each cycle and add 1 to it and at the end that is up to 20, print ok.

The result of the previous code was

2
4
6
8
10
12
14
16
18
20
Ok

Example 9

Write a Python program that prints odd numbers from 1 to 15 and uses a while loop in Python.

x=1
while x<=15:
    if x % 1==0:
        print(x)
    x = x +2
print("Ok")

The result of the previous code

1
3
5
7
9
11
13
15
Ok

 

For loop in Python

A for loop This type of loop is used to repeat the code several times, and is used to pass through all the elements of a string or array and does not need to define a counter, a start or an end.

Example 10

name = "programmer"
for i in name:
    print(i)

We created the variable name and set it to the programmer then we opened the for loop and told the program is i in name and print i and after running the code the loop passed through all the elements of the string and printed character by character meaning every character in a line.

Example 11

Write a program that prints the word Programmer Tech ten times using the for loop in Python.

for i in range(0,10):
    print("Programmer Tech")

We wrote for, which is the beginning of the episode and the counter is i, that is, its beginning starts from 1 to 10, and type the word Programmer Tech according to the times we set the counter, ie ten times.

Example 12

Write code that prints all integers between 1 and 10 using the for loop in Python.

for i in range(1, 11):
    print(i)

We told the program here to print the numbers from 1 to 10 and the rotation keeps working until it reaches the number 10 and stops, and the result we will find in the form

1
2
3
4
5
6
7
8
9
10

Example 13

We told the program here to print the numbers from 1 to 10 and the rotation keeps working until it reaches the number 10 and stops, and the result we will find in the form

for i in range(1, 21):
    if i%2==0:
        print(i)

We created a for loop and said that we want to print numbers between 1 to 21, but we set a condition that only the even numbers between this ring are printed, and when we run the code we will get the result.

2
4
6
8
10
12
14
16
18
20

Example 14

Write a code that prints all odd numbers between 1 and 40.

for i in range(1, 41):
    if i%3==0:
        print(i)

We created a for loop and said that we want to print numbers between 1 to 41, but we set a condition that it only prints the odd numbers between this ring, and when we run the code we will get the result.

3
6
9
12
15
18
21
24
27
30
33
36
39

Example 15

Write a program that prints the entire English language in small form.

for i in range(97,123):
    print(chr(i))

When you run this code, it will print all lowercase letters for us as.

a
b
c
d
e
f
g
h
To the end of the letters until the letter z

Example 16

Write a program that prints the entire English language capital.

for i in range(65,91):
    print(chr(i))

When you run the code it will print all caps.

Example 17

Create an array of numbers and then print the array's elements using for.

n=[18,6,9,40,60,125]
for index in range(0,6):
    print(n[index])

We created an n filter with 6 elements and inserted it into an iterative loop and when the code is run.

18
6
9
40
60
125

Example 18

Print the contents of an array by passing it by a loop.

admin_P = ["data", "kaissar", "saja", "wazeer"]
for x in admin_P:
    print(x)

We create an array with four text elements and leave them in an iterative loop to print its elements, and when we run the code

data
kaissar
saja
wazeer

Example 19

Write a program that passes all the letters of a word and prints each letter on a line.

for x in "Muhammad":
    print(x)

The result

M
u
h
a
m
m
a
d

Example 20

Write a program that exits the loop when it reaches the word kaissar.

admins = ["data", "saja", "kaissar", "wazeer"]
for x in admins:
    print(x)
if x == "kaissar":
    break

We created a variable which is admins and inside it an array with 4 elements then we wrote an iterative for loop and said if x in the variable admins print x and if x equals kaissar then stop the loop at it.

And when you run the code, the result will be.

data
saja
kaissar

Example 21

Write a program that ignores the word kaissar and completes the loop.

admins = ["data", "saja", "kaissar", "wazeer"]
for x in admins:
    if x == "kaissar":
        continue
    print(x)

The result of the previous code

data
saja
wazeer

Example 22

Program that prints numbers from 1 to 7.

for i in range(1, 8):
    print(i)
    print('for loop')

The result of the previous code

1
for loop
2
for loop
3
for loop
4
for loop
5
for loop
6
for loop

A note on our advice to you

Be like the (while) if you don't reach your goal on the first try. Retry once, two, and three until your goal is achieved and don't be like the (if) link. Your destiny is with one attempt, if it is achieved, it will continue, and if it is not achieved, it will stop.


Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com