Python Basics with FOR loop

Python Basics with FOR loop

The for loop is a fundamental programming construct used in Python and other programming languages. It allows you to iterate over a sequence of values, such as a list or a range of numbers, and perform some action on each value in turn.

Here's an example of a simple for loop that iterates over a list of numbers and prints each one:

numbers = [1, 2, 3, 4, 5]
for num in numbers:
print(num)

In this code, we define a list of numbers and then use a for loop to iterate over each number in the list. The loop variable num takes on the value of each number in turn, and the print() function is called to output that value.

You can also use a for loop to iterate over a range of numbers, like this:

for i in range(5):
print(i)

In this code, we use the range() function to generate a sequence of numbers from 0 to 4 (inclusive). The loop variable i takes on the value of each number in turn, and the print() function is called to output that value.

You can combine the for loop with other Python constructs to perform more complex operations. For example, you might use a for loop to iterate over the lines of a file, or the keys of a dictionary.

I hope this helps you get started with Python and the for loop! Let me know if you have any further questions.

Python Basics with FOR loop

Facebook Reviews:

If you are a training provider

Interested to offer our courses in your own platform with Life-time Resale License?