1. Loops (Concept):
Definition: A Loop is a programming concept that
allows you to repeat a block of code multiple times without writing the same
code over and over again. It is the ultimate tool for Automation. Instead of
saying 'Do this, then do this, then do this...', you say 'Do this 100 times'.
The Two Main Types:
1. For Loop: Used when you know exactly how
many times you want to repeat something (e.g., "Run 5 laps").
2. While Loop: Used when you want to repeat until
a condition changes (e.g., "Run until you get tired").
2. Membership Operators:
Definition: These are special keywords used to
test if a specific item is present inside a collection (like a List,
String, or Tuple). They act like a 'Search' tool that returns a simple True or False.
The Syntax: There are two operators:
a. in:
§ Returns True if the value is found inside the list.
§ Returns False if it is missing.
b. not in:
§ Returns True if the value is NOT found inside the list.
§ Returns False if it is present.
For Example,
3. For Loop:
Definition: It is a control flow statement used
to iterate (loop) over a Sequence (like a List, String, or Tuple). It
takes each item from the sequence one by one, stores it in a temporary
variable, and executes the code block for that item.
The Syntax: for variable in sequence:
How it Works (The Conveyor Belt):
1. Sequence: Imagine a conveyor belt with items
on it (the List).
2. Variable: Imagine a robotic arm that picks up one
item at a time.
3. Action: The robot does something with that
item (prints it, calculates it).
4. Repeat: The robot moves to the next item until the belt is empty.
For Example,
The Loop Variable:
§ In the example above, name is just a temporary placeholder. You can name it anything
(e.g., x, item, student), but it is best to give it a meaningful name.
Looping a Specific Number of Times: To loop a specific number of times (e.g., 5 times), we use the range() function:
For Example,
4.
Range Function (range()):
Definition: It is a built-in function that
generates a sequence of numbers. It is most commonly used with For Loops to
tell Python exactly how many times to repeat an action. Instead of creating a
list like [0, 1, 2, 3, 4], we simply write range(5).
The Syntax: range(start, stop, step)
It can take up to 3 arguments:
a.
Start (Optional): Where to begin. (Default is 0).
b.
Stop (Required): Where to end. Crucial Rule: It stops before this number
(Exclusive).
c. Step (Optional): The gap between numbers. (Default is 1).
For Example,
5. While Loop:
Definition: It is a control flow statement that
repeats a block of code as long as a specific Condition remains True. Unlike a For Loop (which runs a fixed number of times), a
While Loop runs indefinitely until the condition becomes False.
The Syntax: while condition:
Infinite Loops: If the condition never becomes False, the loop will run forever and crash your program. You must include a line of code that updates the variable.
For Example,
6. Loop Control Statements
Definition: Sometimes a loop is running, but you
need to change its behaviour mid-way. These three keywords give you manual
control over the loop's execution.
a.
Break (The Emergency Stop)
§ Action: It stops the loop immediately
and exits it completely.
§ Use Case: You found what you were looking for, so there is no need to keep searching.
For Example,
b.
Continue (The Skip Button):
§ Action: It skips the current iteration
(round) and jumps straight to the next one.
§ Use Case: You want to ignore specific items but keep the loop running for the rest.
For Example,
c.
Pass (The Placeholder):
§ Action:
It does absolutely nothing.
§ Use Case:
Python forbids empty code blocks. If you want to write a loop (or function)
later but need the syntax to work now, you use pass to avoid errors.
Comments
Post a Comment