Open up your favorite text editor or IDE (Integrated Development Environment) like Sublime Text or PyCharm. If you don’t have one yet, just use Notepad or any other basic text editor for now. 2. Type in the following code:
# Import the math module, which allows us to use mathematical functions like sqrt() and sin().
import math
# Define the main function that will run when the script is executed.
def main():
# Prompt the user for input using the built-in input() function.
num = int(input("Enter a number: "))
# Check if the number is less than 0.
if num < 0:
# If so, print an error message and exit the program.
print("Error: Number must be greater than or equal to zero.")
return
# Otherwise, calculate the square root of the input number using math.sqrt() and store it in a variable called result.
result = math.sqrt(num)
# Print out the result.
print("The square root of", num, "is:", result)
# Call the main function when the script is executed.
if __name__ == "__main__":
main()
3. Save your file as `my_script.py` and make sure it’s saved in a folder that you can easily access from the command line (e.g., on Windows, save it to C:\Users\YourName\Documents). 4. Open up a terminal or command prompt window and navigate to the directory where your script is located using `cd` followed by the path to your file:
# This script is used to navigate to a specific directory where a python script is located.
# On Linux/MacOS, the `cd` command is used to change the current working directory to the specified path.
# The path should be the location of the python script, in this case, it is "/path/to/my_script.py".
# The "$" symbol is used to indicate that this is a command to be entered in the terminal.
# On Windows, the `cd` command is used in the same way as on Linux/MacOS.
# However, the path should be the location of the python script, in this case, it is "C:\Users\YourName\Documents".
# The ">" symbol is used to indicate that this is a command to be entered in the command prompt.
5. Run your script using `python` followed by the name of your file:
# This script is used to run a python script on different operating systems.
# On Linux/MacOS, the command to run a python script is "python" followed by the name of the file.
# On Windows, the command is the same.
# To run the script, the user must type "python" followed by the name of the file.
# The following code checks the operating system and sets the appropriate command to run the python script.
if [[ "$OSTYPE" == "linux-gnu" || "$OSTYPE" == "darwin"* ]]; then # Checks if the operating system is Linux or MacOS
command="python" # Sets the command to "python"
else
command="python" # Sets the command to "python" for Windows
fi
# The following code runs the python script using the determined command.
$command my_script.py # Runs the python script using the determined command and the name of the file.
6. Follow the prompts and enter a number when asked. The program will calculate its square root using math.sqrt() and print out the result:
# Prompt user to enter a number
echo "Enter a number: "
# Read user input and store it in a variable
read num
# Calculate the square root of the input using math.sqrt()
# and store the result in a variable
result=$(echo "sqrt($num)" | bc -l)
# Print out the result with a message
echo "The square root of $num is: $result"
Congratulations, you’ve just written your very first Python program! And if you followed along with this guide, hopefully you learned some new tricks and techniques that will help you write better code in the future. But don’t take our word for it go ahead and try out these tips yourself and see how they work for you.