Creating and Running a Simple Python Program: A Step-by-Step Guide – P3

Setting Up the Environment

  1. Create a New Folder: Begin by creating a new folder for your project. For instance, if you’re working on Exercise 3 of a Python course, you might name the folder ex_02_03. This organization helps in managing your files and keeping track of different exercises or projects.
  2. Open a Text Editor: Use a text editor like Atom for writing your code. It’s a user-friendly interface for coding and offers various features to enhance your programming experience.

Writing Your First Python Script

  1. Create a New File: In your text editor, start a new file. As a start, you could write a simple print statement, like print("PY4E"), to test your setup.
  2. Save the File Correctly: Choose ‘Save As’ and ensure the file is saved in the correct folder. It’s advisable to avoid spaces in file names for compatibility with all operating systems. Use underscores instead. Make sure to save the file with a .py extension, indicating it’s a Python script.

Running the Script

  1. Open the Terminal: Access the terminal on your computer to run the Python script.
  2. Navigate to the Correct Directory: Use the cd command to navigate to the directory where your Python file is saved. For instance, cd Desktop/py4e/ex_02_03 would change your current directory to the ex_02_03 folder inside the py4e directory on your desktop.
  3. List the Files: You can use ls or ls -l to list the files in the directory, confirming your script is there.
  4. Run the Python Script: Execute your Python script by typing python3 in the terminal. This step runs your script and displays any output or errors in the terminal.

Writing a Simple Input-Output Program

  1. Write Input Statements: Write input statements for your program, such as xh = input("Enter Hours: ") and xr = input("Enter Rate: "). This allows the user to input values when the program runs.
  2. Process the Input: Perform calculations or processing based on the input. For instance, calculate pay by multiplying hours and rate (xp = xh * xr).
  3. Print the Output: Display the result using a print statement, like print("Pay", xp). Remember, a comma in the print statement automatically adds a space in the output.

Debugging and Testing Your Program

  1. Understanding Tracebacks: If you encounter an error, Python will provide a traceback. This is not an error on your part, but Python’s way of saying it doesn’t understand your instructions. For example, you cannot multiply string types directly.
  2. Type Conversion: Convert string inputs to the appropriate type, like floats, for numerical calculations. Use float(xh) and float(xr) to convert the input strings to floating-point numbers.
  3. Save and Re-run the Script: After making changes, remember to save your script. Run it again in the terminal to test the changes.

Finalizing and Grading Your Assignment

  1. Run Test Cases: Run your program with different inputs to ensure it works as expected. For example, use inputs like 10 for hours and 5 for rate to validate the output.
  2. Submit for Grading: If your course uses an autograder, copy your final code and submit it for grading. Make sure to adhere to the specific requirements of the assignment, like output format, to avoid mismatches in grading.
  3. Adjust as Needed: If there’s a mismatch or an error in the autograder, make the necessary adjustments to your code. Pay attention to output formatting and precision of calculations.