A for loop is used to iterate over elements in a sequence (like a list, tuple, or string) or other iterable objects. It allows you to execute a block of code repeatedly for each item in the sequence.
Here’s an example of a for loop in Python:
fruits = ['apple', 'banana', 'cherry', 'grape']
for fruit in fruits:
print(fruit)
Code language: Python (python)
In this example, we have a list called fruits
that contains four elements: ‘apple’, ‘banana’, ‘cherry’, and ‘grape’. The for loop iterates over each item in the fruits
list, and the variable fruit
takes the value of each element one by one in each iteration. The print(fruit)
statement inside the loop will output each fruit name on a separate line:
Output:
apple
banana
cherry
grape
Code language: Python (python)
The loop continues until it has iterated over all the elements in the fruits
list. For loops are commonly used for tasks like iterating through collections, processing elements, or performing repetitive actions on data.
Simple example of for loop in python
Here are four more examples of for loops in Python:
- Summing elements in a list:
numbers = [1, 2, 3, 4, 5]
sum_result = 0
for num in numbers:
sum_result += num
print("Sum of numbers:", sum_result)
Code language: Python (python)
Output:
Sum of numbers: 15
Code language: Python (python)
- Printing numbers in a range with a step:
for i in range(0, 10, 2):
print(i)
Code language: Python (python)
Output:
0
2
4
6
8
Code language: Python (python)
- Finding the maximum element in a list:
numbers = [12, 45, 6, 27, 8, 33]
max_num = numbers[0]
for num in numbers:
if num > max_num:
max_num = num
print("Maximum number:", max_num)
Code language: Python (python)
Output:
Maximum number: 45
Code language: Python (python)
- Iterating over characters in a string:
message = "Hello, Python!"
for char in message:
print(char)
Code language: Python (python)
Output:
H
e
l
l
o
,
P
y
t
h
o
n
!
Code language: Python (python)
In these examples, you can see how for loops are used to perform various tasks, such as calculating sums, iterating over ranges, finding maximum values, and processing characters in a string. For loops are versatile and can be applied to many different situations in Python programming.
Here are five more examples of for loops in Python:
- Counting even and odd numbers in a list:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_count = 0
odd_count = 0
for num in numbers:
if num % 2 == 0:
even_count += 1
else:
odd_count += 1
print("Even numbers:", even_count)
print("Odd numbers:", odd_count)
Code language: Python (python)
Output:
Even numbers: 5
Odd numbers: 5
Code language: Python (python)
- Printing the multiplication table:
number = 7
for i in range(1, 11):
result = number * i
print(f"{number} x {i} = {result}")
Code language: Python (python)
Output:
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
Code language: Python (python)
- Printing characters with their ASCII values:
message = "Hello, Python!"
for char in message:
ascii_value = ord(char)
print(f"{char}: {ascii_value}")
Code language: Python (python)
Output:
H: 72
e: 101
l: 108
l: 108
o: 111
,: 44
: 32
P: 80
y: 121
t: 116
h: 104
o: 111
n: 110
!: 33
Code language: Python (python)
- Iterating over a dictionary and printing key-value pairs:
student_scores = {"Alice": 85, "Bob": 92, "Charlie": 78, "David": 95}
for name, score in student_scores.items():
print(f"{name} scored {score} marks.")
Code language: Python (python)
Output:
Alice scored 85 marks.
Bob scored 92 marks.
Charlie scored 78 marks.
David scored 95 marks.
Code language: Python (python)
- Reversing a list using a for loop:
numbers = [1, 2, 3, 4, 5]
reversed_numbers = []
for i in range(len(numbers) - 1, -1, -1):
reversed_numbers.append(numbers[i])
print("Original list:", numbers)
print("Reversed list:", reversed_numbers)
Code language: Python (python)
Output:
Original list: [1, 2, 3, 4, 5]
Reversed list: [5, 4, 3, 2, 1]
Code language: Python (python)
In these additional examples, you can see how for loops are used for counting elements, generating multiplication tables, working with ASCII values, iterating through dictionaries, and reversing lists. For loops are an essential part of Python and are often used in various programming tasks.
What is the for in Python?
In Python, “for” is a keyword used to create a loop that iterates over a sequence of elements. It is used to execute a block of code repeatedly for each item in the sequence.
What is for () an example of in coding?
The phrase “for ()” is not commonly used in Python coding or programming in general. The correct syntax of a for loop in Python does not involve parentheses after “for.” If this is referring to a specific context or pattern, more context would be needed to provide an accurate answer.
Read More;
- How to write update query in MySQL in Python?
- Simple Example Python Programs for Practice [Beginners ]
- How can I run a Python script online for free?
- What is Generator in Python With Example?
- What is class and object in Python with example?
- What is an example of a user-defined function in Python?
- Can R and Python be used together? [With Example]
- How does format () work in Python?
- What is .2f Python format?
- What is the e function in Python?
- How do you write an automation test script in Python?
- How do you automate daily tasks in Python?