PYTHON

Writing and executing Python scripts


Python Script:

A Python script is a file containing Python code that is meant to be executed directly by the Python interpreter. These scripts can perform a variety of tasks, such as automating repetitive tasks, processing data, or even developing web applications

Creating and Running a Python Script

Here’s how to create and run a simple Python script:

Create a Python Script:

  • Open a text editor (e.g., Notepad, VS Code, Sublime Text).
  • Write some Python code. For example
# This is a simple Python script
print("Hello, World!")
  • Save the file with a .py extension, for example, hello.py.

 

Basic Syntax:

Comments

Comments are used to explain code and make it more readable. They are ignored by the Python interpreter.

  • Single-line comments start with #:
# This is a comment
print("Hello, World!")  # This is an inline comment

 

  • Multi-line comments use triple quotes ''' or """:
"""
This is a
multi-line comment
"""

 

Indentation:

Indentation is crucial in Python. It indicates a block of code. Consistent use of spaces or tabs is required (PEP 8 recommends using 4 spaces per indentation level).

if x > 0:
   print("x is positive")
   if x > 10:
       print("x is greater than 10")
else:
   print("x is non-positive")

 

 


PYTHON