#100DaysOfPython Day 2: Functions, Scope and Best Practices
Tags: todayilearned, python, 100daysofcode, beginners
Published: 2019-11-19 18:08:39

---
title: #100DaysOfPython Day 2: Functions, Scope and Best Practices
published: true
description: Growing collection of notes on Python
tags: #todayilearned, python, 100daysofcode, beginners
canonical_url: https://taeluralexis.com/2020/04/22/100daysofpython-day-2-functions-scope-and-best-practices/
---
![link title](https://thepracticaldev.s3.amazonaws.com/i/70abxudewznrbvg6dhv6.gif)
So far I've learned a bit about the different types Python posseses and how to print Hello World. Today however, I've learned more about Python's best practices as well as functions and how the language handles access to variables in the local and global scope <strong>(spoiler alert: Scope works the same way in Python as it does in JavaScript :-p) </strong>.

<strong> Update: </strong> My collection of notes will always be updated as I learn more things. I'm merely posting as I actively learn. I do not claim to be a Pythonista, I am not an expert.

## Advocating for Best Practices

A key quality of an exceptional engineer is consistently writing code that follows best practices. But why is that important? Style guides that contain best practices are basically a set of rules and guidelines that ensures consistency, readability for other humans and efficiency. 

The standard that the Python community follows is called <a href="https://www.python.iorg/dev/peps/pep-0008/" target="_blank">PEP-8</a>. My advice, especially when you're a beginner, is to bookmark this style guide (or the relevant style guide to your programming language/framework) and reference it frequently.

<strong> Key tip for early-career developers: </strong> The best way to write consistently good code is to always practice and reference standard guides.

Here are just a few of Python's Best Practices:
<table style="width:100%">
  <tr>
    <th>Rule</th>
    <th>Meaning</th> 
    <th>Example</th>
  </tr>
  <tr>
    <td>Function & Variable Names</td>
    <td>Be explicit with variable & function names </td>
    <td>x = "x is a nondescript variable name" <br> <strong> vs </strong> <br> favorite_food =" This variable name gives us a better hint at what this piece of data is about"</td>
  </tr>
  <tr>
    <td>Indentation</td>
    <td>Indentation is an essential concept in Python</td>
    <td>Use 4 spaces per indentation level </td>
  </tr>
  <tr>
    <td>Underscores create more readability</td>
    <td>For your descriptive and longer variable and function names, use underscores to make the names more readable</td>
    <td> self_care_message() <br> <strong>vs.</strong> <br>selfcaremessage() </td>
  </tr>
<tr>
<td>Refrain from using confusing standalone letters as variable or function names; exception is when they have meaning in math </td>
<td> Using the lowercase letter <strong>'l'</strong>, uppercase letter <strong>O</strong> or uppercase letter <strong>I</strong> can be confusing. We don't write code for ourselves, we write for others.</td>
<td> i = 1</td>
</tr>
</table>

## All About Functions

### Indentation Matters, Ya'll 
####Anatomy of a Function:
![link title](https://thepracticaldev.s3.amazonaws.com/i/xvb2fypkn3e1260hs3oe.png)
<ol>
<li> <strong>def</strong> is how you initalize the function </li>
<li> Name the function and be as descriptive as you can be (remember if you're being verbose with the naming, use underscores in between each word for the function name) </li>
<li> Follow the function name with parentheses and a <strong>colon</strong>! (As a Javascript developer I was initially tripped up by this and kept using a semicolon)</li>
<li> Make an indentation (a tab or 4 spaces) to create your instructions (aka return or print statement)</li>
<li> Complete and Exit the function by pressing enter</li>
<li> Call the function by the name you gave it followed by parentheses</li>
</ol>
### Difference between a return statement versus a print statement

A return statement will explicitly return a value.

A print statement will return nothing.

![link title](https://thepracticaldev.s3.amazonaws.com/i/27x8th75pcpnwwjz73hd.png)

### Local and Global Scope

Scope is what determines what variables you can access. I learned in JavaScript that there is a global scope as well as a local scope.

<strong> Local scope </strong> is when the variables you define within a function can only be accessed in that very function in which you defined it. It won't be accessible to the <strong>global scope <strong>(aka rest of the application).


In internal/local scope, the function has access to variables defined within it; has access to variables defined in the global scope (aka rest of application outside that function) but cant change them. Meaning you can't reassign a global variable within a local function. 

Functions without proper whitespace leads to an indentation error. 

In production-level code, don't have too many global variables outside of a defined scope to prevent confusion and bugs; constants are okay to define outside of a scope

### Notes on Arguments
![link title](https://thepracticaldev.s3.amazonaws.com/i/6310momn665cug373j8m.png)
Positional arguments are all required and must be given in the order in which they are declared.

Default arguments is kinda like the fallback/default option when you don't call the last argument when you invoke a function. It is always the last argument in a function. 


# What Will I Learn Next in Day 3 of the #100DaysOfPython series?

<ul>
 <li>More advanced data types like sets, tuples, dictionaries</li>
</ul>