Skip to content

Latest commit

 

History

History
1111 lines (875 loc) · 77.9 KB

clean_code_guidelines_(PEP8).md

File metadata and controls

1111 lines (875 loc) · 77.9 KB
<link crossorigin="anonymous" media="all" integrity="sha512-+HeWsRssemPGuloygAOs7GmFgy7YbkHjZBpS1IjHtDIae/SjCIxQJ/fuYzcnfL/ldHiTsEhC9jk40cIqTEXDiA==" rel="stylesheet" href="https://github.githubassets.com/assets/github-075cbca7db412e75878ec493711218f4.css" />
<title>Python_for_Data_Science/clean_code_guidelines_(PEP8).md at master · nj935/Python_for_Data_Science</title>
<meta property="og:image" content="https://avatars3.githubusercontent.com/u/31963247?s=400&amp;v=4" /><meta property="og:site_name" content="GitHub" /><meta property="og:type" content="object" /><meta property="og:title" content="nj935/Python_for_Data_Science" /><meta property="og:url" content="https://github.com/nj935/Python_for_Data_Science" /><meta property="og:description" content="Materials for the Spring 2019 course. Contribute to nj935/Python_for_Data_Science development by creating an account on GitHub." />
  <meta name="google-site-verification" content="KT5gs8h0wvaagLKAVWq8bbeNwnZZK1r1XQysX3xurLU">
<meta name="google-site-verification" content="ZzhVyEFwb7w3e0-uOTltm8Jsck2F5StVihD0exw2fsA">
<meta name="google-site-verification" content="GXs5KoUUkNCoaAZn7wPN-t01Pywp9M3sEjnt_3_ZWPc">

<meta name="google-analytics" content="UA-3769691-2">
  <meta name="hostname" content="github.com">
<meta name="user-login" content="nj935">

  <meta name="expected-hostname" content="github.com">
<meta name="js-proxy-site-detection-payload" content="NWIyMmM5ZDRiZjhiZjUxYjJlNzZkMDBmNTU5M2E4ODA0MmJjYjQyZDY4Njg1ZTgzNGI5ZmQwNzg0NDJkNGE1NHx7InJlbW90ZV9hZGRyZXNzIjoiMTQxLjE1Ni4xNzUuMTAxIiwicmVxdWVzdF9pZCI6IkMwRTg6Mzk2NDoyODVCMTY6NENBNkEyOjVDQTRCQUJBIiwidGltZXN0YW1wIjoxNTU0Mjk5NjAzLCJob3N0IjoiZ2l0aHViLmNvbSJ9">

<meta name="enabled-features" content="UNIVERSE_BANNER,MARKETPLACE_SOCIAL_PROOF_CUSTOMERS,MARKETPLACE_TRENDING_SOCIAL_PROOF,MARKETPLACE_UNVERIFIED_LISTINGS,MARKETPLACE_PLAN_RESTRICTION_EDITOR,NOTIFY_ON_BLOCK,RELATED_ISSUES">
  <link href="https://github.com/nj935/Python_for_Data_Science/commits/master.atom" rel="alternate" title="Recent Commits to Python_for_Data_Science:master" type="application/atom+xml">

<link rel="canonical" href="https://github.com/nj935/Python_for_Data_Science/blob/master/clean_code_guidelines_(PEP8).md" data-pjax-transient>
Skip to content
  </div>

</div>

<div class="HeaderMenu d-flex flex-justify-between flex-auto">
  <nav class="d-flex flex-items-center" aria-label="Global">
        <div class="">
          <div class="header-search mr-3 scoped-search site-scoped-search js-site-search position-relative js-jump-to"

role="combobox" aria-owns="jump-to-results" aria-label="Search or jump to" aria-haspopup="listbox" aria-expanded="false"

        <div class="Box position-absolute overflow-hidden d-none jump-to-suggestions js-jump-to-suggestions-container">
  • No suggested jump to results
        </div>
  </label>
        </div>

      <ul class="d-flex pl-2 flex-items-center text-bold list-style-none">
        <li>
          <a class="js-selected-navigation-item HeaderNavlink px-2" data-hotkey="g p" data-ga-click="Header, click, Nav menu - item:pulls context:user" aria-label="Pull requests you created" data-selected-links="/pulls /pulls/assigned /pulls/mentioned /pulls" href="/pulls">
            Pull requests

  • Issues
  • Marketplace
  • Explore
  •   <div class="d-flex">
    
        <!-- '"` --><!-- </textarea></xmp> --></option></form><form class="sr-only right-0" action="/logout" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="&#x2713;" /><input type="hidden" name="authenticity_token" value="+Gatn1dsRUFwM8jrm+4fFqZJ9UzeGzcQ3ZfP0tzZJd25LO4A/uRZhCD+98oF3t7IZP5oMjo7QymbvLMYGDIPCg==" />
          <button type="submit" class="dropdown-item dropdown-signout" data-ga-click="Header, sign out, icon:logout">
            Sign out
          </button>
    
    <div id="js-flash-container">
    
      <ul class="pagehead-actions">
    
  • <!-- '"` --><!-- </textarea></xmp> --></option></form><form data-remote="true" class="clearfix js-social-form js-social-container" action="/notifications/subscribe" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="&#x2713;" /><input type="hidden" name="authenticity_token" value="ZNitluuugzKiyZ3A+wsOtaO+91wAcUlX6rWtBXItYVIT1CeZh6e5E9LtTNJJ0t5HwgMvZaTTWZ9DMdXIZejxvw==" />      <input type="hidden" name="repository_id" value="177828798">
    
      <details class="details-reset details-overlay select-menu float-left">
        <summary class="select-menu-button float-left btn btn-sm btn-with-count" data-hydro-click="{&quot;event_type&quot;:&quot;repository.click&quot;,&quot;payload&quot;:{&quot;target&quot;:&quot;WATCH_BUTTON&quot;,&quot;repository_id&quot;:177828798,&quot;client_id&quot;:&quot;559676169.1547418249&quot;,&quot;originating_request_id&quot;:&quot;C0E8:3964:285B16:4CA6A2:5CA4BABA&quot;,&quot;originating_url&quot;:&quot;https://github.com/nj935/Python_for_Data_Science/blob/master/clean_code_guidelines_(PEP8).md&quot;,&quot;referrer&quot;:&quot;https://github.com/nj935/Python_for_Data_Science&quot;,&quot;user_id&quot;:31963247}}" data-hydro-click-hmac="8ee3b9f59c90d42b1388a27783595b07c5e09339e35fe1eb42dcff6fe70de101" data-ga-click="Repository, click Watch settings, action:blob#show">            <span data-menu-button>
                <svg class="octicon octicon-eye v-align-text-bottom" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M8.06 2C3 2 0 8 0 8s3 6 8.06 6C13 14 16 8 16 8s-3-6-7.94-6zM8 12c-2.2 0-4-1.78-4-4 0-2.2 1.8-4 4-4 2.22 0 4 1.8 4 4 0 2.22-1.78 4-4 4zm2-4c0 1.11-.89 2-2 2-1.11 0-2-.89-2-2 0-1.11.89-2 2-2 1.11 0 2 .89 2 2z"/></svg>
                Watch
            </span>
    
    Notifications
    Not watching Be notified only when participating or @mentioned. Watch
            <button type="submit" name="do" value="release_only" class="select-menu-item width-full" aria-checked="false" role="menuitemradio">
              <svg class="octicon octicon-check select-menu-item-icon" viewBox="0 0 12 16" version="1.1" width="12" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M12 5l-8 8-4-4 1.5-1.5L4 10l6.5-6.5L12 5z"/></svg>
              <div class="select-menu-item-text">
                <span class="select-menu-item-heading">Releases only</span>
                <span class="description">Be notified of new releases, and when participating or @mentioned.</span>
                <span class="hidden-select-button-text" data-menu-button-contents>
                  <svg class="octicon octicon-eye v-align-text-bottom" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M8.06 2C3 2 0 8 0 8s3 6 8.06 6C13 14 16 8 16 8s-3-6-7.94-6zM8 12c-2.2 0-4-1.78-4-4 0-2.2 1.8-4 4-4 2.22 0 4 1.8 4 4 0 2.22-1.78 4-4 4zm2-4c0 1.11-.89 2-2 2-1.11 0-2-.89-2-2 0-1.11.89-2 2-2 1.11 0 2 .89 2 2z"/></svg>
                  Unwatch releases
                </span>
              </div>
            </button>
    
            <button type="submit" name="do" value="subscribed" class="select-menu-item width-full" aria-checked="false" role="menuitemradio">
              <svg class="octicon octicon-check select-menu-item-icon" viewBox="0 0 12 16" version="1.1" width="12" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M12 5l-8 8-4-4 1.5-1.5L4 10l6.5-6.5L12 5z"/></svg>
              <div class="select-menu-item-text">
                <span class="select-menu-item-heading">Watching</span>
                <span class="description">Be notified of all conversations.</span>
                <span class="hidden-select-button-text" data-menu-button-contents>
                  <svg class="octicon octicon-eye v-align-text-bottom" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M8.06 2C3 2 0 8 0 8s3 6 8.06 6C13 14 16 8 16 8s-3-6-7.94-6zM8 12c-2.2 0-4-1.78-4-4 0-2.2 1.8-4 4-4 2.22 0 4 1.8 4 4 0 2.22-1.78 4-4 4zm2-4c0 1.11-.89 2-2 2-1.11 0-2-.89-2-2 0-1.11.89-2 2-2 1.11 0 2 .89 2 2z"/></svg>
                  Unwatch
                </span>
              </div>
            </button>
    
            <button type="submit" name="do" value="ignore" class="select-menu-item width-full" aria-checked="false" role="menuitemradio">
              <svg class="octicon octicon-check select-menu-item-icon" viewBox="0 0 12 16" version="1.1" width="12" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M12 5l-8 8-4-4 1.5-1.5L4 10l6.5-6.5L12 5z"/></svg>
              <div class="select-menu-item-text">
                <span class="select-menu-item-heading">Ignoring</span>
                <span class="description">Never be notified.</span>
                <span class="hidden-select-button-text" data-menu-button-contents>
                  <svg class="octicon octicon-mute v-align-text-bottom" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M8 2.81v10.38c0 .67-.81 1-1.28.53L3 10H1c-.55 0-1-.45-1-1V7c0-.55.45-1 1-1h2l3.72-3.72C7.19 1.81 8 2.14 8 2.81zm7.53 3.22l-1.06-1.06-1.97 1.97-1.97-1.97-1.06 1.06L11.44 8 9.47 9.97l1.06 1.06 1.97-1.97 1.97 1.97 1.06-1.06L13.56 8l1.97-1.97z"/></svg>
                  Stop ignoring
                </span>
              </div>
            </button>
    
          </div>
        </details-menu>
      </details>
        <a class="social-count js-social-count"
          href="/nj935/Python_for_Data_Science/watchers"
          aria-label="0 users are watching this repository">
          0
        </a>
    
  • Unstar 0 Star 0
  • Fork

    Fork Python_for_Data_Science

    If this dialog fails to load, you can visit the fork page directly.

    <a href="/nj935/Python_for_Data_Science/network/members" class="social-count"
       aria-label="0 users forked this repository">
      0
    </a>
    
  •   <h1 class="public ">
    

    /Python_for_Data_Science

    </div>
    
    <span itemscope itemtype="http://schema.org/ListItem" itemprop="itemListElement">
      <a itemprop="url" data-hotkey="g i" class="js-selected-navigation-item reponav-item" data-selected-links="repo_issues repo_labels repo_milestones /nj935/Python_for_Data_Science/issues" href="/nj935/Python_for_Data_Science/issues">
        <svg class="octicon octicon-issue-opened" viewBox="0 0 14 16" version="1.1" width="14" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"/></svg>
        <span itemprop="name">Issues</span>
        <span class="Counter">0</span>
        <meta itemprop="position" content="2">
    

    <a data-hotkey="g b" class="js-selected-navigation-item reponav-item" data-selected-links="repo_projects new_repo_project repo_project /nj935/Python_for_Data_Science/projects" href="/nj935/Python_for_Data_Science/projects">
      <svg class="octicon octicon-project" viewBox="0 0 15 16" version="1.1" width="15" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M10 12h3V2h-3v10zm-4-2h3V2H6v8zm-4 4h3V2H2v12zm-1 1h13V1H1v14zM14 0H1a1 1 0 0 0-1 1v14a1 1 0 0 0 1 1h13a1 1 0 0 0 1-1V1a1 1 0 0 0-1-1z"/></svg>
      Projects
      <span class="Counter" >0</span>
    
    <a class="js-selected-navigation-item reponav-item" data-hotkey="g w" data-selected-links="repo_wiki /nj935/Python_for_Data_Science/wiki" href="/nj935/Python_for_Data_Science/wiki">
      <svg class="octicon octicon-book" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M3 5h4v1H3V5zm0 3h4V7H3v1zm0 2h4V9H3v1zm11-5h-4v1h4V5zm0 2h-4v1h4V7zm0 2h-4v1h4V9zm2-6v9c0 .55-.45 1-1 1H9.5l-1 1-1-1H2c-.55 0-1-.45-1-1V3c0-.55.45-1 1-1h5.5l1 1 1-1H15c.55 0 1 .45 1 1zm-8 .5L7.5 3H2v9h6V3.5zm7-.5H9.5l-.5.5V12h6V3z"/></svg>
      Wiki
    
    Insights Settings
    <a class="d-none js-permalink-shortcut" data-hotkey="y" href="/nj935/Python_for_Data_Science/blob/56e3e5359f428e12c475cb9fd567ddaaf93c0d3f/clean_code_guidelines_(PEP8).md">Permalink</a>
    
    <!-- blob contrib key: blob_contributors:v21:db40984e816f178058d2dda30db554a6 -->
    
    
    
    <div class="d-flex flex-shrink-0 flex-items-center mb-3">
    
    title="Switch branches or tags"> <i>Branch:</i> <span class="css-truncate-target">master</span>
      <div id="blob-path" class="breadcrumb flex-auto ml-2">
        <span class="js-repo-root text-bold"><span class="js-path-segment"><a data-pjax="true" href="/nj935/Python_for_Data_Science"><span>Python_for_Data_Science</span></a></span></span><span class="separator">/</span><strong class="final-path">clean_code_guidelines_(PEP8).md</strong>
      </div>
      <div class="BtnGroup">
        <a href="/nj935/Python_for_Data_Science/find/master"
              class="js-pjax-capture-input btn btn-sm BtnGroup-item"
              data-pjax
              data-hotkey="t">
          Find file
        </a>
        <clipboard-copy for="blob-path" class="btn btn-sm BtnGroup-item">
          Copy path
        </clipboard-copy>
      </div>
    </div>
    
    <div class="Box-body d-flex flex-items-center flex-auto f6 border-bottom-0" >
      <details class="details-reset details-overlay details-overlay-dark lh-default text-gray-dark float-left mr-2" id="blob_contributors_box" >
        <summary class="btn-link" aria-haspopup="dialog">
          <span><strong>1</strong> contributor</span>
        </summary>
        <details-dialog class="Box Box--overlay d-flex flex-column anim-fade-in fast" aria-label="Users who have contributed to this file">
          <div class="Box-header">
            <button class="Box-btn-octicon btn-octicon float-right" type="button" aria-label="Close dialog" data-close-dialog>
              <svg class="octicon octicon-x" viewBox="0 0 12 16" version="1.1" width="12" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M7.48 8l3.75 3.75-1.48 1.48L6 9.48l-3.75 3.75-1.48-1.48L4.52 8 .77 4.25l1.48-1.48L6 6.52l3.75-3.75 1.48 1.48L7.48 8z"/></svg>
            </button>
            <h3 class="Box-title">
              Users who have contributed to this file
            </h3>
          </div>
              <ul class="list-style-none overflow-auto">
    <li class="Box-row">
      <a class="link-gray-dark no-underline" href="/nj935">
        <img class="avatar mr-1" alt="" src="https://avatars3.githubusercontent.com/u/31963247?s=40&amp;v=4" width="20" height="20" />
        nj935
    

        </details-dialog>
      </details>
    </div>
    
    <div class="Box mt-3 position-relative">
    
    79 lines (60 sloc) 1.51 KB
    <div class="BtnGroup">
      <a id="raw-url" class="btn btn-sm BtnGroup-item" href="/nj935/Python_for_Data_Science/raw/master/clean_code_guidelines_(PEP8).md">Raw</a>
        <a class="btn btn-sm js-update-url-with-hash BtnGroup-item" data-hotkey="b" href="/nj935/Python_for_Data_Science/blame/master/clean_code_guidelines_(PEP8).md">Blame</a>
      <a rel="nofollow" class="btn btn-sm BtnGroup-item" href="/nj935/Python_for_Data_Science/commits/master/clean_code_guidelines_(PEP8).md">History</a>
    </div>
    
    
    <div>
            <a class="btn-octicon tooltipped tooltipped-nw"
               href="x-github-client://openRepo/https://github.com/nj935/Python_for_Data_Science?branch=master&amp;filepath=clean_code_guidelines_%28PEP8%29.md"
               aria-label="Open this file in GitHub Desktop"
               data-ga-click="Repository, open with desktop, type:mac">
                <svg class="octicon octicon-device-desktop" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M15 2H1c-.55 0-1 .45-1 1v9c0 .55.45 1 1 1h5.34c-.25.61-.86 1.39-2.34 2h8c-1.48-.61-2.09-1.39-2.34-2H15c.55 0 1-.45 1-1V3c0-.55-.45-1-1-1zm0 9H1V3h14v8z"/></svg>
            </a>
    
            <!-- '"` --><!-- </textarea></xmp> --></option></form><form class="inline-form js-update-url-with-hash" action="/nj935/Python_for_Data_Science/edit/master/clean_code_guidelines_(PEP8).md" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="&#x2713;" /><input type="hidden" name="authenticity_token" value="IfHN8O5TclPOOUlQPIVfFm/waWBTHZsT/l799HOOE1hVdA0k4IQrtePA+VNpseJklaBz+gUHrm4StRThrcMryg==" />
              <button class="btn-octicon tooltipped tooltipped-nw" type="submit"
                aria-label="Edit this file" data-hotkey="e" data-disable-with>
                <svg class="octicon octicon-pencil" viewBox="0 0 14 16" version="1.1" width="14" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M0 12v3h3l8-8-3-3-8 8zm3 2H1v-2h1v1h1v1zm10.3-9.3L12 6 9 3l1.3-1.3a.996.996 0 0 1 1.41 0l1.59 1.59c.39.39.39 1.02 0 1.41z"/></svg>
              </button>
    

    Writing clean code based on PEP8 guidelines

    Clear and well-written code is important. The main set of conventions for Python, written by Guido Van Rossum, is called PEP8.

    For our course, remember the following:

    1. Maximum line length

    • Lines of code shouldn't exceed 79 characters.
    • To comply with this, break code into several lines by wrapping expressions in parentheses.

    2. Imports

    • Import libraries at the top of your notebook. Use a new line for each import.
    • Learn the conventional shortened names for common libraries (eg. import pandas as pd).
    YES:
    import sys
    import os
    
    NO: 
    import sys, os
    

    3. Declaring strings

    • Python lets you use single quotes or double quotes around strings; pick one option and stick to it.
    YES:
    string1 = 'I love Python'
    string2 = 'not R'
    
    NO:
    string1 = "I love Python"
    string2 = 'not R'
    

    4. Whitespace

    • Use spaces between operators
    • Be consistent and don't add redundant whitespace.
    YES:
    a = b + c
    spam(ham[1], {eggs: 2})
    
    NO:
    a=b+c
    spam( ham[ 1 ], { eggs: 2 } )
    

    5. Comments

    • Start comments with # and a single space.
    YES:
    x = x + 1          # Increment x
    

    6. Naming

    • Function and variable names should be lowercase, with underscores between the words as necessary to improve readability.
    • Use descriptive variable names.
    YES:
    mean_trip_time = []
    
    PROBABLY NOT:
    mtt = []
    
    </div>
    
    Go
    </main>
    
    <a aria-label="Homepage" title="GitHub" class="footer-octicon mx-lg-4" href="https://github.com">
      <svg height="24" class="octicon octicon-mark-github" viewBox="0 0 16 16" version="1.1" width="24" aria-hidden="true"><path fill-rule="evenodd" d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.013 8.013 0 0 0 16 8c0-4.42-3.58-8-8-8z"/></svg>
    
    You can’t perform that action at this time.
    <script crossorigin="anonymous" integrity="sha512-tEFti6nF1hBd4GQkgKYMs3eNp6ezhYHYiZBTDLaIhGDhFXca1JMrdgvzRCxp91tXzg3bnIn3NqA2f7EMJ672bQ==" type="application/javascript" src="https://github.githubassets.com/assets/frameworks-c3fed18a.js"></script>
    
    <script crossorigin="anonymous" async="async" integrity="sha512-RTnNZaEfRdIW5qcxvSKBrlnT+vCt8dL6JB20e8euvp288G90XIjNlq4EEJg0XqsyvkRrOevCXlqIA4tt2aoHrw==" type="application/javascript" src="https://github.githubassets.com/assets/github-bootstrap-bff57258.js"></script>
    
    You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.