A Python script is a file containing Python code that can be executed as a program. Writing and running scripts is fundamental for automating tasks and building applications.
Writing a Python Script
- Create a new file with a
.py
extension (e.g.,script.py
). - Write your Python code in the file.
- Save the file.
Example Script
# script.py
print('Hello, World!')
Executing the Script
Open a terminal or command prompt and navigate to the directory containing your script. Run the script using:
python script.py
Making the Script Executable (Unix/Linux)
Add a shebang line at the top:
#!/usr/bin/env python3
Make the script executable:
chmod +x script.py
./script.py
Conclusion
Writing and running Python scripts is a straightforward process that enables you to automate tasks and develop applications.