Python LOOPS Exercise for Beginners and Intermediate

Enable the edit mode with double click on the editor.
For answers, please select the invisible text right after Answer:


Write a for loop that iterates over the x list and prints out all the elements of the list.

x = [10, 12, 13, 14, 17, 19, 21, 22, 25]
10
12
13
14
17
19
21
22
25

Answer:

for i in x:
    print(i)

Write a for loop that iterates over the x list and prints out the remainders of each element of the list divided by 3.

x = [10, 12, 13, 14, 17, 19, 21, 22, 25]
1
0
1
2
2
1
0
1
1

Answer:

for i in x:
    print(i % 3)

Write a for loop that iterates over the x list and prints out all the elements of the list in reversed order and multiplied by 10.

x = [10, 12, 13, 14, 17, 19, 21, 22, 25]
250
220
210
190
170
140
130
120
100

Answer:

for i in sorted(x, reverse = True):
    print(i * 10)

Write a for loop that iterates over the x list and prints out all the elements of the list divided by 2 and the string Well done! after the list is exhausted.

x = [10, 12, 13, 14, 17, 19, 21, 22, 25]
5.0
6.0
6.5
7.0
8.5
9.5
10.5
11.0
12.5
Well done!

Answer:

for i in x:
    print(i / 2)
else:
    print("Well done!")

Write a for loop that iterates over the x list and prints out the index of each element.

x = [10, 12, 13, 14, 17, 19, 21, 22, 25]
0
1
2
3
4
5
6
7
8

Answer:

for i in x:
    print(x.index(i))

Write a while loop that prints out the value of x squared while x is less than or equal to 5. Be careful not to end up with an infinite loop!

x = 0
0
1
4
9
16
25

Answer:

while x <= 5:
    print(x ** 2)
    x += 1

Write a while loop that prints out the value of x plus 10 while x is less than or equal to 15 and the remainder of x divided by 5 is 0. Be careful not to end up with an infinite loop!

x = 10
20

Answer:

while x <= 15 and x % 5 == 0:
    print(x + 10)
    x += 1

Write a while loop that prints out the value of x times y while x is greater than or equal to 5 and less than 10, and prints out the result of x divided by y when x becomes 10. Be careful not to end up with an infinite loop!

x = 5
y = 2
10
12
14
16
18
5.0

Answer:

while x >= 5 and x < 10:
    print(x * y)
    x += 1
else:
    print(x / y)

Write code that will iterate over the x and y lists and multiply each element of x with each element of y, also printing the results to the screen.

Hint: use nesting!

x = [2, 4, 6]
y = [5, 10]
10
20
20
40
30
60

Answer:

for i in x:
    for j in y:
        print(i * j)

Write code that will iterate over the x and y lists and multiply each element of x that is greater than 5 with each element of y that is less than 12, also printing the results to the screen.

Hint: use nesting!

x = [2, 4, 6, 8]
y = [5, 10, 15, 20]
30
60
40
80

Answer:

for i in x:
    for j in y:
        if i > 5 and j < 12:
            print(i * j)

Write code that will iterate over the x and y lists and multiply each element of x with each element of y that is less than or equal to 10, also printing the results to the screen. For ys elements that are greater than 10, multiply each element of x with y squared.

Hint: use nesting!

x = [2, 4, 6, 8]
y = [5, 10, 15, 20]
10
20
450
800
20
40
900
1600
30
60
1350
2400
40
80
1800
3200

Answer:

for i in x:
    for j in y:
        if j <= 10:
            print(i * j)
        else:
            print(i * j ** 2)

Write code that will print out each character in x doubled if that character is also inside y. Hint: use nesting!

x = "cryptocurrency"
y = "blockchain"
cc
oo
cc
nn
cc

Answer:

for i in x:
    if i in y:
        print(i * 2)

Write code that will iterate over the range generated by range(10) and for each element  that is between 3 and 7 inclusively print out the result of multiplying that element by the third element in the same range.

Hint: use nesting!

my_range = range(10)
6
8
10
12
14

Answer:

for i in my_range:
    if 3 <= i <= 7:
        print(i * my_range[2])

Write code that will iterate over the range starting at 5, up to but not including 25, with a step of 5, and for each element that is between 10 and 21 inclusively print out the result of multiplying that element by the second to last element of the same range. For any other element of the range (outside [10-21]) print Outside! Finally, after the entire range is exhausted print out The end!

Hint: use nesting!

Outside!
150
225
300
The end!

Answer:

for i in range(5,25,5):
    if 10 <= i <= 21:
        print(i * range(5,25,5)[-2])
    else:
        print("Outside!")
else:
    print("The end!")

Insert a break statement where necessary in order to obtain the following result:

x = [1, 2]
y = [10, 100]
1
1
100
10

Answer:

for i in x:
    for j in y:
        if i % 2 == 0:
            break
            print(i * j)
        print(i)
    print(j)

Insert a continue statement where necessary in order to obtain the following result:

x = [1, 2]
y = [10, 100]
1
1
100
20
200
100

Answer:

for i in x:
    for j in y:
        if i % 2 == 0:
            print(i * j)
            continue
        print(i)
    print(j)

Leave a Reply

Prev
Convert Text to Speech with Python in Different Languages
Convert Text to Speech with Python in Different Languages

Convert Text to Speech with Python in Different Languages

In this tutorial, we will learn how to convert text into human-like speech

Next
Python EXCEPTIONS Exercise for Beginners and Intermediate

Python EXCEPTIONS Exercise for Beginners and Intermediate

Enable the edit mode with double click on the editor

You May Also Like