CompTIA PenTest+ guideStudy & preparation
CompTIA PenTest+ Scripting: Read Logic in Python, Bash and PowerShell
Short answer
Practice tracing conditions, loops and output, then making one small change and predicting its effect. CompTIA's PT0-003 objectives include scripting logic in objective 2.3 and scripting to automate attacks in objective 4.10. The offline exercises below build code-reading habits; they do not predict an exam question or establish a required level of programming expertise.
Where does scripting appear in PT0-003?
Practice reading conditions, loops and output before extending a script. CompTIA's PT0-003 objectives, checked November 11, 2026, include scripting logic in objective 2.3 and scripting to automate attacks in objective 4.10.
That gives scripting a clear place in your preparation, but it does not tell you which snippet will appear or how much code you will write on your form. Start with the PenTest+ domain and objective overview if you need to place these skills in the broader exam. Objectives 2.3 and 4.10 sit in two different domains, and CompTIA PenTest+ prep planned for launch weights its questions across all five.
The examples here are original teaching exercises using fictional review data. They make no network requests and change no files. Their job is to help you explain what executes, what is skipped and what changes when an input changes. They are not official or recalled exam questions.
How do you trace input, condition and output?
Before running an example, make a small trace on paper. Use these four prompts:
- Input: What values does the snippet start with? Separate the supplied data from anything you are assuming.
- Condition: What exact test chooses the next branch? Name the meaning of true or success in that language.
- Path: Which statement executes next? If there is a loop, repeat the trace for each item.
- Output: What appears, in what order? Include the output that never appears and explain why.
For the fictional findings below, a review flag means only that a reviewer has completed the assigned review. A ready label means ready for the next report-review step. It does not mean the finding was remediated, the asset is secure or an engagement is complete.
Write that meaning beside the input before tracing. Otherwise, you may correctly follow the code but attach an unsupported conclusion to its result. Your first deliverable is a prediction you can compare with the example, not a guess about the author's intention.
How do Python loops and branches change the result?
Python's control-flow tutorial, checked November 14, 2026, explains that a for loop visits items in a sequence in order. An if branch is selected by its condition, with an optional else branch for the alternative.
Original Python exercise: These are the review flags for a saved finding list, copied into memory in this order: F-01, F-02, F-03.
reviewed = [True, False, True]
for finding_reviewed in reviewed:
if finding_reviewed:
print("ready")
else:
print("review")
The output is three lines: ready, then review, then ready.
- F-01 supplies
True, so the first branch printsready. - F-02 supplies
False, so the alternative printsreview. - F-03 supplies
True, so the first branch printsreadyagain.
Change one input: Replace the second flag with True. The output becomes ready, ready, ready. Nothing in the code reviewed F-02; you changed the supplied record. This distinction matters when explaining what a script proves.
If you encounter range, check the endpoint separately. The same Python tutorial states that the endpoint is excluded: range(1, 3) supplies 1 and 2. Changing it to range(1, 4) includes 3. When your predicted output has one extra line, inspect the loop's inputs before blaming the condition.
Next, trace the original three flags without looking at the explanation. If you can state the selected branch for each flag, move to the language comparison.
How do Bash exit status and PowerShell conditions differ?
Microsoft's about_If documentation, checked November 14, 2026, says PowerShell runs a branch when its conditional test evaluates to true. With the explicit Boolean below, there is no need to infer how a different data type would be converted.
Original PowerShell exercise: A single fictional finding has not completed review.
$reviewed = $false
if ($reviewed) {
Write-Host "ready"
} else {
Write-Host "review"
}
The condition is false, so the output is review. Change only $false to $true, and the output becomes ready. Explain that result by the condition and selected branch, not by memorizing the label's position.
Bash uses a different rule for its if construct: according to the GNU Bash manual, checked November 11, 2026, the then branch runs when the test command returns exit status zero.
Original Bash reasoning exercise, written as pseudocode: The fictional offline check's result is supplied. This is not executable shell syntax or an instruction to run a particular command.
Supplied result: offline check returned exit status 0
IF the test command returned exit status 0:
EMIT "check passed"
ELSE:
EMIT "review"
For the supplied status 0, the selected output is check passed. Change the supplied status to 1, and the selected output is review. Here, zero belongs to the command's exit status. Do not transfer that rule to the explicit PowerShell Boolean or the Python review flags.
Keep the result equally narrow: this fictional check passing establishes only its stated result. Before practicing another snippet, identify whether you are reading a Boolean condition or a command's exit status. Short recall work helps keep these language rules apart, and flashcards for all five domains are planned for CompTIA PenTest+ prep.
How do you find and repair one logic error?
Use a stated requirement to decide whether the code is wrong. For the Python example, the requirement is: print ready for a reviewed finding and review for an unreviewed finding.
Now imagine someone swaps the two printed labels while leaving the conditions unchanged. For [True, False, True], the output becomes review, ready, review. The snippet still follows a consistent path, but each label contradicts the requirement.
Repair the two labels in their existing branches. Do not change the finding flags to make the mistaken code produce the desired output. Then check both kinds of input:
- A reviewed finding must reach the true branch and print
ready. - An unreviewed finding must reach the alternative and print
review.
This exercise's smallest useful evidence is the original prediction, the incorrect result, the changed lines and the corrected trace. A successful run alone would not explain why the change was right. Keep the input unchanged during the repair so you can attribute the changed output to your edit. The same habit works on practice misses: Fix It in the CompTIA PenTest+ prep planned for launch keeps each missed question until you answer it correctly.
What should your next short practice session produce?
Finish with three saved traces: the Python sequence, the PowerShell Boolean and the Bash status example. For each, write the supplied input, selected path, output and result after one change. You should be able to explain every line of output without appealing to an unspecified scanner or environment.
Use the 20 free PenTest+ practice questions to identify concepts that still need explanation, then give those concepts a separate block in the free PenTest+ study plan. A multiple-choice result does not replace the written trace.
When you can follow the logic, use the PenTest+ PBQ preparation guide to practice reading a task and checking the artifact you produced. Carry over one habit: state what the result proves, and stop before claiming more than the supplied evidence supports.
Trust the source
Official sources
Exam policies can change. Use these primary sources for the most current details.