Python Cheatsheet

repository·master·Indexed 26 days ago

https://github.com/labex-labs/python-cheatsheet

A comprehensive, hands-on Python learning course by LabEx covering basic syntax, data structures, OOP, and advanced concepts like decorators and context managers. Includes guides on Python 3.14 beta features such as Template Strings (t-strings), deferred type annotations, the interpreters module, and Zstandard compression, as well as detailed tutorials on list, set, and dict comprehensions.

Tokens
77.2K
Snippets
329
Records
516
Agent score
89%

What's inside labex-labs-python-cheatsheet

  1. Overview of Python Cheatsheet Course

    master

    The Python Cheatsheet is a comprehensive course designed to help developers master Python programming through practical labs. It covers a wide range of topics from fundamental basics to advanced concepts including:

    • Basics: Syntax, built-in functions, and control flow.
    • Data Structures: Lists, tuples, dictionaries, sets, and comprehensions.
    • String Manipulation: Formatting and regular expressions.
    • File & Data Handling: File/directory path manipulation, reading/writing files, and working with JSON and YAML.
    • Advanced Python: Exception handling, debugging, *args and **kwargs, decorators, and context managers.
    • Object-Oriented Programming (OOP): OOP basics and Data Classes.
    • Development Workflow: Using VS Code, packaging, main functions, and virtual environments.
  2. Understand the difference between Tuples and Lists

    master

    The fundamental difference between tuples and lists in Python is mutability:

    • Lists are mutable: You can modify, add, or remove elements after the list has been created.
    • Tuples are immutable: Once created, their contents cannot be changed. Because of this, tuples are generally more memory-efficient than lists.

    Both types can contain any type of data.

  3. Use the traditional setup.py approach for Python packaging

    master

    The setup.py method is the traditional way to define project metadata. To be functional, a setup.py file must include three required fields: name (must be unique on PyPI), version (tracks releases), and packages (describes the location of Python source code).

    You can install a package using this method by running:

    python setup.py install
  4. Access all keys and values in a shelf

    master

    The shelf object supports keys() and values() methods similar to a dictionary. Because these methods return list-like views rather than true lists, wrap them in list() if you need a standard list object.

    import shelve
    
    # Access all keys and values in shelf
    with shelve.open('mydata') as shelf_file:
        print(list(shelf_file.keys()))
        print(list(shelf_file.values()))
  5. Copy files and directories with shutil

    master

    Use the shutil module for file operations:

    • shutil.copy(src, dst): Copies a single file to a destination.
    • shutil.copytree(src, dst): Recursively copies an entire directory tree, including all subdirectories and files.
    import shutil
    
    # Copy a single file
    shutil.copy('/tmp/spam.txt', '/tmp/delicious')
    
    # Copy an entire directory tree
    shutil.copytree('/tmp/bacon', '/tmp/bacon_backup')
  6. Manage ordered collections with `list`

    master

    Lists (list) are mutable, ordered collections that can hold mixed data types. Use them when you need to build collections item-by-item or access items by position.

    Common Operations:

    • append(item): Adds an item to the end.
    • insert(index, item): Inserts an item at a specific position.
    • extend(iterable): Appends multiple items from an iterable.
    • remove(value): Removes the first occurrence of a value.
    • pop(index): Removes and returns the item at the given index (defaults to the last item).
    • len(list): Returns the number of items.