STEM | Beginning Python/Robotics | (3) Creating Patterns with Python L

Loops are one of the most important concepts in programming. They allow us to repeat a block of code multiple times without writing it manually. Python has two main types of loops: for loops and while loops. One fun way to learn how loops work is by using them to create patterns!

The first pattern is a right-angled triangle. We use a for loop to print rows of stars (*), increasing the number of stars in each row.

print("Pattern 1: Right-Angle Triangle (For Loop)")
for i in range(1, 6):  # Looping 5 times
	print('*' * i)  # Print '*' repeated 'i' times
	

Now, let’s use a while loop to create an inverted triangle.


print("Pattern 2: Inverted Right-Angle Triangle (While Loop)")
rows = 5
while rows > 0:  # Loop until 'rows' is 0
    print('*' * rows)  # Print 'rows' number of stars
    rows -= 1  # Decrease the value of 'rows' by 1

Loops are powerful tools that make repetitive tasks simple. By using for and while loops creatively, we can generate interesting patterns. Try modifying the code to create your own designs!

Happy coding!


Bonus Activity: Disk Transport

Using the Vex VR robotics simulator, we practiced programming our virtual vehicle to transport the color disks to their respective, color-coded squares. In addition to using the drive-train for the vehicle, we also must learn to operate the magnet to boost or drop the disk.