A Python script is a plain text file containing Python code that is executed from top to bottom. Scripts are used to automate tasks, run programs, or perform computations.
.py
file extension.main()
function with a guard if __name__ == "__main__":
to allow importing without running code immediately.print("This script prints a greeting message.")
def greet(name):
print(f"Hello, {name}!")
greet("John")
def main():
print("Script is running as main program")
if __name__ == "__main__":
main()
To run a script, use the command line:
python script_name.py
The if __name__ == "__main__":
guard ensures that certain code only runs when the script is executed directly, not when imported as a module. This improves code reuse and modularity.