Python for all: Version differences, terminal checking and PEP standards 8

онлайн тренажер по питону
Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Python: Key Aspects for the Modern Developer

Python continues to be one of the most popular and in-demand programming languages in 2025. Knowledge of key aspects that influence the quality and efficiency of development is necessary for both novice and experienced specialists. In this article, we will consider the differences between Python versions, ways to check the interpreter version, the principles of the interpreter, and PEP 8 code formatting standards.

Python 2 vs Python 3: The Evolution of the Language

Python has been actively developing for over three decades. For a long time, two main versions existed in parallel: Python 2.x and Python 3.x. Understanding the differences between them is critical, as support for Python 2 has been discontinued.

Key Differences Between Python 2 and Python 3

Feature Python 2 Python 3
Support Ended in 2020 Active
Output print "Hello" print("Hello")
Integer Division Returns an integer (1/2 = 0) Returns a float (1/2 = 0.5)
Unicode Strings Requires u"text" Strings are Unicode by default
range() Function Returns a list Returns an iterator
Compatibility Compatible only with Python 2 Compatible with Python 3 and above

Output Syntax: print

In Python 3, print is a function, making the syntax more explicit.

Python 2:

print "Hello, World!"

Python 3:

print("Hello, World!")

Number Division

In Python 2, integer division returns an integer. To get a floating-point result, explicit type casting is required.

Python 2:

print(1 / 2) # Outputs 0

Python 3:

print(1 / 2) # Outputs 0.5

Working with Strings and Unicode

In Python 3, all strings are represented in Unicode format by default. In Python 2, you had to explicitly specify Unicode strings.

Python 2:

text = u"Привет"

Python 3:

text = "Привет"

This is especially important when working with multilingual projects and processing data containing characters from different alphabets.

Iterators and Generators

In Python 3, functions that return lists (e.g., range(), map(), filter()) return iterators, which saves memory and improves performance.

Python 2:

print(range(5)) # [0, 1, 2, 3, 4]

Python 3:

print(range(5)) # range(0, 5) - iterator

Library Compatibility

After the end of support for Python 2, most modern libraries (e.g., Django, Flask, TensorFlow) are only being developed for Python 3.

Why is it important to use Python 3?

  • Active security and new feature support.
  • Compatibility with modern libraries and tools.
  • Optimized memory management and improved performance.
  • Simplified work with texts and international projects thanks to Unicode.

How to Check the Python Version in 2025

Knowing the installed Python version is necessary for:

  • Compatibility with libraries and frameworks.
  • Correct configuration of virtual environments.
  • Preventing errors related to syntax differences.

Checking the Python Version via Terminal

  1. Open the terminal (or command prompt in Windows).
  2. Execute the command:
python --version

or

python -V

Example output: Python 3.13.0

Checking the Version When Multiple Versions are Present

If multiple versions of Python are installed, specify the specific version:

python3 --version

Example output: Python 3.13.0

Checking the Version from the Interactive Console

In the Python interactive console, execute:

import sys
print(sys.version)

Example output: 3.13.0 (main, Jan 1 2025, 00:00:00) [GCC 11.3.0]

Checking Versions via py (Windows Only)

For Windows:

py -0

This command will display all installed versions of Python and their location.

Determining the Interpreter Location

  • Linux/macOS: which python
  • Windows: where python

Tools for Managing Python Versions

  • pyenv (Linux/macOS)
  • conda (cross-platform solution)
  • virtualenv and venv for virtual environments.

How the Python Interpreter Works

The Python interpreter processes code line by line.

Stages of the Interpreter's Operation

  1. Reading Source Code: Reading the .py file and checking syntax.
  2. Lexical Analysis: Breaking the code into tokens (keywords, variables, operators). Example: a = 5 + 3 -> Tokens: a, =, 5, +, 3
  3. Syntactic Analysis (Parsing): Building a syntax tree (AST) and checking the program's structure.
  4. Bytecode Generation: Converting the AST into bytecode (a .pyc file) containing instructions for the Python Virtual Machine (PVM). Example bytecode:
    LOAD_CONST 5
    LOAD_CONST 3
    BINARY_ADD
    STORE_NAME a
    
  5. Execution on the Python Virtual Machine (PVM): Interpreting and executing the bytecode.

Key Aspects of the Interpreter

  • CPython (written in C) - the main interpreter.
  • Alternatives:
    • PyPy - an accelerated version with JIT compilation.
    • Jython - for running Python code in JVM (Java).
    • IronPython - integration with .NET.
    • MicroPython - for microcontrollers.

Advantages of Working Through an Interpreter

  • Fast code checking and execution.
  • Interactive console (REPL).
  • Convenience of debugging and testing.

Disadvantages

  • Lower performance than compiled languages.
  • Dependence on the installed interpreter.

Interactive Work with REPL

Run the interpreter in the terminal with the command python.

Example:

>>> 2 + 3
5

PEP 8: Code Formatting Standards

PEP 8 is the official coding style guide for Python, defining standards for writing clean and readable code.

What is PEP?

PEP (Python Enhancement Proposal) is a document proposing improvements to the Python language. PEP 8 defines code formatting standards.

Why is PEP 8 Needed?

  • Code readability.
  • Effective teamwork.
  • Debugging simplification.
  • Project unification.

Basic PEP 8 Rules

  • Indentation: 4 spaces (not a tab).
def my_function():
    print("Hello, World!")
  • Line Length: No more than 79 characters.
  • Naming:
    • snake_case for functions and variables: user_name = "Alice"
    • CamelCase for classes: class DataProcessor: pass
  • Blank Lines:
    • One blank line between methods within a class.
    • Two blank lines between classes and functions at the module level.
  • Imports: At the beginning of the file in the following order:
    • Standard libraries.
    • Third-party libraries.
    • Local modules.
import os
import sys
import requests
from my_project import utils

Tools for Complying with PEP 8

  • flake8 - code analyzer.
  • pylint - static code analysis.
  • black - automatic code formatter.
  • autopep8 - utility for automatically bringing code to PEP 8.

Importance of Complying with PEP 8

Following PEP 8 makes the code professional, clean, and understandable, which is critical when working on large projects.

Conclusion

Understanding the differences between Python 2 and Python 3, being able to check the interpreter version, knowing the principles of the interpreter, and following PEP 8 standards will help you become a more efficient and professional Python developer. Remember that quality code is valued no less than its functionality.

News