Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Managing Multiple Python Versions and Core Language Concepts

Tech 1

Managing Python Versions on Windows

To handle multiple Python installations on Windows, use pyenv-win. Install it via command line:

pip install pyenv-win --target %USERPROFILE%\.pyenv

This tool simplifies version switching and environment management.

Foundational Python Concepts

Comments

Python supports single-line and multi-line comments.

Single-Line Comments

Begin with #; everything after is ignored.

# This is a comment
result = 5 + 3  # Calculation with inline comment

Multi-Line Comments

Use triple quotes (''' or """) for documentation strings, often serving as comments.

"""
This is a docstring.
It can span multiple lines.
Commonly used for function descriptions.
"""

def compute_total(value_a, value_b):
    """
    Adds two numbers.

    Parameters:
        value_a: First numeric input.
        value_b: Second numeric input.

    Returns:
        Sum of the inputs.
    """
    return value_a + value_b

Print Function

print() outputs various data types:

  • Numbers
  • Strings
  • Expresssions with operators

Output destinations include displays and files. Control newlines with the end parameter.

print(42)  # Integer
print("text")  # String
print(7 * 6)  # Expression
print("line1", end=" ")  # No newline
print("line2")

Escape Sequences and Raw Strings

Escape sequences handle special characters:

  • \\: Backslash
  • \': Single quote
  • \": Double quote
  • \n: Newline
  • \t: Tab

Use raw strings to disable escape processing by prefixing with r or R.

print("Path: C:\\Users\\file.txt")  # Escaped backslashes
print(r"Path: C:\Users\file.txt")  # Raw string
# Avoid trailing backslash in raw strings
print(r"example\path\\")  # Double backslash at end

Character Encoding

Convert between characters and binary representations.

print(ord('A'))  # Unicode code point
print(chr(0b1000001))  # Binary to character

Keywords and Identifiers

Keywords are reserved; view them with:

import keyword
print(keyword.kwlist)

Identifiers name variables, functions, etc. Rules:

  • Use letters, digits, underscores
  • Cannot start with a digit
  • Case-sensitive
  • Avoid keywords

Data Types

Basic Types

  • Integer (int)
  • Floating-point (float)
  • Boolean (bool)
  • String (str)

Container Types

  • List (list): Mutable sequence
  • Tuple (tuple): Immutable sequence
  • Set (set): Unordered unique collection
  • Dictionary (dict): Key-value mapping

Other Built-in Types

  • NoneType: Represents null
  • Bytes (bytes): Immutable binary data
  • Bytearray (bytearray): Mutable binary data
  • Range (range): Number sequence

Custom and Advanced Types

  • Classes: User-defined types
  • Frozenset: Immutable set
  • Complex (complex): Complex numbers
  • Generators and iterators

Sequences

Sequences include:

  • String: Immutable Unicode
  • List: Mutable
  • Tuple: Immutable
  • Range: Immutable numbers
  • Bytes: Immutable binary
  • Bytearray: Mutable binary

System and Package Management

Locate Python Installations

Find installed Python versions:

where python

Pip Commands

Manage Dependencies

Export and install from requirements file:

pip freeze > dependencies.txt
pip install -r dependencies.txt

Download Wheel Files

pip download package_name --only-binary :all:

Uninstall Packages

pip uninstall package_name

Configure Environment Variables

Set temporary Python path:

export PYTHONPATH=$PYTHONPATH:/path/to/package.whl

Verify installation:

python -c "import package; print(package.__version__)"

Package Source Configuration

Global Source

Edit pip.ini in C:\Users\[Username]\pip:

[global]
index-url = https://mirrors.aliyun.com/pypi/simple
[install]
trusted-host = mirrors.aliyun.com

Temporary Source

Specify source during installation:

pip install requests -i https://pypi.tuna.tsinghua.edu.cn/simple

Machine Learning Setup

NumPy and Jupyter

Install essential packages:

pip install jupyter numpy

Start Jupyter:

jupyter notebook

If pyzmq causes compatibility issues, rienstall a specific version:

pip uninstall pyzmq
pip install pyzmq==19.0.2

For Jupyter extensions in notebook 6.x, refer to documentation for plugin installation steps.

Bulk Package Removal

Uninstall all pip packages except core utilities:

pip freeze | grep -vE "^-e|^pip|^setuptools|^wheel" | xargs pip uninstall -y

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.