If we want to start doing more complicated and repetitive tasks, we probably don't want to type all the commands into the command line every time. Instead, we can create and use scripts. A script is a python file that executes its code whenever you run it. This is perfect if you have something repetitive and involved to do!
Let's write our first script in Spyder. The script will output a computed angle to the command window. Click the New File button (top left). In the new file window, write:
import math
All scripts should start with importing any modules that the script depends on. In this case, we will be using atan() from math. Even if you have imported math into your command window, it's good practice to include this line in your script, so you don't run into errors for missing modules in the future.
Next, write:
angle = math.atan(6 / 4)
hypotenuse = 6 / math.sin(angle)
All trigonometry in math uses angles measured in radians. This means that atan() returns radians, and sin() takes radians as input. The degrees() function in math converts an angle in radians to degrees. A radians() function is available in math to do the inverse.
Add one more line:
print('The angle is: ', math.degrees(angle))
Let's pause for a second and look at the print() function a little bit more. print() will print the parameters provided to it in order. So, by saying 'The angle is: ' followed by math.degrees(angle) will print that first string followed by the number. You can call print() with any number of objects to print! This allows for some clever formatting.
Save the script with CTRL-S or the save button. Take note of where it was saved, because you will need to remember that to open it in the future!
Notice that it is saved as a .py file. This is the Python file extension, and it tells the computer that Python code is inside. Then, we can run it by pressing the green triangle button. Check the command window, and you should see something like this:
Let's write our first script in Spyder. The script will output a computed angle to the command window. Click the New File button (top left). In the new file window, write:
import math
All scripts should start with importing any modules that the script depends on. In this case, we will be using atan() from math. Even if you have imported math into your command window, it's good practice to include this line in your script, so you don't run into errors for missing modules in the future.
Next, write:
angle = math.atan(6 / 4)
hypotenuse = 6 / math.sin(angle)
All trigonometry in math uses angles measured in radians. This means that atan() returns radians, and sin() takes radians as input. The degrees() function in math converts an angle in radians to degrees. A radians() function is available in math to do the inverse.
Add one more line:
print('The angle is: ', math.degrees(angle))
Let's pause for a second and look at the print() function a little bit more. print() will print the parameters provided to it in order. So, by saying 'The angle is: ' followed by math.degrees(angle) will print that first string followed by the number. You can call print() with any number of objects to print! This allows for some clever formatting.
Save the script with CTRL-S or the save button. Take note of where it was saved, because you will need to remember that to open it in the future!
Notice that it is saved as a .py file. This is the Python file extension, and it tells the computer that Python code is inside. Then, we can run it by pressing the green triangle button. Check the command window, and you should see something like this:
Congratulations! You have written your first Python script!