A great automation isn't just a rigid list of commands; it’s a smart system that knows how to make "choices." Should the bot skip a missing product? Should it flag a high-value order for review? This decision-making power is called Conditional Logic. In Python, if statements are the brain of your script. If you’ve ever used an "If/Else" branch in an RPA designer, you already know the logic—now, it's time to learn the language.
The Foundation: "If... Then..."
A Conditional Statement allows a program to check if a specific condition is True or False before deciding whether to run a block of code.
Basic Syntax:
if condition:
# If the condition is True, execute this indented block
print("Condition met!")
The Golden Rules:
The Colon (
:): Never forget the colon at the end of theifline; it signals that a code block is starting.Indentation: Python uses 4 spaces (or a tab) to identify which lines belong to the
ifbranch. No indentation means the code is outside the "choice."
The RPA Mirror:
In Octoparse AI, dragging in an "If/Else" command is exactly the same as writing an if statement. The "Condition Box" in the RPA tool is your Python if condition:, and the actions inside the "Then" branch are your indented lines of code.
Handling Multiple Paths: If-Elif-Else
Real-world tasks often have more than two outcomes. For these, we use a chain of checks.
The Logic: Python checks the conditions from top to bottom. As soon as it finds one that is
True, it executes that block and skips the rest of the chain.Syntax:
if condition_1:
# Action for Case 1
elif condition_2:
# Action for Case 2 (Else If)
else:
# Action for all other cases
RPA Mapping: The elif (short for "else if") and else parts correspond directly to the additional branches you can add to a Conditional Activity in the RPA designer.
Building the Condition: Operators
To make a choice, you need to compare data. We use Comparison and Logical operators to do this.
Comparison Operators (Comparing Values)
Operator | Meaning | Example (Returns True) | RPA Equivalent |
== | Equal to | 10 == 10 | Equals |
!= | Not equal to | 10 != 5 | Not Equals |
> | Greater than | 10 > 5 | Greater Than |
in | Included in | "A" in ["A", "B"] | Contains / Includes |
Logical Operators (Combining Conditions)
and: Both conditions must be True.or: At least one condition must be True.not: Reverses the result (True becomes False).
In Octoparse AI: These match the "And/Or" settings you select when combining multiple sub-conditions in the parameter configuration editor.
Putting It All Together: Real-World Pattern
Let's see how a bot handles a pricing logic scenario using the variables and types we learned in the previous lesson:
Example:
# Variables gathered from the webpage
order_amount = 1500
customer_type = "vip"
if order_amount > 1000 and customer_type == "vip":
discount = 0.2 # 20% discount for VIP large orders
elif order_amount > 1000:
discount = 0.1 # 10% discount for regular large orders
else:
discount = 0 # No discount for small orders
Nested Logic: Just like dragging an If-activity inside another If-branch in RPA, you can "nest" if statements in Python to create even more precise filters.
Summary: The Brain of the Bot
if-elif-else structures are the core of programmatic decision-making. By using comparison and logical operators, your bot can automatically choose different paths based on live data. Whether you are using a visual flowchart or a Python script, the logic remains identical.
Now that your bot can choose, it’s time to make it scale. In the next guide, we will learn how to make your program repeat tasks tirelessly, processing thousands of data rows with a single command.
