Skip to main content

Notes - Tues 01/12/21

Tuesday 01/12/21

Methods and Python Documentation

Objects in Python have methods. So for example the .split() and .append() are types of methods. A list of the methods can be found by just typing in mylist. and seeing the popup window in VSCode. You can also use the help function which would be help(mylist.mymethod) which will output help. It's like man in the terminal.

For more in-depth documentation, you can go to the official Python documentation.

Intro to Functions

Functions allow us to create blocks of code that can be repeated or executed multiple times. This unlocks more capabilities and problem-solving.

def Keyword - Define a Function

Creating functions uses specific syntax. We use the def keyword to define a function. This includes the def keyword, indentation, and proper structure. So let's look at the basic syntax:

def name_of_function():
    ```
    Docstring
    ```
    then_your_code_goes_here
  1. def = Tells Python you're creating a function
  2. name_of_function = This is the name of your function. Note it uses "snake casing" which is the lower case words seperated with underscores _. This is the standard Python way to note functions so it's easy to ID later.
  3. parenthesis () = We can pass in arguements or parameters into the function. This sets up future potential.
  4. colon : = Indicates to Python and upcoming indented block which will be what's inside the function.
  5. Docstring ``` = It's a multiple format to put in comments about the function. I've been using it in Markdown to show lines of code and been using multiple lines of # to do the same thing.
  6. Then you write your code that you want the function to execure.
  • Note that functions can accept arguements to be passed by the user.

Typically we use return keyword to see the result of the function instead of printing it out. We can use return to assign the output of the function to a new variable. So this "saves" the output. Return will be covered in depth later.