What is For Loops in Python | EP-39 For Loops in Python | Python For Loops | Free Python Tutorials
For Loops in Python
"""
The For Loops in Python are a special type of loop statement that is used for sequential traversal. Python For loop is used for iterating over an iterable like a String, Tuple,
List, Set, or Dictionary.
Python For Loop Syntax
for var in iterable:
statements
Note: In Python, for loops only implement the collection-based iteration."""
1. Python For Loop with String
"""This code uses a for loop to iterate over a string and print each character on a new line. The loop assigns each character to the variable i and continues until all characters in the string have been processed."""
course="Python Tutorial"
for i in course:
print(I)
2. Python for loop with Range
"""The range() function in Python generates a sequence of numbers, starting from the specified start value (inclusive), ending before the stop value (exclusive), and optionally stepping by a given interval.
It's commonly used to iterate over a specific number of times within a for loop.
syntax:
for variable in range(start, stop, step):
Code to be executed in each iteration
variable: A temporary variable that takes on each value from the generated sequence in each iteration.
start: The starting value (default is 0).
stop: The ending value (exclusive).
step: The interval between values (default is 1).
"""
for j in range(5):
print(j)
for num in range(2,10,4):
print(num,"num")
for i in range(5,0,-1):
print(i,"I")
3. Python for loop Enumerate
"""The enumerate() function in Python is a built-in generator that returns pairs of indices and values from an iterable object (such as a list, tuple, or string).
It's commonly used when you need to iterate over an iterable and access both the elements and their corresponding indices simultaneously.
syntax:
for index, value in enumerate(iterable):
Code to be executed in each iteration
index: The current index of the element in the iterable.
value: The value of the current element in the iterable.
iterable: The iterable object you want to enumerate."""
fruits = ["apple", "banana", "orange"]
for index,fruit in enumerate(fruits):
print(index,fruit)
#PythonForLoops #ForLoopsInPython #PythonTutorial #PythonProgramming #CodingForBeginners #LearnPython #PythonBasics #ProgrammingTutorials #FreePythonTutorials #PythonLooping #PythonCoding #CodeWithPython #PythonTips #PythonDevelopment #python3 #learntocode #programmingtutorial #pythoncode #pythonbeginner
Connect With Us:
—————————————
➡️ Website: https://www.cybrosys.com/
➡️ Email: info@cybrosys.com
➡️ Twitter: / cybrosys
➡️ LinkedIn: / cybrosys
➡️ Facebook: / cybrosystechnologies
➡️ Instagram: / cybrosystech
➡️ Pinterest: / cybrosys