Loops in Python are a fundamental concept used to execute a block of code repeatedly under certain conditions. There are two main types of loops in Python: for
loops and while
loops.
- For Loops:
- A
for
loop is used for iterating over a sequence (such as a list, tuple, dictionary, set, or string). - The basic syntax is:pythonCopy code
for element in sequence: # do something with element
- It’s commonly used with the
range()
function when you need to repeat an action a certain number of times.
- A
- While Loops:
- A
while
loop repeatedly executes a target statement as long as a given condition is true. - The basic syntax is:pythonCopy code
while condition: # do something
- It’s important to ensure the condition eventually becomes false; otherwise, you’ll create an infinite loop.
- A
Here’s a simple example of each:
For Loop Example
# Print numbers from 0 to 4 for i in range(5): print(i)
While Loop Example
# Print numbers from 0 to 4 i = 0 while i < 5: print(i) i += 1
In both examples, the loop prints numbers from 0 to 4. The for
loop iterates over a range of numbers, while the while
loop continues as long as i
is less than 5, with i
being incremented in each iteration.
Loops can also be nested, and you can use break
and continue
statements to control the flow of the loops. break
exits the loop, while continue
skips to the next iteration.
Example 1 in Manim
Using loops in Manim, you can create interesting animations by repeating certain actions. For instance, if you want to rotate an object continuously, you can use a loop to apply a rotation animation repeatedly.
Here is an example of how you might use a loop in Manim to rotate an object, such as a square:
from manim import *
class RotateSquare(Scene):
def construct(self):
# Create a square
square = Square(color=BLUE)
# Add the square to the scene
self.add(square)
# Number of rotations
num_rotations = 5
# Loop to apply the rotation animation
for _ in range(num_rotations):
# Rotate the square by PI/2 (90 degrees) over 1 second
self.play(Rotate(square, angle=PI/2, run_time=1))
# Keep the final frame for a short duration
self.wait(1)
In this example:
- A square is created and added to the scene.
- A
for
loop is used to rotate the squarenum_rotations
times. Each rotation is 90 degrees (PI/2 radians). - The
Rotate
function is used inside theself.play()
method to animate the rotation of the square. - After the loop completes,
self.wait(1)
keeps the final frame displayed for a short duration.
When you run this script using Manim, it will produce an animation where the square rotates 90 degrees five times in succession. You can modify num_rotations
and the angle
parameter in Rotate
to change the number of rotations and the angle of each rotation, respectively.
Example 2 in Manim
Let’s create an example where we use a loop in Manim to animate a series of objects appearing one after the other with a slight delay between each appearance. This is a common technique to create a “chain reaction” style animation.
In this example, we will animate a series of circles appearing one by one:
from manim import *
class SequentialCircles(Scene):
def construct(self):
# Number of circles
num_circles = 5
# Loop to create and animate each circle
for i in range(num_circles):
# Create a circle with a specific position
circle = Circle(color=RED).shift(RIGHT * i)
# Animate the circle appearing with a fade-in effect
self.play(FadeIn(circle, run_time=0.5))
# Short delay after each circle is created
self.wait(0.2)
# Keep the final frame for a short duration
self.wait(1)
In this script:
- We define a
num_circles
variable to specify how many circles we want to animate. - A
for
loop is used to create and animate each circle one by one. - Each circle is positioned to the right of the previous one (
RIGHT * i
) to create a line of circles. - The
FadeIn
animation is used within theself.play()
method to make each circle appear with a fade-in effect. - There is a slight delay (
self.wait(0.2)
) after each circle appears to create a staggered effect. - After all circles have appeared,
self.wait(1)
keeps the final frame displayed for a short duration.
When run, this script will produce an animation where each circle fades in one after the other, creating a sequential appearance effect. You can modify the number of circles, their colors, and the delay to create different variations of this animation.
It will look like this!Example 3 in Manim
Below is an example code snippet that will create an animation featuring four squares, one after the other.
class FourSquares(Scene):
def construct(self):
# Define four squares
squares = [Square().scale(0.5) for _ in range(4)]
# Position squares next to each other
for i in range(1, 4):
squares[i].next_to(squares[i - 1], RIGHT)
# Create an animation where each square is drawn one after the other
for i in range(4):
self.play(Create(squares[i]))
# Keep the squares displayed
self.wait(2)
The code will do the following:
- Create four squares.
- Position them next to each other.
- Animate their appearance one after the other.