cs410
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Help
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # CS 410 Python Style Guide Welcome to the CS410 Python Style Guide. Our mission in this course is to help you uplevel your technical skills while mastering the art of writing clean, consistent code. Trust us, your future self and teammates will thank you! Why is this important? Clean code isn't just a nice-to-have; it's essential for effective collaboration, maintenance, and readability. This guide will walk you through industry standards and prepare you for the professional world. Make sure to read through it carefully. We’ll be keeping an eye on your coding style, and **repeated issues will lead to point deductions on your homework**. But don't worry—we're here to help you build great habits that will benefit you not just in this course, but throughout your entire coding career. # Background This style guide is built on the best practices outlined in [PEP 8 Style Guide for Python Code](https://peps.python.org/pep-0008/) and [PEP 257 Docstring Conventions](https://peps.python.org/pep-0257/), the go-to industry standards for writing readable and consistent Python code. PEP 8, created in 2001 by Guido van Rossum, Barry Warsaw, and Alyssa Coghlan, aims to standardize Python code for clarity and uniformity. PEP 257 enhances documentation through clear and consistent docstring conventions. As the PEPs keep evolving with the language, you can be sure they will stay relevant and useful. Stick to these guidelines for happier coding! # Best Practices ## Naming Conventions ### Variables and Functions - **Use descriptive names**: Give variables and functions names that clearly describe their purpose. For example: `user_name`, `calculate_total`. ### Modules - **Lowercase names**: Module names should be all lowercase, with words concatenated together. Keep them short but descriptive. For example: `mymod`. ### Classes - **UpperCamelCase**: Classes should be named using nouns that describe what the class models. Class names should use UpperCamelCase. For example: `Ocean`, `TreeHouse`. - **Exceptions**: Class names for exceptions should end with “Error”. For example: `BadDataError`. ### Constants - **CONSTANT_CASE**: Constant names should be all uppercase with words separated by underscores. For example: `MY_PYTHON_CONSTANT`. ### Everything Else - **snake_case**: Functions, fields, parameters, and variables should use snake_case. For example: `my_variable`, `exciting_new_method`. ## Formatting ### Indentation In Python, indentation is a must. Your code will not compile or run properly if you don’t have the correct indentation. It can be a little finicky, though: if you use spaces to indent in some places and tabs in another your compiler will not be happy. - **Use 4 spaces**: Indent all blocks of code with 4 spaces. Avoid using tabs. - **Block indentation**: A new block is preceded by a colon (:), and the indent level returns to the previous level when the block ends. Example: ```python! def check_adult(age): if age >= 18: print("You are officially an adult!") else: print("Not an adult yet...") ``` - **Configure your editor**: Set your editor to insert 4 spaces when the tab key is pressed. ::: spoiler **Tip: Configure VSCode to insert 4 spaces when tab is pressed.** In the bottom right of your window in VSCode, there’s a portion that will say “Spaces: \<number>” or “Tab Size: \<number>.” **Click on that and choose “Indent using spaces” followed by “4” to have the correct settings. When you’re done you should see “Spaces: 4” in the bottom corner of your code window.** ::: ### Line Wrapping - **80 characters max**: Lines of code should not exceed 80 characters. - **Operator on new line**: When wrapping lines, the operator should start the new line. For example: ```python! # This is correct long_argument_name = even_longer_argument_name \ + yet_another_even_longer_name # This is not correct long_argument_name = even_longer_argument_name + yet_another_even_longer_name # This is also not correct long_argument_name = even_longer_argument_name + yet_another_even_longer_name ``` - **Backslash for wrapping**: Use a backslash (`\`) at the end of a line to indicate continuation. Here's how it may come in handy: ```python! # This is correct now! long_argument_name = even_longer_argument_name \ + yet_another_even_longer_name # And so is this! long_argument_name = even_longer_argument_name + \ yet_another_even_longer_name ``` :::warning **Note:** When wrapping with the backslash, the wrapped line of code must be indented so that the first character of the wrapped line is in line with the beginning of the expression on the original line (in this case, the equality operator). ::: ## Commenting The general principle of commenting is to clarify / explain your code to an outside observer (or to your future self!). ### Implementation Comments - **Use `#`**: Begin all implementation comments with `#`. ```python # This is a multi-line comment # in Python! x = 5 # This is an end-of-line comment ``` ### Type Annotations - **Type annotations**: Include type annotations for all inputs and outputs. Omit output annotations for functions with no return value (e.g., functions that only print). ### Documentation Comments (Docstrings) Also known as a “docstring,” documentation comments in Python appear directly _underneath_ the declaration of each function. :::warning **Write docstrings for all functions** including helper and nested functions. A good docstring should clearly describe the function's inputs, outputs, and purpose, allowing users to understand the function without needing to look at the body. ::: - **Use triple quotes**: Place docstrings directly under the function declaration. - **Follow this format**: - One sentence summary of the function's purpose - Parameter descriptions (if any) - Return value descriptions (if any) - Exceptions thrown (if any) - Additional notes (e.g., restrictions, side effects) **Example docstring:** ```python def sum(x: float, y: float) -> float: """Sums two numbers Parameters: x -- the first number y -- the second number Returns: A number (the sum of x and y) Throws: BadInputError if x or y (or both) is not a number """ ``` ## Other Tips ### `self` Keyword - **Use `self`**: **Always** use `self` to access methods and fields within a class. It should be the first parameter of any method in the class. ```python class Food: def __init__(self, fruit: str, color: str) -> None: self.fruit = fruit self.color = color def show(self) -> None: print("fruit is", self.fruit) print("color is", self.color) ``` ### Import Statements - **Importing**: You can import specific functions or entire modules. ```python from random import randint num = randint(1, 10) import random num = random.randint(1, 10) ``` 1. **Use constants and helper functions appropriately**: Ensure they are not redundant. 2. **Return statements**: Write return statements without parentheses. ```python return value ``` 3. **if statements**: Use newlines for readability. ```python if condition1: print('condition1') elif condition2: print('condition2') else: print('condition3') ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully