Hi there, we’re Harisystems
"Unlock your potential and soar to new heights with our exclusive online courses! Ignite your passion, acquire valuable skills, and embrace limitless possibilities. Don't miss out on our limited-time sale - invest in yourself today and embark on a journey of personal and professional growth. Enroll now and shape your future with knowledge that lasts a lifetime!".
For corporate trainings, projects, and real world experience reach us. We believe that education should be accessible to all, regardless of geographical location or background.
1Python Nested Loops
A nested loop is a loop within another loop. It allows you to execute a set of instructions repeatedly, with one loop inside another. Nested loops are useful when you need to perform repetitive tasks that require multiple levels of iteration. Let's explore the syntax and usage of nested loops in Python with examples.
Syntax
The basic syntax of a nested loop in Python is as follows:
for outer_item in outer_sequence:
for inner_item in inner_sequence:
# Code to execute for each combination of outer_item and inner_item
Example 1: Multiplication Table
Consider the following example:
for i in range(1, 6):
for j in range(1, 11):
print(i, "*", j, "=", i*j)
print() # Print an empty line after each row
In this example, we have a nested loop structure to generate a multiplication table. The outer loop iterates over the numbers from 1 to 5, representing the multiplicand. The inner loop iterates over the numbers from 1 to 10, representing the multiplier. Inside the inner loop, we calculate and print the multiplication of the current multiplicand and multiplier. After each inner loop iteration, we print an empty line to separate the rows of the multiplication table.
Example 2: Pattern Printing
You can also use nested loops to print patterns. Here's an example that prints a triangle pattern:
rows = 5
for i in range(rows):
for j in range(i + 1):
print("*", end=" ")
print()
In this example, the outer loop iterates over the numbers from 0 to 4, representing the number of rows in the triangle. The inner loop iterates over the numbers from 0 to the current row number. Inside the inner loop, we print an asterisk followed by a space. After each inner loop iteration, we move to the next line using the print() function, resulting in the triangle pattern being printed.
Example 3: Nested While Loop
Nested loops can also involve a combination of for loops and while loops. Here's an example:
i = 1
while i <= 5:
j = 1
while j <= i:
print(j, end=" ")
j += 1
print()
i += 1
In this example, we have a nested loop structure that combines a while loop and a for loop. The outer while loop iterates over the numbers from 1 to 5. Inside the outer loop, we have a nested while loop that iterates from 1 to the current value of the outer loop variable. Inside the inner loop, we print the current value of the inner loop variable followed by a space. After each inner loop iteration, we move to the next line using the print() function.
Conclusion
Nested loops are powerful constructs in Python that allow you to perform repetitive tasks with multiple levels of iteration. By understanding the syntax and usage of nested loops, you can tackle complex problems, generate patterns, and process multidimensional data structures efficiently in your Python programs.
4.5L
Learners
20+
Instructors
50+
Courses
6.0L
Course enrollments
Future Trending Courses
When selecting, a course, Here are a few areas that are expected to be in demand in the future:.
Future Learning for all
If you’re passionate and ready to dive in, we’d love to join 1:1 classes for you. We’re committed to support our learners and professionals their development and well-being.
View CoursesMost Popular Course topics
These are the most popular course topics among Software Courses for learners