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.
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
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.
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
Open the Terminal: Access the terminal on your computer to run the Python script.
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.
List the Files: You can use ls or ls -l to list the files in the directory, confirming your script is there.
Run the Python Script: Execute your Python script by typing python3 ex_02_03.py in the terminal. This step runs your script and displays any output or errors in the terminal.
Writing a Simple Input-Output Program
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.
Process the Input: Perform calculations or processing based on the input. For instance, calculate pay by multiplying hours and rate (xp = xh * xr).
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
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.
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.
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
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.
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.
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.