How to create methods in Python
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.
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.
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:
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
Here you are !
You can now reuse this method in other projects without running into problems.
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.
Related Articles
Continue your learning journey with these related topics
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. 📚