{"id":164825,"date":"2019-11-25T10:00:21","date_gmt":"2019-11-25T04:30:21","guid":{"rendered":"https:\/\/www.digitalvidya.com\/blog\/?p=164825"},"modified":"2022-04-28T17:33:21","modified_gmt":"2022-04-28T12:03:21","slug":"python-code-pep8","status":"publish","type":"post","link":"https:\/\/www.digitalvidya.com\/blog\/python-code-pep8\/","title":{"rendered":"Beginners Guide to Write Beautiful Python Code With PEP 8"},"content":{"rendered":"<p dir=\"ltr\">PEP is a document that contains best practices and python code writing guidelines. The primary focus of the documentation is to improve the consistency and readability of the Python code, written by Guido Van Rossum, Nick Coghlan, and Barry Warsaw.<\/p>\n<p dir=\"ltr\">PEP or Python Enhancement Proposal is used to define new features such as Python&#8217;s style and design. But we need a PEP, is the question?<\/p>\n<p dir=\"ltr\">PEP 8&#8217;s basic goal is to improve the readability of the code. As said by Guido Van Rossum himself, code is read more frequently than written. This is because it will only be written once, no matter how long you have spent writing the script.<\/p>\n<p dir=\"ltr\">It&#8217;s not the same for reading the script. A code can be read by the writer or some other developer multiple times. Therefore, you have to be able to understand why it works and what it does every time you look at that code.<\/p>\n<p dir=\"ltr\">If you follow the guidelines listed in PEP 8, you would have named the variables correctly and added white space, you can follow the logic in the code.<\/p>\n<p dir=\"ltr\">To make it easier to read, you should also put comments on your script. Also, after PEP 8 becomes more critical when you&#8217;re looking for a job as a designer.<\/p>\n<p dir=\"ltr\"><div class=\"cta-form\"><div class=\"content\"><div class=\"left-block\"><div class=\"section-title\">Want to Know the Path to Become a <strong> Data Science Expert?<\/strong><\/div><div class=\"desc\"><p>Download Detailed Brochure and Get Complimentary access to Live Online Demo Class with Industry Expert.<\/p>\n<\/div><div class=\"time\"> Date: August 1 (Sat) | 11 AM - 12 PM (IST)<\/div><\/div><div class=\"right-block\"><script>\nvar gform;gform||(document.addEventListener(\"gform_main_scripts_loaded\",function(){gform.scriptsLoaded=!0}),document.addEventListener(\"gform\/theme\/scripts_loaded\",function(){gform.themeScriptsLoaded=!0}),window.addEventListener(\"DOMContentLoaded\",function(){gform.domLoaded=!0}),gform={domLoaded:!1,scriptsLoaded:!1,themeScriptsLoaded:!1,isFormEditor:()=>\"function\"==typeof InitializeEditor,callIfLoaded:function(o){return!(!gform.domLoaded||!gform.scriptsLoaded||!gform.themeScriptsLoaded&&!gform.isFormEditor()||(gform.isFormEditor()&&console.warn(\"The use of gform.initializeOnLoaded() is deprecated in the form editor context and will be removed in Gravity Forms 3.1.\"),o(),0))},initializeOnLoaded:function(o){gform.callIfLoaded(o)||(document.addEventListener(\"gform_main_scripts_loaded\",()=>{gform.scriptsLoaded=!0,gform.callIfLoaded(o)}),document.addEventListener(\"gform\/theme\/scripts_loaded\",()=>{gform.themeScriptsLoaded=!0,gform.callIfLoaded(o)}),window.addEventListener(\"DOMContentLoaded\",()=>{gform.domLoaded=!0,gform.callIfLoaded(o)}))},hooks:{action:{},filter:{}},addAction:function(o,r,e,t){gform.addHook(\"action\",o,r,e,t)},addFilter:function(o,r,e,t){gform.addHook(\"filter\",o,r,e,t)},doAction:function(o){gform.doHook(\"action\",o,arguments)},applyFilters:function(o){return gform.doHook(\"filter\",o,arguments)},removeAction:function(o,r){gform.removeHook(\"action\",o,r)},removeFilter:function(o,r,e){gform.removeHook(\"filter\",o,r,e)},addHook:function(o,r,e,t,n){null==gform.hooks[o][r]&&(gform.hooks[o][r]=[]);var d=gform.hooks[o][r];null==n&&(n=r+\"_\"+d.length),gform.hooks[o][r].push({tag:n,callable:e,priority:t=null==t?10:t})},doHook:function(r,o,e){var t;if(e=Array.prototype.slice.call(e,1),null!=gform.hooks[r][o]&&((o=gform.hooks[r][o]).sort(function(o,r){return o.priority-r.priority}),o.forEach(function(o){\"function\"!=typeof(t=o.callable)&&(t=window[t]),\"action\"==r?t.apply(null,e):e[0]=t.apply(null,e)})),\"filter\"==r)return e[0]},removeHook:function(o,r,t,n){var e;null!=gform.hooks[o][r]&&(e=(e=gform.hooks[o][r]).filter(function(o,r,e){return!!(null!=n&&n!=o.tag||null!=t&&t!=o.priority)}),gform.hooks[o][r]=e)}});\n<\/script>\n\n                <div class='gf_browser_unknown gform_wrapper gravity-theme gform-theme--no-framework dv-form_wrapper' data-form-theme='gravity-theme' data-form-index='0' id='gform_wrapper_287' ><div id='gf_287' class='gform_anchor' tabindex='-1'><\/div><form method='post' enctype='multipart\/form-data' target='gform_ajax_frame_287' id='gform_287' class='dv-form' action='\/blog\/wp-json\/wp\/v2\/posts\/164825#gf_287' data-formid='287' novalidate>\n                        <div class='gform-body gform_body'><div id='gform_fields_287' class='gform_fields top_label form_sublabel_below description_below validation_below'><div id=\"field_287_28\" class=\"gfield gfield--type-text gfield_contains_required field_sublabel_below gfield--no-description field_description_below hidden_label field_validation_below gfield_visibility_visible\"  ><label class='gfield_label gform-field-label' for='input_287_28'>Name<span class=\"gfield_required\"><span class=\"gfield_required gfield_required_text\">(Required)<\/span><\/span><\/label><div class='ginput_container ginput_container_text'><input name='input_28' id='input_287_28' type='text' value='' class='large'    placeholder='Name *' aria-required=\"true\" aria-invalid=\"false\"   \/><\/div><\/div><div id=\"field_287_2\" class=\"gfield gfield--type-email gfield_contains_required field_sublabel_below gfield--no-description field_description_below hidden_label field_validation_below gfield_visibility_visible\"  ><label class='gfield_label gform-field-label' for='input_287_2'>Email<span class=\"gfield_required\"><span class=\"gfield_required gfield_required_text\">(Required)<\/span><\/span><\/label><div class='ginput_container ginput_container_email'>\n                            <input name='input_2' id='input_287_2' type='email' value='' class='large'   placeholder='Email *' aria-required=\"true\" aria-invalid=\"false\"  \/>\n                        <\/div><\/div><div id=\"field_287_3\" class=\"gfield gfield--type-phone gfield_contains_required field_sublabel_below gfield--no-description field_description_below hidden_label field_validation_below gfield_visibility_visible\"  ><label class='gfield_label gform-field-label' for='input_287_3'>Phone<span class=\"gfield_required\"><span class=\"gfield_required gfield_required_text\">(Required)<\/span><\/span><\/label><div class='ginput_container ginput_container_phone'><input name='input_3' id='input_287_3' type='tel' value='' class='large'  placeholder='Phone *' aria-required=\"true\" aria-invalid=\"false\"   \/><\/div><\/div><div id=\"field_287_34\" class=\"gfield gfield--type-hidden gform_hidden field_sublabel_below gfield--no-description field_description_below field_validation_below gfield_visibility_visible\"  ><div class='ginput_container ginput_container_text'><input name='input_34' id='input_287_34' type='hidden' class='gform_hidden'  aria-invalid=\"false\" value='fmc' \/><\/div><\/div><div id=\"field_287_9\" class=\"gfield gfield--type-hidden gform_hidden field_sublabel_below gfield--no-description field_description_below field_validation_below gfield_visibility_visible\"  ><div class='ginput_container ginput_container_text'><input name='input_9' id='input_287_9' type='hidden' class='gform_hidden'  aria-invalid=\"false\" value='' \/><\/div><\/div><div id=\"field_287_10\" class=\"gfield gfield--type-hidden gform_hidden field_sublabel_below gfield--no-description field_description_below field_validation_below gfield_visibility_visible\"  ><div class='ginput_container ginput_container_text'><input name='input_10' id='input_287_10' type='hidden' class='gform_hidden'  aria-invalid=\"false\" value='' \/><\/div><\/div><div id=\"field_287_11\" class=\"gfield gfield--type-hidden gform_hidden field_sublabel_below gfield--no-description field_description_below field_validation_below gfield_visibility_visible\"  ><div class='ginput_container ginput_container_text'><input name='input_11' id='input_287_11' type='hidden' class='gform_hidden'  aria-invalid=\"false\" value='' \/><\/div><\/div><div id=\"field_287_12\" class=\"gfield gfield--type-hidden gform_hidden field_sublabel_below gfield--no-description field_description_below field_validation_below gfield_visibility_visible\"  ><div class='ginput_container ginput_container_text'><input name='input_12' id='input_287_12' type='hidden' class='gform_hidden'  aria-invalid=\"false\" value='https:\/\/www.digitalvidya.com\/blog\/wp-json\/wp\/v2\/posts\/164825' \/><\/div><\/div><div id=\"field_287_13\" class=\"gfield gfield--type-hidden gform_hidden field_sublabel_below gfield--no-description field_description_below field_validation_below gfield_visibility_visible\"  ><div class='ginput_container ginput_container_text'><input name='input_13' id='input_287_13' type='hidden' class='gform_hidden'  aria-invalid=\"false\" value='' \/><\/div><\/div><div id=\"field_287_14\" class=\"gfield gfield--type-hidden gform_hidden field_sublabel_below gfield--no-description field_description_below field_validation_below gfield_visibility_visible\"  ><div class='ginput_container ginput_container_text'><input name='input_14' id='input_287_14' type='hidden' class='gform_hidden'  aria-invalid=\"false\" value='' \/><\/div><\/div><div id=\"field_287_15\" class=\"gfield gfield--type-hidden gform_hidden field_sublabel_below gfield--no-description field_description_below field_validation_below gfield_visibility_visible\"  ><div class='ginput_container ginput_container_text'><input name='input_15' id='input_287_15' type='hidden' class='gform_hidden'  aria-invalid=\"false\" value='' \/><\/div><\/div><div id=\"field_287_17\" class=\"gfield gfield--type-hidden gform_hidden field_sublabel_below gfield--no-description field_description_below field_validation_below gfield_visibility_visible\"  ><div class='ginput_container ginput_container_text'><input name='input_17' id='input_287_17' type='hidden' class='gform_hidden'  aria-invalid=\"false\" value='' \/><\/div><\/div><div id=\"field_287_18\" class=\"gfield gfield--type-hidden gform_hidden field_sublabel_below gfield--no-description field_description_below field_validation_below gfield_visibility_visible\"  ><div class='ginput_container ginput_container_text'><input name='input_18' id='input_287_18' type='hidden' class='gform_hidden'  aria-invalid=\"false\" value='' \/><\/div><\/div><div id=\"field_287_19\" class=\"gfield gfield--type-hidden gform_hidden field_sublabel_below gfield--no-description field_description_below field_validation_below gfield_visibility_visible\"  ><div class='ginput_container ginput_container_text'><input name='input_19' id='input_287_19' type='hidden' class='gform_hidden'  aria-invalid=\"false\" value='' \/><\/div><\/div><div id=\"field_287_20\" class=\"gfield gfield--type-hidden gform_hidden field_sublabel_below gfield--no-description field_description_below field_validation_below gfield_visibility_visible\"  ><div class='ginput_container ginput_container_text'><input name='input_20' id='input_287_20' type='hidden' class='gform_hidden'  aria-invalid=\"false\" value='{dmo_day}' \/><\/div><\/div><div id=\"field_287_25\" class=\"gfield gfield--type-hidden gform_hidden field_sublabel_below gfield--no-description field_description_below field_validation_below gfield_visibility_visible\"  ><div class='ginput_container ginput_container_text'><input name='input_25' id='input_287_25' type='hidden' class='gform_hidden'  aria-invalid=\"false\" value='{dmo_date}' \/><\/div><\/div><div id=\"field_287_24\" class=\"gfield gfield--type-hidden gform_hidden field_sublabel_below gfield--no-description field_description_below field_validation_below gfield_visibility_visible\"  ><div class='ginput_container ginput_container_text'><input name='input_24' id='input_287_24' type='hidden' class='gform_hidden'  aria-invalid=\"false\" value='{dmo_time}' \/><\/div><\/div><div id=\"field_287_35\" class=\"gfield gfield--type-hidden gfield--width-full gform_hidden field_sublabel_below gfield--no-description field_description_below field_validation_below gfield_visibility_visible\"  ><div class='ginput_container ginput_container_text'><input name='input_35' id='input_287_35' type='hidden' class='gform_hidden'  aria-invalid=\"false\" value='{dmo_time}' \/><\/div><\/div><div id=\"field_287_36\" class=\"gfield gfield--type-hidden gfield--width-full gform_hidden field_sublabel_below gfield--no-description field_description_below field_validation_below gfield_visibility_visible\"  ><div class='ginput_container ginput_container_text'><input name='input_36' id='input_287_36' type='hidden' class='gform_hidden'  aria-invalid=\"false\" value='' \/><\/div><\/div><div id=\"field_287_37\" class=\"gfield gfield--type-hidden gfield--width-full gform_hidden field_sublabel_below gfield--no-description field_description_below field_validation_below gfield_visibility_visible\"  ><div class='ginput_container ginput_container_text'><input name='input_37' id='input_287_37' type='hidden' class='gform_hidden'  aria-invalid=\"false\" value='' \/><\/div><\/div><div id=\"field_287_22\" class=\"gfield gfield--type-hidden gform_hidden field_sublabel_below gfield--no-description field_description_below field_validation_below gfield_visibility_visible\"  ><div class='ginput_container ginput_container_text'><input name='input_22' id='input_287_22' type='hidden' class='gform_hidden'  aria-invalid=\"false\" value='' \/><\/div><\/div><div id=\"field_287_38\" class=\"gfield gfield--type-honeypot gform_validation_container field_sublabel_below gfield--has-description field_description_below field_validation_below gfield_visibility_visible\"  ><label class='gfield_label gform-field-label' for='input_287_38'>Email<\/label><div class='ginput_container'><input name='input_38' id='input_287_38' type='text' value='' autocomplete='new-password'\/><\/div><div class='gfield_description' id='gfield_description_287_38'>This field is for validation purposes and should be left unchanged.<\/div><\/div><\/div><\/div>\n        <div class='gform-footer gform_footer top_label'> <input type='submit' id='gform_submit_button_287' class='gform_button button' onclick='gform.submission.handleButtonClick(this);' data-submission-type='submit' value='Register Now'  \/> <input type='hidden' name='gform_ajax' value='form_id=287&amp;title=&amp;description=&amp;tabindex=0&amp;theme=gravity-theme&amp;styles=[]&amp;hash=0c9c234943f312d5985375593e68ff15' \/>\n            <input type='hidden' class='gform_hidden' name='gform_submission_method' data-js='gform_submission_method_287' value='iframe' \/>\n            <input type='hidden' class='gform_hidden' name='gform_theme' data-js='gform_theme_287' id='gform_theme_287' value='gravity-theme' \/>\n            <input type='hidden' class='gform_hidden' name='gform_style_settings' data-js='gform_style_settings_287' id='gform_style_settings_287' value='[]' \/>\n            <input type='hidden' class='gform_hidden' name='is_submit_287' value='1' \/>\n            <input type='hidden' class='gform_hidden' name='gform_submit' value='287' \/>\n            \n            <input type='hidden' class='gform_hidden' name='gform_unique_id' value='' \/>\n            <input type='hidden' class='gform_hidden' name='state_287' value='WyJbXSIsIjc5YTc5ZmI0NzJmZjc1YWM4NDI0ZmE2ZmFkNDQwNjAxIl0=' \/>\n            <input type='hidden' autocomplete='off' class='gform_hidden' name='gform_target_page_number_287' id='gform_target_page_number_287' value='0' \/>\n            <input type='hidden' autocomplete='off' class='gform_hidden' name='gform_source_page_number_287' id='gform_source_page_number_287' value='1' \/>\n            <input type='hidden' name='gform_field_values' value='' \/>\n            \n        <\/div>\n                        <p style=\"display: none !important;\" class=\"akismet-fields-container\" data-prefix=\"ak_\"><label>&#916;<textarea name=\"ak_hp_textarea\" cols=\"45\" rows=\"8\" maxlength=\"100\"><\/textarea><\/label><input type=\"hidden\" id=\"ak_js_1\" name=\"ak_js\" value=\"4\"\/><script>\ndocument.getElementById( \"ak_js_1\" ).setAttribute( \"value\", ( new Date() ).getTime() );\n<\/script>\n<\/p><\/form>\n                        <\/div>\n\t\t                <iframe style='display:none;width:0px;height:0px;' src='about:blank' name='gform_ajax_frame_287' id='gform_ajax_frame_287' title='This iframe contains the logic required to handle Ajax powered Gravity Forms.'><\/iframe>\n\t\t                <script>\ngform.initializeOnLoaded( function() {gformInitSpinner( 287, 'https:\/\/www.digitalvidya.com\/blog\/wp-content\/plugins\/gravityforms\/images\/spinner.svg', true );jQuery('#gform_ajax_frame_287').on('load',function(){var contents = jQuery(this).contents().find('*').html();var is_postback = contents.indexOf('GF_AJAX_POSTBACK') >= 0;if(!is_postback){return;}var form_content = jQuery(this).contents().find('#gform_wrapper_287');var is_confirmation = jQuery(this).contents().find('#gform_confirmation_wrapper_287').length > 0;var is_redirect = contents.indexOf('gformRedirect(){') >= 0;var is_form = form_content.length > 0 && ! is_redirect && ! is_confirmation;var mt = parseInt(jQuery('html').css('margin-top'), 10) + parseInt(jQuery('body').css('margin-top'), 10) + 100;if(is_form){jQuery('#gform_wrapper_287').html(form_content.html());if(form_content.hasClass('gform_validation_error')){jQuery('#gform_wrapper_287').addClass('gform_validation_error');} else {jQuery('#gform_wrapper_287').removeClass('gform_validation_error');}setTimeout( function() { \/* delay the scroll by 50 milliseconds to fix a bug in chrome *\/ jQuery(document).scrollTop(jQuery('#gform_wrapper_287').offset().top - mt); }, 50 );if(window['gformInitDatepicker']) {gformInitDatepicker();}if(window['gformInitPriceFields']) {gformInitPriceFields();}var current_page = jQuery('#gform_source_page_number_287').val();gformInitSpinner( 287, 'https:\/\/www.digitalvidya.com\/blog\/wp-content\/plugins\/gravityforms\/images\/spinner.svg', true );jQuery(document).trigger('gform_page_loaded', [287, current_page]);window['gf_submitting_287'] = false;}else if(!is_redirect){var confirmation_content = jQuery(this).contents().find('.GF_AJAX_POSTBACK').html();if(!confirmation_content){confirmation_content = contents;}jQuery('#gform_wrapper_287').replaceWith(confirmation_content);jQuery(document).scrollTop(jQuery('#gf_287').offset().top - mt);jQuery(document).trigger('gform_confirmation_loaded', [287]);window['gf_submitting_287'] = false;wp.a11y.speak(jQuery('#gform_confirmation_message_287').text());}else{jQuery('#gform_287').append(contents);if(window['gformRedirect']) {gformRedirect();}}jQuery(document).trigger(\"gform_pre_post_render\", [{ formId: \"287\", currentPage: \"current_page\", abort: function() { this.preventDefault(); } }]);        if (event && event.defaultPrevented) {                return;        }        const gformWrapperDiv = document.getElementById( \"gform_wrapper_287\" );        if ( gformWrapperDiv ) {            const visibilitySpan = document.createElement( \"span\" );            visibilitySpan.id = \"gform_visibility_test_287\";            gformWrapperDiv.insertAdjacentElement( \"afterend\", visibilitySpan );        }        const visibilityTestDiv = document.getElementById( \"gform_visibility_test_287\" );        let postRenderFired = false;        function triggerPostRender() {            if ( postRenderFired ) {                return;            }            postRenderFired = true;            gform.core.triggerPostRenderEvents( 287, current_page );            if ( visibilityTestDiv ) {                visibilityTestDiv.parentNode.removeChild( visibilityTestDiv );            }        }        function debounce( func, wait, immediate ) {            var timeout;            return function() {                var context = this, args = arguments;                var later = function() {                    timeout = null;                    if ( !immediate ) func.apply( context, args );                };                var callNow = immediate && !timeout;                clearTimeout( timeout );                timeout = setTimeout( later, wait );                if ( callNow ) func.apply( context, args );            };        }        const debouncedTriggerPostRender = debounce( function() {            triggerPostRender();        }, 200 );        if ( visibilityTestDiv && visibilityTestDiv.offsetParent === null ) {            const observer = new MutationObserver( ( mutations ) => {                mutations.forEach( ( mutation ) => {                    if ( mutation.type === 'attributes' && visibilityTestDiv.offsetParent !== null ) {                        debouncedTriggerPostRender();                        observer.disconnect();                    }                });            });            observer.observe( document.body, {                attributes: true,                childList: false,                subtree: true,                attributeFilter: [ 'style', 'class' ],            });        } else {            triggerPostRender();        }    } );} );\n<\/script>\n<\/div><\/div><\/div><\/p>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">Let\u2019s explore in detail all the features of the PEP 8 documentation that makes code easier to read:<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"naming-conventions\"><\/span><strong>Naming Conventions<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">When you are coding, there are several things that you have to give a name to like variables, classes, functions, packages, etc. So, to save time and energy for the future, you need to select names that make sense. Meaning that you should be able to figure out what is in the variable by just seeing its name. Using inappropriate names will only increase your problems during debugging. <\/span><\/p>\n<figure id=\"attachment_165543\" aria-describedby=\"caption-attachment-165543\" style=\"width: 535px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.digitalvidya.com\/blog\/wp-content\/uploads\/2019\/09\/ami-2015-python-basics-8-638-300x225.jpg\" alt=\"Naming Conventions\" class=\"wp-image-165543\" width=\"535\" height=\"401\" title=\"\" srcset=\"https:\/\/www.digitalvidya.com\/blog\/wp-content\/uploads\/2019\/09\/ami-2015-python-basics-8-638-300x225.jpg 300w, https:\/\/www.digitalvidya.com\/blog\/wp-content\/uploads\/2019\/09\/ami-2015-python-basics-8-638-150x113.jpg 150w, https:\/\/www.digitalvidya.com\/blog\/wp-content\/uploads\/2019\/09\/ami-2015-python-basics-8-638-600x450.jpg 600w, https:\/\/www.digitalvidya.com\/blog\/wp-content\/uploads\/2019\/09\/ami-2015-python-basics-8-638-220x165.jpg 220w, https:\/\/www.digitalvidya.com\/blog\/wp-content\/uploads\/2019\/09\/ami-2015-python-basics-8-638-595x447.jpg 595w, https:\/\/www.digitalvidya.com\/blog\/wp-content\/uploads\/2019\/09\/ami-2015-python-basics-8-638-510x382.jpg 510w, https:\/\/www.digitalvidya.com\/blog\/wp-content\/uploads\/2019\/09\/ami-2015-python-basics-8-638-480x360.jpg 480w, https:\/\/www.digitalvidya.com\/blog\/wp-content\/uploads\/2019\/09\/ami-2015-python-basics-8-638.jpg 638w\" sizes=\"(max-width: 535px) 100vw, 535px\" \/><figcaption id=\"caption-attachment-165543\" class=\"wp-caption-text\">Naming conventions source &#8211; slideshare<\/figcaption><\/figure>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">Here is the list of the common naming styles that you should use while coding in Python:<\/span><\/p>\n<p><span style=\"font-weight: 400\"><strong>Variable<\/strong> \u2013 a single letter, a word or multiple words separated by an underscore, all in lowercase. For example var, the_variable.<\/span><\/p>\n<p><span style=\"font-weight: 400\"><strong>Function<\/strong> \u2013 Use words or multiple words separated by underscores, all in lowercase. For example func, the_function.<\/span><\/p>\n<p><span style=\"font-weight: 400\"><strong>Constant<\/strong> \u2013 Use a single letter, word, or multiple words separated by underscores, all in uppercase. For example, CONST, THE_CONSTANT.<\/span><\/p>\n<p><span style=\"font-weight: 400\"><strong>Method<\/strong> \u2013 Use a single word or multiple words separated by underscores, all in lowercase. For example, method, the_method.<\/span><\/p>\n<p><span style=\"font-weight: 400\"><strong>Class<\/strong> \u2013 A single word or multiple words without underscores separating them. The first letter should be capitalized. This format f word is known as camel case. For example, Model, MyClass.<\/span><\/p>\n<p><span style=\"font-weight: 400\"><strong>Module<\/strong> \u2013 A short word or multiple words separated by underscores, all in lowercase. For example module_example.py.<\/span><\/p>\n<p><span style=\"font-weight: 400\"><strong>Package<\/strong> \u2013 A short word or words not separated by underscore, all in lowercase. For example packageexample.<\/span><\/p>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">Now when it comes to naming conventions, just selecting the right format is not enough. You also have to select the right name. Here are a few tips that will help you name effectively:<\/span><\/p>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">Before you give a name to your variable, function, method, or class, you need to make sure that it makes your code readable. What you need to do is think of descriptive names that clearly show what the object represents.<\/span><\/p>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">Let\u2019s start with variables. It is a very common mistake among beginners to use a single-letter name for variable like x or y. But x doesn\u2019t give an idea of what it represents. The issue with that is depicted in the following example:<\/span><\/p>\n<pre><span style=\"font-weight: 400\">&gt;&gt;&gt;<\/span>\r\n\r\n<span style=\"font-weight: 400\">&gt;&gt;&gt; # Not recommended<\/span>\r\n\r\n<span style=\"font-weight: 400\">&gt;&gt;&gt; <\/span><span style=\"font-weight: 400\">x<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">'John Smith'<\/span>\r\n\r\n<span style=\"font-weight: 400\">&gt;&gt;&gt; <\/span><span style=\"font-weight: 400\">y,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">z<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">x<\/span><span style=\"font-weight: 400\">.<\/span><span style=\"font-weight: 400\">split()<\/span>\r\n\r\n<span style=\"font-weight: 400\">&gt;&gt;&gt; <\/span><span style=\"font-weight: 400\">print<\/span><span style=\"font-weight: 400\">(z,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">y,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">sep<\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\">', '<\/span><span style=\"font-weight: 400\">)<\/span>\r\n\r\n<span style=\"font-weight: 400\">'Smith, John'<\/span><\/pre>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">Now, this will only work when you the exact idea of what x, y, and z is. This will become much more confusing when you are working on complicated projects where there are thousands of variables or you are working with other developers. Here is an example with a clear choice of names for variables:<\/span><\/p>\n<pre><span style=\"font-weight: 400\">&gt;&gt;&gt;<\/span>\r\n\r\n<span style=\"font-weight: 400\">&gt;&gt;&gt; # Recommended<\/span>\r\n\r\n<span style=\"font-weight: 400\">&gt;&gt;&gt; <\/span><span style=\"font-weight: 400\">name<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">'John Smith'<\/span>\r\n\r\n<span style=\"font-weight: 400\">&gt;&gt;&gt; <\/span><span style=\"font-weight: 400\">first_name,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">last_name<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">name<\/span><span style=\"font-weight: 400\">.<\/span><span style=\"font-weight: 400\">split()<\/span>\r\n\r\n<span style=\"font-weight: 400\">&gt;&gt;&gt; <\/span><span style=\"font-weight: 400\">print<\/span><span style=\"font-weight: 400\">(last_name,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">first_name,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">sep<\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\">', '<\/span><span style=\"font-weight: 400\">)<\/span>\r\n\r\n<span style=\"font-weight: 400\">'Smith, John'<\/span><\/pre>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">This will increase the amount of time you spend on typing, but the overall advantage it provides by offering readability makes up for it. Also, you must avoid using abbreviations while naming objects in your code. Here is an example where db() is a function that takes x, a single argument, and doubles it:<\/span><\/p>\n<pre><span style=\"font-weight: 400\"># Not recommended<\/span>\r\n\r\n<span style=\"font-weight: 400\">def<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">db(x):<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">return<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">x<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">*<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">2<\/span><\/pre>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">You might think that using db as an abbreviation for double is a sensible choice. But if you come back to this code after a few days, you might forget how you abbreviated it making the code difficult to understand. Now, if you use the following example, you will be able to get a clearer idea of what a function does even if you check out the code in a couple of weeks or months:<\/span><\/p>\n<pre><span style=\"font-weight: 400\"># Recommended<\/span>\r\n\r\n<span style=\"font-weight: 400\">def<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">multiply_by_two(x):<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">return<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">x<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">*<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">2<\/span><\/pre>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">The same thing can be applied to all the other objects and data types involved in Python. You must use descriptive names.<\/span><\/p>\n<h3><span class=\"ez-toc-section\" id=\"code-layout\"><\/span><b>Code Layout<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">Another major aspect of code\u2019s readability is code layout. Here you will learn about adding vertical whitespace for improving readability of the code.\u00a0<\/span><\/p>\n<p dir=\"ltr\">\n<h3><span class=\"ez-toc-section\" id=\"blank-lines\"><\/span><b>Blank Lines<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">Blank lines or vertical whitespace plays an important role for improving the code\u2019s readability. If your code is bunched up, it can get overwhelming making it hard to read. Also, if you add too much whitespace, it will make the code look sparse and the reader will have to scroll down unnecessarily. <\/span><\/p>\n<figure id=\"attachment_165544\" aria-describedby=\"caption-attachment-165544\" style=\"width: 519px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.digitalvidya.com\/blog\/wp-content\/uploads\/2019\/09\/python-programming-essentials-m31-pep-8-7-638-300x225.jpg\" alt=\"Black Lines\" class=\"wp-image-165544\" width=\"519\" height=\"389\" title=\"\" srcset=\"https:\/\/www.digitalvidya.com\/blog\/wp-content\/uploads\/2019\/09\/python-programming-essentials-m31-pep-8-7-638-300x225.jpg 300w, https:\/\/www.digitalvidya.com\/blog\/wp-content\/uploads\/2019\/09\/python-programming-essentials-m31-pep-8-7-638-150x113.jpg 150w, https:\/\/www.digitalvidya.com\/blog\/wp-content\/uploads\/2019\/09\/python-programming-essentials-m31-pep-8-7-638-600x450.jpg 600w, https:\/\/www.digitalvidya.com\/blog\/wp-content\/uploads\/2019\/09\/python-programming-essentials-m31-pep-8-7-638-220x165.jpg 220w, https:\/\/www.digitalvidya.com\/blog\/wp-content\/uploads\/2019\/09\/python-programming-essentials-m31-pep-8-7-638-595x447.jpg 595w, https:\/\/www.digitalvidya.com\/blog\/wp-content\/uploads\/2019\/09\/python-programming-essentials-m31-pep-8-7-638-510x382.jpg 510w, https:\/\/www.digitalvidya.com\/blog\/wp-content\/uploads\/2019\/09\/python-programming-essentials-m31-pep-8-7-638-480x360.jpg 480w, https:\/\/www.digitalvidya.com\/blog\/wp-content\/uploads\/2019\/09\/python-programming-essentials-m31-pep-8-7-638.jpg 638w\" sizes=\"(max-width: 519px) 100vw, 519px\" \/><figcaption id=\"caption-attachment-165544\" class=\"wp-caption-text\">Black lines source &#8211; slideshare<\/figcaption><\/figure>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">Here are three guidelines that will help you understand how to use blank lines or vertical whitespace properly:<\/span><b><\/b><\/p>\n<h3><span class=\"ez-toc-section\" id=\"using-two-blank-lines-for-classes-and-top-level-functions\"><\/span><b>Using two blank lines for classes and top-level functions<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">It is important that you self-contained the top-level classes and functions and handle their functionality separately. Vertical space around them can help in clearing their boundaries. Take a look at this example:<\/span><\/p>\n<pre><span style=\"font-weight: 400\">class<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">MyFirstClass:<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">pass<\/span>\r\n\r\n<span style=\"font-weight: 400\">class<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">MySecondClass:<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">pass<\/span>\r\n\r\n<span style=\"font-weight: 400\">def<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">top_level_function():<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">return<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">None<\/span><b><\/b><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"using-a-single-blank-line-for-surrounding-method-definitions-present-inside-classes\"><\/span><b>Using a single blank line for surrounding method definitions present inside classes<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">All the functions present in the class are in relation with one another. So, you need to leave just a single line between them:<\/span><\/p>\n<pre><span style=\"font-weight: 400\">class<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">MyClass:<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">def<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">first_method(<\/span><span style=\"font-weight: 400\">self<\/span><span style=\"font-weight: 400\">):<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">return<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">None<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">def<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">second_method(<\/span><span style=\"font-weight: 400\">self<\/span><span style=\"font-weight: 400\">):<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">return<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">None<\/span><b><\/b><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"using-blank-lines-inside-functions-for-clearing-the-steps\"><\/span><b>Using blank lines inside functions for clearing the steps<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">A complicated function might have several steps involved before reaching the return statement. For helping the readers better understand the function\u2019s logic, you need to leave a blank line after every step. Here is an example where a function is used for calculating the list\u2019s variance. There is a blank line between the two steps of the functions and before the return statement:<\/span><\/p>\n<pre><span style=\"font-weight: 400\">def<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">calculate_variance(number_list):<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">sum_list<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">0<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">for<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">number<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">in<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">number_list:<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">sum_list<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">sum_list<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">+<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">number<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">mean<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">sum_list<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">\/<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">len<\/span><span style=\"font-weight: 400\">(number_list)<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">sum_squares<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">0<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">for<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">number<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">in<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">number_list:<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">sum_squares<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">sum_squares<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">+<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">number<\/span><span style=\"font-weight: 400\">**<\/span><span style=\"font-weight: 400\">2<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">mean_squares<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">sum_squares<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">\/<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">len<\/span><span style=\"font-weight: 400\">(number_list)<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">return<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">mean_squares<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">-<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">mean<\/span><span style=\"font-weight: 400\">**<\/span><span style=\"font-weight: 400\">2<\/span><\/pre>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">Blank lines or vertical whitespace can help the readers understand how and where the code is splitting into different sections and how one section is related to other.<\/span><\/p>\n<h3><span class=\"ez-toc-section\" id=\"maximum-line-length-and-line-breaking\"><\/span><b>Maximum Line Length and Line Breaking<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">According to PEP 8, the limit for a line is 79 characters. This allows you to avoid line wrapping and have multiple files open next to each other. However, it is not always possible to keep the statements under 79 characters. PEP 8 contains ways that allow the statements to run for multiple lines. If parentheses, braces, or brackets are used in the code, line continuation will be assumed by Python. Take a look at the below example:<\/span><\/p>\n<pre><span style=\"font-weight: 400\">def<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">function(arg_one,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">arg_two,<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">arg_three,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">arg_four):<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">return<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">arg_one<\/span>\r\n\r\n<span style=\"font-weight: 400\">If you cannot get implied continuation, you can break lines by using backslashes:<\/span>\r\n\r\n<span style=\"font-weight: 400\">from<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">mypkg<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">import<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">example1,<\/span><span style=\"font-weight: 400\"> \\<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">example2,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">example3<\/span><\/pre>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">If implied continuation is not working for you and you have use line breaking, you need to make sure that you break the line before the binary operators. Here is an example of breaking the line before the binary operator:<\/span><\/p>\n<pre><span style=\"font-weight: 400\"># Recommended<\/span>\r\n\r\n<span style=\"font-weight: 400\">total<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">(first_variable<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">+<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">second_variable<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">-<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">third_variable)<\/span><\/pre>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">It is very clear to see which variable is added or subtracted. Here is another example where the line is broke after the binary operator:<\/span><\/p>\n<pre><span style=\"font-weight: 400\"># Not Recommended<\/span>\r\n\r\n<span style=\"font-weight: 400\">total<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">(first_variable<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">+<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">second_variable<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">-<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">third_variable)<\/span><\/pre>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">It is difficult to see which variable is added or subtracted.<\/span><\/p>\n<h3><span class=\"ez-toc-section\" id=\"indentation\"><\/span><b>Indentation<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">Also known as leading whitespace, Indentation plays an important role making the code readable. The indentation shows how different statements in the code are grouped together. Consider this:<\/span><\/p>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">x<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">3<\/span><\/p>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">if<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">x<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">&gt;<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">5<\/span><span style=\"font-weight: 400\">:<\/span><\/p>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">print<\/span><span style=\"font-weight: 400\">(<\/span><span style=\"font-weight: 400\">&#8216;x is larger than 5&#8217;<\/span><span style=\"font-weight: 400\">)<\/span><\/p>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">The print statement is indented which shows that it should be executed only if the if condition is true. Indentation also shows what code belongs to which class and what code should be executed when a function is called. Here are the PEP 8\u2019s key indentation rules:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Use spaces instead of tabs<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">4 consecutive spaces for indentation<\/span><\/li>\n<\/ul>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">If you are using both spaces and tabs for indentation, you will have errors. To check for consistency you have to run the code with a \u2013t flag. If there is an inconsistency with spaces and tabs, issue warnings will be given by the interpreter:<\/span><\/p>\n<pre><span style=\"font-weight: 400\">$<\/span><span style=\"font-weight: 400\"> python2 -t code.py<\/span>\r\n\r\n<span style=\"font-weight: 400\">code.py: inconsistent use of tabs and spaces in indentation<\/span>\r\n\r\n<span style=\"font-weight: 400\">If you want to know exactly where the inconsistency is, you need to use the \u2013tt flag that issues errors, not warnings:<\/span>\r\n\r\n<span style=\"font-weight: 400\">$<\/span><span style=\"font-weight: 400\"> python2 -tt code.py<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0File \"code.py\", line 3<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0print(i, j)<\/span><\/pre>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0^<\/span><\/p>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">TabError: inconsistent use of tabs and spaces in indentation<\/span><\/p>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">If you are working on Python3, all the issues regarding inconsistencies in spaces and tabs are automatically issues:<\/span><\/p>\n<pre><span style=\"font-weight: 400\">$<\/span><span style=\"font-weight: 400\"> python3 code.py<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0File \"code.py\", line 3<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0print(i, j)<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0^<\/span><\/pre>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">TabError: inconsistent use of tabs and spaces in indentation<\/span><\/p>\n<h3><span class=\"ez-toc-section\" id=\"indentation-following-line-breaks\"><\/span><b>Indentation Following Line Breaks<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">You have to use indentation when you are breaking lines for keeping the statements under 79 characters. This will help the reader in distinguishing between a single line of code and two lines of code. There are two indentation styles that you can use:<\/span><\/p>\n<pre><span style=\"font-weight: 400\">Aligning the block with opening delimiter<\/span>\r\n\r\n<span style=\"font-weight: 400\">def<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">function(arg_one,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">arg_two,<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">arg_three,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">arg_four):<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">return<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">arg_one<\/span><\/pre>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">You need 4 spaces for aligning the code with the opening delimiter. Now, the if statement has a nested code block, indentation can be a little trick. Take a look at the following example:<\/span><\/p>\n<pre><span style=\"font-weight: 400\">x<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">5<\/span>\r\n\r\n<span style=\"font-weight: 400\">if<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">(x<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">&gt;<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">3<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">and<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">x<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">&lt;<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">10<\/span><span style=\"font-weight: 400\">):<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">print<\/span><span style=\"font-weight: 400\">(x)<\/span><\/pre>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">For solving this, PEP 8 offers two alternatives:<\/span><\/p>\n<p><span style=\"font-weight: 400\">After the final condition, add a comment. This will separate it from the nested code due to the option in syntax highlighting available in most editors:<\/span><\/p>\n<pre><span style=\"font-weight: 400\">x<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">5<\/span>\r\n\r\n<span style=\"font-weight: 400\">if<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">(x<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">&gt;<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">3<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">and<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">x<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">&lt;<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">10<\/span><span style=\"font-weight: 400\">):<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\"># Both conditions satisfied<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">print<\/span><span style=\"font-weight: 400\">(x)<\/span><\/pre>\n<p><span style=\"font-weight: 400\">On the line continuation, add an extra indentation:<\/span><\/p>\n<pre><span style=\"font-weight: 400\">x<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">5<\/span>\r\n\r\n<span style=\"font-weight: 400\">if<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">(x<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">&gt;<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">3<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">and<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">x<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">&lt;<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">10<\/span><span style=\"font-weight: 400\">):<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">print<\/span><span style=\"font-weight: 400\">(x)<\/span><\/pre>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">Hanging Indent is another style of indentation. In this, every line of the block is indented, except the first. In this, there must not be any arguments on the first line. Here is an example:<\/span><\/p>\n<pre><span style=\"font-weight: 400\">var<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">function(<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">arg_one,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">arg_two,<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">arg_three,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">arg_four)<\/span>\r\n\r\n<span style=\"font-weight: 400\"># Not Recommended<\/span>\r\n\r\n<span style=\"font-weight: 400\">var<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">function(arg_one,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">arg_two,<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">arg_three,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">arg_four)<\/span><\/pre>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">When you are working on hanging indent and want to distinguish between the continued line inside the function, you need to add an extra indentation. In the following example, the indentation level of the code and the continued lines is the same which makes it difficult to read:<\/span><\/p>\n<pre><span style=\"font-weight: 400\"># Not Recommended<\/span>\r\n\r\n<span style=\"font-weight: 400\">def<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">function(<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">arg_one,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">arg_two,<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">arg_three,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">arg_four):<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">return<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">arg_one<\/span><\/pre>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">Now, if you use double indentation for the continuous lines it will help you distinguish between the function\u2019s body and arguments:<\/span><\/p>\n<pre><span style=\"font-weight: 400\">def<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">function(<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">arg_one,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">arg_two,<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">arg_three,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">arg_four):<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">return<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">arg_one<\/span><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"where-to-put-the-closing-brace\"><\/span><b>Where to Put the Closing Brace<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">When you are working with line continuations, it is easy to forget about the closing parenthesis, braces, or brackets. There are two options provided by PEP 8 for the closing brace of the line continuations:<\/span><\/p>\n<p><span style=\"font-weight: 400\">Use the first non-whitespace character for lining up the closing brace with the previous line:<\/span><\/p>\n<pre><span style=\"font-weight: 400\">list_of_numbers<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">[<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">1<\/span><span style=\"font-weight: 400\">,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">2<\/span><span style=\"font-weight: 400\">,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">3<\/span><span style=\"font-weight: 400\">,<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">4<\/span><span style=\"font-weight: 400\">,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">5<\/span><span style=\"font-weight: 400\">,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">6<\/span><span style=\"font-weight: 400\">,<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">7<\/span><span style=\"font-weight: 400\">,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">8<\/span><span style=\"font-weight: 400\">,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">9<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">]<\/span><\/pre>\n<p><span style=\"font-weight: 400\">Use the first character of the line for lining up the closing brace.<\/span><\/p>\n<pre><span style=\"font-weight: 400\">list_of_numbers<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">[<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">1<\/span><span style=\"font-weight: 400\">,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">2<\/span><span style=\"font-weight: 400\">,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">3<\/span><span style=\"font-weight: 400\">,<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">4<\/span><span style=\"font-weight: 400\">,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">5<\/span><span style=\"font-weight: 400\">,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">6<\/span><span style=\"font-weight: 400\">,<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">7<\/span><span style=\"font-weight: 400\">,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">8<\/span><span style=\"font-weight: 400\">,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">9<\/span>\r\n\r\n<span style=\"font-weight: 400\">]<\/span><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"comments\"><\/span><b>Comments<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">Once you have written the code, you need to add a comment to the document. It is important because it helps the future you and any other collaborators to understand your code. When you are adding comments, you need to remember the following key points:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Using complete sentences with first word starting with an uppercase.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Limiting the comment\u2019s length to 72 characters.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Updating the comments when you make changes to the code.<\/span><\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"block-comments\"><\/span><b>Block Comments<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">For documenting a small section of code, you should use block comments. They are especially beneficial when you perform a single action by writing multiple lines of code like updating an entry of database or importing data from a file. Here are a few rules for writing block comments according to the PEP 8 documentation:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Using a # followed by a space for starting each line.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Using a single # for separating paragraphs by a line.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Indenting the block comments the same as the code.<\/span><\/li>\n<\/ul>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">Take a look at the following example where a block comment is used to explain the for loop:<\/span><\/p>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">for<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">i<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">in<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">range<\/span><span style=\"font-weight: 400\">(<\/span><span style=\"font-weight: 400\">0<\/span><span style=\"font-weight: 400\">,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">10<\/span><span style=\"font-weight: 400\">):<\/span><\/p>\n<pre><span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\"># Loop over i ten times and print out the value of i, followed by a<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\"># new line character<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">print<\/span><span style=\"font-weight: 400\">(i,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">'\\n'<\/span><span style=\"font-weight: 400\">)<\/span><\/pre>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">If the code is complicated, you might have to enter more than one paragraph in the block comment:<\/span><\/p>\n<pre><span style=\"font-weight: 400\">def<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">quadratic(a,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">b,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">c,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">x):<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\"># Calculate the solution to a quadratic equation using the quadratic<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\"># formula.<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">#<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\"># There are always two solutions to a quadratic equation, x_1 and x_2.<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">x_1<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">(<\/span><span style=\"font-weight: 400\">-<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">b<\/span><span style=\"font-weight: 400\">+<\/span><span style=\"font-weight: 400\">(b<\/span><span style=\"font-weight: 400\">**<\/span><span style=\"font-weight: 400\">2<\/span><span style=\"font-weight: 400\">-<\/span><span style=\"font-weight: 400\">4<\/span><span style=\"font-weight: 400\">*<\/span><span style=\"font-weight: 400\">a<\/span><span style=\"font-weight: 400\">*<\/span><span style=\"font-weight: 400\">c)<\/span><span style=\"font-weight: 400\">**<\/span><span style=\"font-weight: 400\">(<\/span><span style=\"font-weight: 400\">1<\/span><span style=\"font-weight: 400\">\/<\/span><span style=\"font-weight: 400\">2<\/span><span style=\"font-weight: 400\">))<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">\/<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">(<\/span><span style=\"font-weight: 400\">2<\/span><span style=\"font-weight: 400\">*<\/span><span style=\"font-weight: 400\">a)<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">x_2<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">(<\/span><span style=\"font-weight: 400\">-<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">b<\/span><span style=\"font-weight: 400\">-<\/span><span style=\"font-weight: 400\">(b<\/span><span style=\"font-weight: 400\">**<\/span><span style=\"font-weight: 400\">2<\/span><span style=\"font-weight: 400\">-<\/span><span style=\"font-weight: 400\">4<\/span><span style=\"font-weight: 400\">*<\/span><span style=\"font-weight: 400\">a<\/span><span style=\"font-weight: 400\">*<\/span><span style=\"font-weight: 400\">c)<\/span><span style=\"font-weight: 400\">**<\/span><span style=\"font-weight: 400\">(<\/span><span style=\"font-weight: 400\">1<\/span><span style=\"font-weight: 400\">\/<\/span><span style=\"font-weight: 400\">2<\/span><span style=\"font-weight: 400\">))<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">\/<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">(<\/span><span style=\"font-weight: 400\">2<\/span><span style=\"font-weight: 400\">*<\/span><span style=\"font-weight: 400\">a)<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">return<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">x_1,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">x_2<\/span><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"inline-comments\"><\/span><b>Inline Comments<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">These types of comments are used for explaining a single statement is a code. Here are the guidelines laid down in PEP 8 for inline comments:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">The inline comments must be used sparingly.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">They must be written on the same line as the line of code they are referring to.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Use more than 2 spaces for separating the inline comments from the statement.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Use a # and a single space for starting the inline comments.<\/span><\/li>\n<\/ol>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">Here is an example of an inline comment.<\/span><\/p>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">x<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">5<\/span><span style=\"font-weight: 400\">\u00a0 <\/span><span style=\"font-weight: 400\"># This is an inline comment<\/span><\/p>\n<p dir=\"ltr\">\n<h3><span class=\"ez-toc-section\" id=\"documentation-strings\"><\/span><b>Documentation Strings<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">Enclosed in single or double quotation marks, documentation strings are present on the first line of any function, method, class, or module. They are used for explaining and documenting a block of code. Here are the rules for applying document strings to the code:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">The correct way to use quotation on document string is\u00a0&#8220;&#8221;&#8221;This is a docstring&#8221;&#8221;&#8221;.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">They must be mentioned for all public functions, classes, modules, and methods.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Add\u00a0&#8220;&#8221;&#8221;\u00a0to the end of a multi line docstring.<\/span><\/li>\n<\/ul>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">Here is an example that will help you understand the usage of documentation strings better:<\/span><\/p>\n<pre><span style=\"font-weight: 400\">def<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">quadratic(a,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">b,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">c,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">x):<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">\"\"\"Solve quadratic equation via the quadratic formula.<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0A quadratic equation has the following form:<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0ax**2 + bx + c = 0<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0There are always two solutions to a quadratic equation: x_1 &amp; x_2.<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0\"\"\"<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">x_1<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">(<\/span><span style=\"font-weight: 400\">-<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">b<\/span><span style=\"font-weight: 400\">+<\/span><span style=\"font-weight: 400\">(b<\/span><span style=\"font-weight: 400\">**<\/span><span style=\"font-weight: 400\">2<\/span><span style=\"font-weight: 400\">-<\/span><span style=\"font-weight: 400\">4<\/span><span style=\"font-weight: 400\">*<\/span><span style=\"font-weight: 400\">a<\/span><span style=\"font-weight: 400\">*<\/span><span style=\"font-weight: 400\">c)<\/span><span style=\"font-weight: 400\">**<\/span><span style=\"font-weight: 400\">(<\/span><span style=\"font-weight: 400\">1<\/span><span style=\"font-weight: 400\">\/<\/span><span style=\"font-weight: 400\">2<\/span><span style=\"font-weight: 400\">))<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">\/<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">(<\/span><span style=\"font-weight: 400\">2<\/span><span style=\"font-weight: 400\">*<\/span><span style=\"font-weight: 400\">a)<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">x_2<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">(<\/span><span style=\"font-weight: 400\">-<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">b<\/span><span style=\"font-weight: 400\">-<\/span><span style=\"font-weight: 400\">(b<\/span><span style=\"font-weight: 400\">**<\/span><span style=\"font-weight: 400\">2<\/span><span style=\"font-weight: 400\">-<\/span><span style=\"font-weight: 400\">4<\/span><span style=\"font-weight: 400\">*<\/span><span style=\"font-weight: 400\">a<\/span><span style=\"font-weight: 400\">*<\/span><span style=\"font-weight: 400\">c)<\/span><span style=\"font-weight: 400\">**<\/span><span style=\"font-weight: 400\">(<\/span><span style=\"font-weight: 400\">1<\/span><span style=\"font-weight: 400\">\/<\/span><span style=\"font-weight: 400\">2<\/span><span style=\"font-weight: 400\">))<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">\/<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">(<\/span><span style=\"font-weight: 400\">2<\/span><span style=\"font-weight: 400\">*<\/span><span style=\"font-weight: 400\">a)<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">return<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">x_1,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">x_2<\/span><\/pre>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">You need to keep the\u00a0&#8220;&#8221;&#8221;\u00a0on the same line for one-line docstrings:<\/span><\/p>\n<pre><span style=\"font-weight: 400\">def<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">quadratic(a,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">b,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">c,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">x):<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">\"\"\"Use the quadratic formula\"\"\"<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">x_1<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">(<\/span><span style=\"font-weight: 400\">-<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">b<\/span><span style=\"font-weight: 400\">+<\/span><span style=\"font-weight: 400\">(b<\/span><span style=\"font-weight: 400\">**<\/span><span style=\"font-weight: 400\">2<\/span><span style=\"font-weight: 400\">-<\/span><span style=\"font-weight: 400\">4<\/span><span style=\"font-weight: 400\">*<\/span><span style=\"font-weight: 400\">a<\/span><span style=\"font-weight: 400\">*<\/span><span style=\"font-weight: 400\">c)<\/span><span style=\"font-weight: 400\">**<\/span><span style=\"font-weight: 400\">(<\/span><span style=\"font-weight: 400\">1<\/span><span style=\"font-weight: 400\">\/<\/span><span style=\"font-weight: 400\">2<\/span><span style=\"font-weight: 400\">))<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">\/<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">(<\/span><span style=\"font-weight: 400\">2<\/span><span style=\"font-weight: 400\">*<\/span><span style=\"font-weight: 400\">a)<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">x_2<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">(<\/span><span style=\"font-weight: 400\">-<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">b<\/span><span style=\"font-weight: 400\">-<\/span><span style=\"font-weight: 400\">(b<\/span><span style=\"font-weight: 400\">**<\/span><span style=\"font-weight: 400\">2<\/span><span style=\"font-weight: 400\">-<\/span><span style=\"font-weight: 400\">4<\/span><span style=\"font-weight: 400\">*<\/span><span style=\"font-weight: 400\">a<\/span><span style=\"font-weight: 400\">*<\/span><span style=\"font-weight: 400\">c)<\/span><span style=\"font-weight: 400\">**<\/span><span style=\"font-weight: 400\">(<\/span><span style=\"font-weight: 400\">1<\/span><span style=\"font-weight: 400\">\/<\/span><span style=\"font-weight: 400\">2<\/span><span style=\"font-weight: 400\">))<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">\/<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">(<\/span><span style=\"font-weight: 400\">2<\/span><span style=\"font-weight: 400\">*<\/span><span style=\"font-weight: 400\">a)<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">return<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">x_1,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">x_2<\/span><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"whitespace-in-expressions-and-statements\"><\/span><b>Whitespace in Expressions and Statements<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<figure id=\"attachment_165545\" aria-describedby=\"caption-attachment-165545\" style=\"width: 499px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.digitalvidya.com\/blog\/wp-content\/uploads\/2019\/11\/python-programming-essentials-m31-pep-8-11-638-510_810b4f617a76b73776b45aa2ca596222.jpg\" alt=\"Whitespace in Expressions and Statements\" class=\"wp-image-165545\" width=\"499\" height=\"374\" title=\"\"><figcaption id=\"caption-attachment-165545\" class=\"wp-caption-text\">Whitespace in expressions and statements source &#8211; slideshare<\/figcaption><\/figure>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">You have to use whitespace in expressions and statements to make the code readable. But you have to be very careful with it as too much whitespace will make combining related terms difficult and too little whitespace will make the code difficult to read.\u00a0<\/span><\/p>\n<h3><span class=\"ez-toc-section\" id=\"whitespace-around-binary-operators\"><\/span><b>Whitespace Around Binary Operators<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">When you are using binary operators, you need to use a single space on both sides. When you are assigning a value to the argument of the function, you don\u2019t have to use spaces:<\/span><\/p>\n<p dir=\"ltr\"><span style=\"font-weight: 400\"># Recommended<\/span><\/p>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">def<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">function(default_parameter<\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\">5<\/span><span style=\"font-weight: 400\">):<\/span><\/p>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\"># &#8230;<\/span><\/p>\n<p dir=\"ltr\"><span style=\"font-weight: 400\"># Not recommended<\/span><\/p>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">def<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">function(default_parameter<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">5<\/span><span style=\"font-weight: 400\">):<\/span><\/p>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\"># &#8230;<\/span><\/p>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">When there are more than one operator, you should use the whitespace around the operator that has lowest priority:<\/span><\/p>\n<p dir=\"ltr\"><span style=\"font-weight: 400\"># Recommended<\/span><\/p>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">y<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">x<\/span><span style=\"font-weight: 400\">**<\/span><span style=\"font-weight: 400\">2<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">+<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">5<\/span><\/p>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">z<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">(x<\/span><span style=\"font-weight: 400\">+<\/span><span style=\"font-weight: 400\">y)<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">*<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">(x<\/span><span style=\"font-weight: 400\">&#8211;<\/span><span style=\"font-weight: 400\">y)<\/span><\/p>\n<p dir=\"ltr\"><span style=\"font-weight: 400\"># Not Recommended<\/span><\/p>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">y<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">x<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">**<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">2<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">+<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">5<\/span><\/p>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">z<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">(x<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">+<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">y)<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">*<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">(x<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">&#8211;<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">y)<\/span><\/p>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">When you are working with slices, colons are like the binary operators. So the same rule applies to them:<\/span><\/p>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">list<\/span><span style=\"font-weight: 400\">[<\/span><span style=\"font-weight: 400\">3<\/span><span style=\"font-weight: 400\">:<\/span><span style=\"font-weight: 400\">4<\/span><span style=\"font-weight: 400\">]<\/span><\/p>\n<p dir=\"ltr\"><span style=\"font-weight: 400\"># Treat the colon as the operator with lowest priority<\/span><\/p>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">list<\/span><span style=\"font-weight: 400\">[x<\/span><span style=\"font-weight: 400\">+<\/span><span style=\"font-weight: 400\">1<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">:<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">x<\/span><span style=\"font-weight: 400\">+<\/span><span style=\"font-weight: 400\">2<\/span><span style=\"font-weight: 400\">]<\/span><\/p>\n<p dir=\"ltr\"><span style=\"font-weight: 400\"># In an extended slice, both colons must be<\/span><\/p>\n<p dir=\"ltr\"><span style=\"font-weight: 400\"># surrounded by the same amount of whitespace<\/span><\/p>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">list<\/span><span style=\"font-weight: 400\">[<\/span><span style=\"font-weight: 400\">3<\/span><span style=\"font-weight: 400\">:<\/span><span style=\"font-weight: 400\">4<\/span><span style=\"font-weight: 400\">:<\/span><span style=\"font-weight: 400\">5<\/span><span style=\"font-weight: 400\">]<\/span><\/p>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">list<\/span><span style=\"font-weight: 400\">[x<\/span><span style=\"font-weight: 400\">+<\/span><span style=\"font-weight: 400\">1<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">:<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">x<\/span><span style=\"font-weight: 400\">+<\/span><span style=\"font-weight: 400\">2<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">:<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">x<\/span><span style=\"font-weight: 400\">+<\/span><span style=\"font-weight: 400\">3<\/span><span style=\"font-weight: 400\">]<\/span><\/p>\n<p dir=\"ltr\"><span style=\"font-weight: 400\"># The space is omitted if a slice parameter is omitted<\/span><\/p>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">list<\/span><span style=\"font-weight: 400\">[x<\/span><span style=\"font-weight: 400\">+<\/span><span style=\"font-weight: 400\">1<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">:<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">x<\/span><span style=\"font-weight: 400\">+<\/span><span style=\"font-weight: 400\">2<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">:]<\/span><\/p>\n<h3><span class=\"ez-toc-section\" id=\"when-to-avoid-adding-whitespace\"><\/span><b>When to Avoid Adding Whitespace<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">In some cases, the addition of whitespace can make the code difficult to read by making it sparse. So there are some places outlined by PEP 8 where you should avoid using whitespace. The end of the line is one such place. Known as trailing whitespace, it cannot be seen and can create errors that cannot be traced. Here is a list outlining some other cases where you should not use whitespace:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Inside parentheses, braces, or brackets:<\/span><\/li>\n<\/ol>\n<pre><span style=\"font-weight: 400\"># Recommended<\/span>\r\n\r\n<span style=\"font-weight: 400\">my_list<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">[<\/span><span style=\"font-weight: 400\">1<\/span><span style=\"font-weight: 400\">,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">2<\/span><span style=\"font-weight: 400\">,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">3<\/span><span style=\"font-weight: 400\">]<\/span>\r\n\r\n<span style=\"font-weight: 400\"># Not recommended<\/span>\r\n\r\n<span style=\"font-weight: 400\">my_list<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">[<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">1<\/span><span style=\"font-weight: 400\">,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">2<\/span><span style=\"font-weight: 400\">,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">3<\/span><span style=\"font-weight: 400\">,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">]<\/span><\/pre>\n<ol start=\"2\">\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Before a colon, semicolon, or comma:<\/span><\/li>\n<\/ol>\n<pre><span style=\"font-weight: 400\">x<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">5<\/span>\r\n\r\n<span style=\"font-weight: 400\">y<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">6<\/span>\r\n\r\n<span style=\"font-weight: 400\"># Recommended<\/span>\r\n\r\n<span style=\"font-weight: 400\">print<\/span><span style=\"font-weight: 400\">(x,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">y)<\/span>\r\n\r\n<span style=\"font-weight: 400\"># Not recommended<\/span>\r\n\r\n<span style=\"font-weight: 400\">print<\/span><span style=\"font-weight: 400\">(x<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">y)<\/span><\/pre>\n<ol start=\"3\">\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Before the open parenthesis where the function\u2019s arguments start:<\/span><\/li>\n<\/ol>\n<pre><span style=\"font-weight: 400\">def<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">double(x):<\/span>\r\n\r\n<span style=\"font-weight: 400\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400\">return<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">x<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">*<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">2<\/span>\r\n\r\n<span style=\"font-weight: 400\"># Recommended<\/span>\r\n\r\n<span style=\"font-weight: 400\">double(<\/span><span style=\"font-weight: 400\">3<\/span><span style=\"font-weight: 400\">)<\/span>\r\n\r\n<span style=\"font-weight: 400\"># Not recommended<\/span>\r\n\r\n<span style=\"font-weight: 400\">double<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">(<\/span><span style=\"font-weight: 400\">3<\/span><span style=\"font-weight: 400\">)<\/span><\/pre>\n<ol start=\"4\">\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Before the open bracket starting a slice or an index:<\/span><\/li>\n<\/ol>\n<pre><span style=\"font-weight: 400\"># Recommended<\/span>\r\n\r\n<span style=\"font-weight: 400\">list<\/span><span style=\"font-weight: 400\">[<\/span><span style=\"font-weight: 400\">3<\/span><span style=\"font-weight: 400\">]<\/span>\r\n\r\n<span style=\"font-weight: 400\"># Not recommended<\/span>\r\n\r\n<span style=\"font-weight: 400\">list<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">[<\/span><span style=\"font-weight: 400\">3<\/span><span style=\"font-weight: 400\">]<\/span><\/pre>\n<ol start=\"5\">\n<li style=\"font-weight: 400\">\n<pre><span style=\"font-weight: 400\">Between a closing parenthesis and a trailing comma:<\/span><\/pre>\n<\/li>\n<\/ol>\n<pre><span style=\"font-weight: 400\"># Recommended<\/span>\r\n\r\n<span style=\"font-weight: 400\">tuple<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">(<\/span><span style=\"font-weight: 400\">1<\/span><span style=\"font-weight: 400\">,)<\/span>\r\n\r\n<span style=\"font-weight: 400\"># Not recommended<\/span>\r\n\r\n<span style=\"font-weight: 400\">tuple<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">(<\/span><span style=\"font-weight: 400\">1<\/span><span style=\"font-weight: 400\">,<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">)<\/span><\/pre>\n<ol start=\"6\">\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">For aligning the assignment operators:<\/span><\/li>\n<\/ol>\n<pre><span style=\"font-weight: 400\"># Recommended<\/span>\r\n\r\n<span style=\"font-weight: 400\">var1<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">5<\/span>\r\n\r\n<span style=\"font-weight: 400\">var2<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">6<\/span>\r\n\r\n<span style=\"font-weight: 400\">some_long_var<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">7<\/span>\r\n\r\n<span style=\"font-weight: 400\"># Not recommended<\/span>\r\n\r\n<span style=\"font-weight: 400\">var1<\/span><span style=\"font-weight: 400\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">5<\/span>\r\n\r\n<span style=\"font-weight: 400\">var2<\/span><span style=\"font-weight: 400\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">6<\/span>\r\n\r\n<span style=\"font-weight: 400\">some_long_var<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\"> <\/span><span style=\"font-weight: 400\">7<\/span><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"programming-recommendations\"><\/span><b>Programming Recommendations<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">This section will cover the suggestions provided by PEP 8 for preserving consistency and removing ambiguity.<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Don\u2019t use the equivalence operator for comparing Boolean values to true or false.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Empty sequence in if statements are false.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">In the if statements, instead of \u2018not\u2019, use \u2018is not\u2019.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">If x is not None, do not use if x:.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Instead of slicing, try using .startswith() and .endswith().<\/span><\/li>\n<\/ol>\n<h3><span class=\"ez-toc-section\" id=\"ignoring-pep-8\"><\/span><b>Ignoring PEP 8<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">PEP 8 helps you in creating a clean, readable, and professional code. So, you should never ignore PEP 8. However, you can avoid PEP 8, if it is causing inconvenience in the following cases:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Compliance with PEP 8 breaks compatibility with the software.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">The code you are working with doesn\u2019t comply with PEP 8.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">You have to make the code compatible with Python\u2019s older versions.<\/span><\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"ensuring-the-code-follows-pep-8\"><\/span><b>Ensuring the Code Follows PEP 8<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<figure id=\"attachment_165546\" aria-describedby=\"caption-attachment-165546\" style=\"width: 509px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.digitalvidya.com\/blog\/wp-content\/uploads\/2019\/11\/503x283_059eb6fc6fa332fc37076a2d1999aa73.jpg\" alt=\"Ensuring the Code Follows PEP 8\" class=\"wp-image-165546\" width=\"509\" height=\"287\" title=\"\"><figcaption id=\"caption-attachment-165546\" class=\"wp-caption-text\">Ensuring the code follows pep 8 source &#8211; youtube<\/figcaption><\/figure>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">You might not remember all the rules mentioned in the PEP 8 documentation. To speed up the process of making your code compiled with PEP 8, you can take the help of few tools including the following:<\/span><\/p>\n<h3><span class=\"ez-toc-section\" id=\"1-linters\"><\/span><strong>1. Linters<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">These are used for analyzing code and flagging errors. They also show suggestions for fixing the error. You can add them as extensions to the editor. The pycodestyle linter checks the code for style conventions mentioned in PEP 8. Flake8 is another linter tool with capabilities of debugger, pycodestyle, and pyflakes.<\/span><\/p>\n<h3><span class=\"ez-toc-section\" id=\"2-autoformatters\"><\/span><strong>2. Autoformatters<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">These programs are used for automatically refactoring the code to conform to the conventions of PEP 8. Black is one such program.<\/span><\/p>\n<p dir=\"ltr\"><span style=\"font-weight: 400\">At times these guidelines can feel pedantic, but you need to remember that they help in improving your code so that your collaborators or potential employers can understand the code. It makes the code readable and of high-quality.<\/span><\/p>\n<p dir=\"ltr\">\n<h3><span class=\"ez-toc-section\" id=\"final-thoughts\"><\/span><strong>Final Thoughts<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p dir=\"ltr\"><span class=\"word\"><span>You\u00a0<\/span><\/span><span class=\"word\"><span>can\u00a0<\/span><\/span><span class=\"word\"><span>become\u00a0a\u00a0<\/span><\/span><span class=\"word\"><span>highly\u00a0<\/span><\/span><span class=\"word\"><span>qualified\u00a0<\/span><\/span><span class=\"word\"><span>Python\u00a0<\/span><\/span><span class=\"word\"><span>programmer\u00a0<\/span><\/span><span class=\"word\"><span>by\u00a0<\/span><\/span><span class=\"word\"><span>taking\u00a0<\/span><\/span><span class=\"word\"><span>a\u00a0<\/span><\/span><a href=\"https:\/\/www.digitalvidya.com\/python-course\/\"><span class=\"word\"><span>Python\u00a0<\/span><\/span><span class=\"word\"><span>programming Course<\/span><\/span><\/a><span class=\"word\"><span>.\u00a0<\/span><\/span><span class=\"word\"><span>Any aspiring programmer<\/span><\/span><span class=\"word\"><span>\u00a0<\/span><\/span><span class=\"word\"><span>can\u00a0<\/span><\/span><span class=\"word\"><span>learn\u00a0<\/span><\/span><span class=\"word\"><span>from\u00a0<\/span><\/span><span class=\"word\"><span>the\u00a0<\/span><\/span><span class=\"word\"><span>fundamentals\u00a0<\/span><\/span><span class=\"word\"><span>of\u00a0<\/span><\/span><span class=\"word\"><span>Python\u00a0<\/span><\/span><span class=\"word\"><span>and\u00a0<\/span><\/span><span class=\"word\"><span>continue\u00a0<\/span><\/span><span class=\"word\"><span>to\u00a0<\/span><\/span><span class=\"word\"><span>finesse\u00a0<\/span><\/span><span class=\"word\"><span>Python\u00a0<\/span><\/span><span class=\"word\"><span>after\u00a0<\/span><\/span><span class=\"word\"><span>the\u00a0<\/span><\/span><span class=\"word\"><span>course.<\/span><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>PEP is a document that contains best practices and python code writing guidelines. The primary focus of the documentation is to improve the consistency and readability of the Python code, written by Guido Van Rossum, Nick Coghlan, and Barry Warsaw. PEP or Python Enhancement Proposal is used to define new features such as Python&#8217;s style [&hellip;]<\/p>\n","protected":false},"author":16370,"featured_media":172214,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[11144],"tags":[11321,11322,11323,11324],"class_list":["post-164825","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-science","tag-python-code","tag-pep8","tag-pep-8","tag-pep8-python"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.digitalvidya.com\/blog\/wp-json\/wp\/v2\/posts\/164825","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.digitalvidya.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.digitalvidya.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.digitalvidya.com\/blog\/wp-json\/wp\/v2\/users\/16370"}],"replies":[{"embeddable":true,"href":"https:\/\/www.digitalvidya.com\/blog\/wp-json\/wp\/v2\/comments?post=164825"}],"version-history":[{"count":0,"href":"https:\/\/www.digitalvidya.com\/blog\/wp-json\/wp\/v2\/posts\/164825\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.digitalvidya.com\/blog\/wp-json\/wp\/v2\/media\/172214"}],"wp:attachment":[{"href":"https:\/\/www.digitalvidya.com\/blog\/wp-json\/wp\/v2\/media?parent=164825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.digitalvidya.com\/blog\/wp-json\/wp\/v2\/categories?post=164825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.digitalvidya.com\/blog\/wp-json\/wp\/v2\/tags?post=164825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}