Rodrigo πŸπŸ“ Profile picture
Sep 30 β€’ 8 tweets β€’ 2 min read
This is a complete diagram of how you define a function in Python 🐍
To define a function in Python you need two main parts:

- the header
- the body

The header gives defines how the function is called and the arguments it needs.

The body is the code that does stuff.
The header starts with the keyword `def`, which is how Python 🐍 knows we are about to define a function.

Then, we have the function name, which should be descriptive.

After that, we have the list of parameters.

After the list of parameters, a colon.
The list of parameters starts and ends respectively with ( & ).

Between the parens, you write a comma-separated list of parameters.

The parameters are the data your function receives.

The parameter list can be empty, in which case you just have `():` next to the function name.
The function body is indented once with respect to the header and contains the code that runs when the function is called.

Before the body, it is customary to see a docstring, a comment that describes the function.

Your IDE will show you the docstring when you use the function.
Functions typically have return statments.

Return statements signal the end of the function & the data that comes off of the function.

If your function computes some result, you probably will use a return statement to give it back to the function user.
This was the gist of how you define a function in Python 🐍

Did I forget anything? Let me know πŸ‘‡

If you liked this short thread, follow me @mathsppblog.

I'm helping you take your Python 🐍 skills to the next level πŸš€
TL;DR:

A function definition has:

- its defining keyword
- the function name
- a (possibly empty) parameter list
- a colon to start the body
- a docstring with a function description
- the function body to do stuff
- a return statement to give results back

β€’ β€’ β€’

Missing some Tweet in this thread? You can try to force a refresh
γ€€

Keep Current with Rodrigo πŸπŸ“

Rodrigo πŸπŸ“ Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @mathsppblog

Sep 18
Python 🐍 decorators are usually 2 nested functions.

If you need a decorator with arguments, then it's 3.

But how do those work? >>> @print_args("Args ...
Decorators in Python can be applied with the at operator @.

However, that is just syntactic sugar!

Using the @ before or calling the decorator after is the same thing: # With syntactic sugar πŸ‘€βœ¨ @...
So, we see that what comes after the @ is the callable that receives the function.

Hence, if your decorator accepts arguments, it must return a callable to serve as the actual decorator.

That's because, without syntactic sugar, this is what `print_args` does: # With syntactic sugar πŸ‘€βœ¨ @...
Read 6 tweets
Sep 5
The Python 🐍 command has many different switches.

Here are the 4 switches I use the most: Ξ» python -c "import math; print(math.factorial(15))&qu
The switch `-c` runs code directly from the command line.

It doesn't open the REPL, and it is convenient for short, one-off expressions.

The result isn't printed by default, so don't forget your `print`! # What is the factorial of 15? Ξ» python -c "import mat
The switch `-m` runs a module as a script.

This will run an installed module's section that is inside `if __name__ == "__main__":`.

The one I use the most is the module `timeit` to measure execution time. Ξ» python -m timeit -s "import math" "math.fa
Read 5 tweets
Sep 3
I learned Python 🐍 on the Internet for free.

That's why my Pydon'ts book is free:

πŸ‘‰ +300 pages
πŸ‘‰ +25 chapters
πŸ‘‰ Available for $0
πŸ‘‰ Always growing

I can't do the hard work for you, but I can make it *easier*!

Link on my profile!
@mathsppblog Downloading the book is the first step.

Now, you have to put the work in!

Let's go Emil @ejd1870

Read 17 tweets
Aug 26
Here are 3 ways in which you can create a Python 🐍 dictionary.

I bet you never used the last one πŸ€” >>> dict([(1, "one"), (2, "two")]) {1: '
#1 an iterable of key, value pairs.

The built-in `dict` can take an iterable with key, value pairs.

Useful, for example, when you have a bunch of keys and a bunch of values that you put together with `zip`: >>> dict([(1, "one"), (2, "two")]) {1: '
#2 keyword arguments

You can use the keyword arguments to `dict` to define key, value pairs in your dictionary!

However, this only works if your keys are valid variable names: # Values don't have to be strings: >>> dict(one=1, two=2) {'
Read 7 tweets
Aug 25
Here are 3 simple ways in which you can reverse a Python 🐍 list.

Let's see how they are different. >>> lst = [42, 73, 0] >>> rev1 = reversed(lst) >>> rev2 = ls
#1: the built-in `reversed`:

The built-in `reversed` accepts a sequence and returns an object that knows how to iterate over that sequence IN REVERSE.

Hence, `reversed`.

Notice it doesn't return a list: >>> reversed(lst) <list_reverseiterator object at 0x00000241
The `list_reverseiterator` object that is returned is β€œlinked” to the original list...

So, if you change the original list, the reverse iterator will notice: >>> lst = [42, 73, 0] >>> rev = reversed(lst) >>> lst[1] = 9
Read 10 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(