Skip to main content

Python Syntax

Python Syntax

Python is a beginner-friendly programming language known for its simple and readable syntax. In this tutorial, we will explore how to execute Python code, understand indentation rules, and learn best practices to avoid errors.


01. Python Execute Syntax

Python syntax can be executed in two primary ways:

  1. Using Python shell (command line mode).
  2. By writing Python scripts in a file with a .py extension and executing them in the command line.

1.1 Running Python in the Interactive Shell

The interactive shell allows you to execute Python commands directly. Open your terminal (Command Prompt, PowerShell, or Terminal on macOS/Linux) and type python or python3 to enter interactive mode.

Example: Running Python interactively

>>> print("Hello, World!")
Hello, World!

Execution:

1.2 Executing a Python Script

For larger programs, you should write Python code in a file and execute it. Follow these steps:

  • Create a new file with a .py extension (e.g., firstprogram.py).
  • Write your Python code inside the file.
  • Run the script using the command line.

Example file: firstprogram.py

Example: firstprogram.py

# This is a simple Python script
print("Hello, World!")

To execute this script, navigate to the file’s location and run:

C:\Users\Admin>python firstprogram.py

Output:

Hello, World!

Execution:


02. Python Indentation

  • Python uses indentation to define blocks of code.
  • Unlike other languages that use curly braces {}, Python relies on consistent indentation.

2.1 Correct Indentation

Example: Correct Indentation

if 10 > 5:
    print("10 is greater than 5")  # Indented correctly

2.2 Incorrect Indentation

If indentation is missing or inconsistent, Python will raise an IndentationError.

Example: Incorrect Indentation

if 10 > 5:
print("10 is greater than 5")  # Incorrect indentation

Output:

IndentationError: expected an indented block

2.3 Standard Indentation Rules

The standard convention is to use 4 spaces per indentation level:

Example: Indentation Recommended

# Example of proper indentation
if True:
    print("Indented using 4 spaces")  # Correct (recommended)

if True:
 print("Using 1 space")  # Valid but not recommended

2.4 Mismatched Indentation

If indentation is inconsistent within a block, Python raises an error.

Example: Mismatched Indentation

if 25 > 20:
    print("This line is properly indented")
        print("This line is incorrectly indented")  # Error

Output:

IndentationError: unindent does not match any outer indentation level

2.5 Using Indentation in Loops

Loops also require indentation for their code blocks.

Example: Using Indentation in Loops

for i in range(3):
    print("Loop iteration:", i)  # Correctly indented inside the loop

Output:

Loop iteration: 0
Loop iteration: 1
Loop iteration: 2

Conclusion

Understanding Python execution and indentation is fundamental for writing error-free programs. Always remember:

  • Use the interactive shell for quick testing and debugging.
  • Save scripts with .py and run them using the command line.
  • Follow indentation rules to structure your code properly.
  • Use 4 spaces per indentation level for readability and consistency.

With these concepts in mind, you’re ready to start writing Python programs efficiently!



Comments