AWS re/Start Lab · Programación en Python

Challenge Lab: Python Scripting Exercise

This challenge lab uses a Linux Host to create and test a small Python script. The goal is to calculate the prime numbers from 1 to 250, print them on screen, and save the same result into a text file for verification.

Lab Summary

Connected to the Linux Host with PuTTY following the standard process from Lab 225, wrote a Python script in script.py to calculate prime numbers, executed it with python3, and confirmed that the same values were written into results.txt.

SSH and Setup

Connected to the Linux Host and prepared the terminal session for editing and testing a Python file.

Script Logic

Created a loop that checks divisibility and stores every prime number found between 1 and 250.

Validation

Ran the script and then opened results.txt to verify that the file contained the expected output.

Step-by-Step Walkthrough

The lab was short, but it covered the full scripting cycle: write, execute, and verify.

01

Connect to the Linux Host and create the script

  • Connected to the EC2 instance via PuTTY following the process described in Lab 225.
  • Opened nano script.py and wrote a Python program that iterates from 2 to 250, tests each number for divisibility, and stores the prime values in a list.
  • Added a second block with open("results.txt", "w") so the same prime numbers were saved line by line into a text file.
The script keeps the logic simple: each candidate number is checked against smaller divisors, and only the values that never divide evenly are added to the result list.
02

Run the script and verify the saved output

  • Executed the program with python3 script.py to print the prime numbers on screen.
  • Used cat results.txt to verify that the text file contained the same values generated by the script.
  • Confirmed that the script and the output file were both stored in the working directory for future reference.

Command Reference

Main commands and Python instructions used during the exercise.

cmd

nano script.py

Creates or opens the Python file in the nano editor.

cmd

python3 script.py

Runs the script with Python 3 and prints the prime numbers.

cmd

cat results.txt

Displays the contents of the output file to verify the saved results.

py

for i in range(2, 251)

Iterates through the number range used in the exercise.

py

open("results.txt", "w")

Creates or overwrites the text file where the final output is stored.

Key Learnings

What Was Actually Learned

A small Python script can combine calculation logic and file output in the same run.
Using python3 explicitly avoids confusion on systems where both Python 2 and Python 3 are installed.
A quick validation step with cat results.txt confirms that the file output matches the terminal output.

Technical Conclusion

This lab worked as a compact scripting exercise. Even though the problem was simple, it covered the exact pattern that matters in real automation work: write logic, execute it, and confirm the output independently.

The result is also a good reminder that a script is more useful when it leaves a traceable artifact. In this case, results.txt becomes the evidence that the program did exactly what it was supposed to do.