Python Code Examples: For Loop in Python

When you are working on your programming assignment, you might need Python code examples which you can find on our website. Our service was created with the purpose to help students reach their academic goals. If you are a high-school, college or university student, you can apply to us for help.

We have published some Python code examples that were completed by our experts. Before using our examples, you would probably like to know more about them and the people who create them. So, let’s take a look at the authors who complete our examples.

We look for experts in diverse fields, including programming. They have deep knowledge and vast experience in particular fields. All of them are accredited with high-level academic degrees. As a result, they are aware of academic standards and complete their tasks in accordance with certain standards.

You can get acquainted with one of our samples below. We hope it will be really helpful for you. If it is not what you are looking for, you can take a look at our other examples or order a specific one just for you. All the samples that you can find on our website are free. If you order a specific sample, you will need to pay for it. However, paid examples will be completed according to all your requirements. Therefore, it will definitely be the most helpful for you.

Now, you should examine our sample thoroughly and decide whether it is exactly what you were looking for. If it is not, contact us and we’ll try to help you!

The for loop, also called the cycle with a parameter in the Python language, is rich in possibilities. In the for loop we specify a variable and a set of values on which to run the variable. The set of values can be a list, a tuple, a string or band.

Here is a simple example of using the loop, where the tuple is used as the set of values:

i = 1
for color in 'red', 'orange', 'yellow', 'green', 'cyan', 'blue', 'violet':
    print('#', i, ' color of rainbow is ', color, sep = '')
    i += 1

In this example, the color variable sequentially receives red, orange, etc. The body of the loop displays a message that contains the name of the color, that is, the value of variable color, as well as the number of iteration of the loop number, which initially is equal to 1, and then increased by one (instruction i + = 1, with each pass of the cycle; instruction i + = 1 equivalent design i = i + 1 ( it’s just shorthand)).

The list of values can be represented by the expressions of any type, for example:

for i in 1, 2, 3, 'one', 'two', 'three':
    print(i)

First 3 iterations i will have the type of int, next 3 times – string.

Range function

Typically, cycles are used for a repetition of a sequence of actions a predetermined number of times, or to change the value of a variable in a loop from some initial value to a final.

To repeat the cycle of a predetermined number of times n, you can use a for loop with the function range:

for i in range (4): # equivalent to instructions for i in 0, 1, 2, 3:
 # Here you can perform cyclic actions
 print (i)
 print (i ** 2)
print ( 'The end of the cycle')

Range function can also take not one, but two parameters. Call range (a, b) means that the index variable will take values from a to b - 1. If a≥b, the cycle will not be executed even once. For example, in order to sum the values of the numbers from 1 to n, you can use the following program:

sum = 0
n = 5
for i in range(1, n + 1):
    sum += i
print(sum)

In this example, the variable i takes the values 1, 2, ..., n, and the value of the sum increases sequentially.

Finally, in order to organize the cycle in which the index variable is reduced, it is necessary to use a range function with three parameters. The first parameter specifies the initial value of the index variable, the second argument – a final variable, and the third parameter – the amount of change of the index variable. For example, to make a loop over all odd numbers from 1 to 99, we can use the function range(1, 100, 2).

Setting the print() function

Default print () function takes several arguments, removes them through the gap, and then puts the line break. This behavior can be changed using named parameters sep (separator) and end (the end).

print(1, 2, 3)
print(4, 5, 6)
print(1, 2, 3, sep=', ', end='. ')
print(4, 5, 6, sep=', ', end='. ')
print()
print(1, 2, 3, sep='', end=' -- ')
print(4, 5, 6, sep=' * ', end='.')

Thanks for your attention!

Leave a Reply

Your email address will not be published. Required fields are marked *

Customer testimonials

Submit your instructions to the experts without charge.