DEV Community

#100DaysOfPython Day 2: Functions, Scope and Best Practices

Tae'lur Alexis πŸ¦„βš› on November 19, 2019

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 ...
Collapse
 
dbanty profile image
Dylan Anthony

Cool post, neat to see Python concepts explained from a JavaScript perspective. I have a few notes / clarifications on some of your points.

  1. Return vs print: the way it was phrased made it seem like the print statement was similar to the return statement. It’s not, print is just a function basically equivalent to console.log.

  2. Scopes: you actually can modify global variables from a local scope by using the global keyword, like this:

global_var = 1

def my_func():
    global global_var
    global_var = 2
    local_var = "a"

    def inner_func():
        nonlocal local_var  # similar to global for inner functions
        local_var = "b"
  1. Arguments: you can have any number of optional arguments (arguments with defaults) after required arguments. There are also a lot more things you can do with arguments (arbitrary args, keyword only, required keywords, positional only) that are useful. They usually involve asterisks and I’m sure you’ll come across them later.
Collapse
 
taeluralexis profile image
Tae'lur Alexis πŸ¦„βš› • Edited

These are not meant to be comprehensive tutorials that teach everything about a topic, they're my personal notes that I made into posts. But thanks

Collapse
 
jnario profile image
Jose Nario

Good stuff. Keep going!

Since you're discussing best practices, it's worth noting that functions should have a "docstring". This string appears directly after the function declaration, and is wrapped in triple-quotes.

This docstring can be accessed/inspected via the REPL, is used by IDEs, and is also returned if you call help(your_function).

More: python.org/dev/peps/pep-0257/

Good luck!

Collapse
 
taeluralexis profile image
Tae'lur Alexis πŸ¦„βš›

I haven't learned that yet. I'm posting and updating as I learn more. For what it's worth, these are notes not tutorials but thank you :)

Collapse
 
xaviguasch profile image
Xavi Guasch

Really loving this series so far!

As a JS dev I'm very curious about Python too, but before I finally dive into it, here's a suggestion about a future post that might be useful for others like me: it's the WHY of Python. What makes Python more suitable to data science, machine learning or other specific fields compared to JS? What makes the language special in that sense?

Collapse
 
schwitzd profile image
Schwitzd

day 3?

Collapse
 
westcliffe profile image
westcliffe

thanks for updating.

Collapse
 
miayuxin profile image
Mia • Edited

Hi! Im wondering when will you update day 3?? Since I am a real beginner, and would like to join your 100 days activity, hope you will update sooooon><

Collapse
 
taeluralexis profile image
Tae'lur Alexis πŸ¦„βš›

Hey Mia! I'm really glad you enjoy the series. Aim is to post every few days so it allows me time to edit and add as much info to each post as well as to update old posts too as I'm learning Python.

If you follow me on Twitter @taeluralexis I usually send the link out there as soon as each post is live on dev.

Post #3 comes out on Monday. Post #4 on Wednesday πŸ’™

Collapse
 
tatianacodes profile image
Tatiana

I continually forget to use a colon, or I'm so stuck on JS that I use the semicolon! Python is so refreshing as a way to get away from front end webdev/JS.