How to create methods in Python

1 min readFundamentalsGetting Started
7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

Python will allow you to create your own methods.

What I mean by methods are pieces of code that you can re-use multiple times.

You will define it once within your main script and will be able run it multiple times, with different parameters.

Here is an example

Here is an example of a method that I use quite often.

def get_csv_in_folder(path):
    """Return the list of csv files in a specific folder
    
    Args:
    	path (string) : Path to the folder of interest
    Return:
    	(list) : all the csv files contained in given folder path
    """
    import os # We can import libraries within the scope of the method

    # We uses the list comprehension capability of python to create
    # The list of csv files in a folder. You can do the same in a loop
    # This can be read as the following
    # Give me every object in the folder (path) where .csv is in the name
    folder_list = [each for each in os.listdir(path) if ".csv" in each]

    return folder_list
Retrieving the list of csv files in a folder

Passing parameters to the method

In order for the method to process information, a good way to send them data is through the use of parameters.

Here is an example with three parameters:

def my_method(parameter1, parameter2, parameter3):
	print(f"param1 : {parameter1}\nparam2 : {parameter2}\nparam3:\n {parameter3}")
Example of method that prints out three parameters

A real life situation

So imagine you are working on a project that requires you to list the csv files contained in a specific folder. (e.g. sales of your e-commerce that has been splitted in files per month) see below.

current_dir/
├── my_script.py
└── data
      ├── january.csv
      └── february.csv
print(get_csv_in_folder("./data")) # This will give you the list of csv files contained in the data folder.
my_script.py

Here you are !

You can now reuse this method in other projects without running into problems.

7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

Free Newsletter

Master Data Science in Days, Not Months 🚀

Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚

Weekly simple and practical lessons
Access to ready to use code examples
Skip the math, focus on results
Learn while drinking your coffee

By subscribing, you agree to receive our newsletter. You can unsubscribe at any time.