Skip to content
Felix
  • Topics
    • My List
    • Felix Guide
    • Asset Management
    • Coding and Data Analysis
      • Data Analysis and Visualization
      • Financial Data Tools
      • Python
      • SQL
    • Credit
      • Credit Analysis
      • Restructuring
    • Financial Literacy Essentials
      • Financial Data Tools
      • Financial Math
      • Foundations of Accounting
    • Industry Specific
      • Banks
      • Chemicals
      • Consumer
      • ESG
      • Insurance
      • Oil and Gas
      • Pharmaceuticals
      • Project Finance
      • Real Estate
      • Renewable Energy
      • Technology
      • Telecoms
    • Introductory Courses
    • Investment Banking
      • Accounting
      • Financial Modeling
      • M&A and Divestitures
      • Private Debt
      • Private Equity
      • Valuation
      • Venture Capital
    • Markets
      • Economics
      • Equity Markets and Derivatives
      • Fixed Income and Derivatives
      • Introduction to Markets
      • Options and Structured Products
      • Other Capital Markets
      • Securities Services
    • Microsoft Office
      • Excel
      • PowerPoint
      • Word & Outlook
    • Professional Skills
      • Career Development
      • Expert Interviews
      • Interview Skills
    • Risk Management
    • Transaction Banking
    • Felix Live
  • Pathways
    • Investment Banking
    • Asset Management
    • Equity Research
    • Sales and Trading
    • Commercial Banking
    • Engineering
    • Operations
    • Private Equity
    • Credit Analysis
    • Restructuring
    • Venture Capital
    • CFA Institute
  • Certified Courses
  • Ask An Instructor
  • Support
  • Log in
  • Topics
    • My List
    • Felix Guide
    • Asset Management
    • Coding and Data Analysis
      • Data Analysis and Visualization
      • Financial Data Tools
      • Python
      • SQL
    • Credit
      • Credit Analysis
      • Restructuring
    • Financial Literacy Essentials
      • Financial Data Tools
      • Financial Math
      • Foundations of Accounting
    • Industry Specific
      • Banks
      • Chemicals
      • Consumer
      • ESG
      • Insurance
      • Oil and Gas
      • Pharmaceuticals
      • Project Finance
      • Real Estate
      • Renewable Energy
      • Technology
      • Telecoms
    • Introductory Courses
    • Investment Banking
      • Accounting
      • Financial Modeling
      • M&A and Divestitures
      • Private Debt
      • Private Equity
      • Valuation
      • Venture Capital
    • Markets
      • Economics
      • Equity Markets and Derivatives
      • Fixed Income and Derivatives
      • Introduction to Markets
      • Options and Structured Products
      • Other Capital Markets
      • Securities Services
    • Microsoft Office
      • Excel
      • PowerPoint
      • Word & Outlook
    • Professional Skills
      • Career Development
      • Expert Interviews
      • Interview Skills
    • Risk Management
    • Transaction Banking
    • Felix Live
  • Pathways
    • Investment Banking
    • Asset Management
    • Equity Research
    • Sales and Trading
    • Commercial Banking
    • Engineering
    • Operations
    • Private Equity
    • Credit Analysis
    • Restructuring
    • Venture Capital
    • CFA Institute
  • Certified Courses
Felix
  • Data
    • Company Analytics
    • My Filing Annotations
    • Market & Industry Data
    • United States
    • Relative Valuation
    • Discount Rate
    • Building Forecasts
    • Capital Structure Analysis
    • Europe
    • Relative Valuation
    • Discount Rate
    • Building Forecasts
    • Capital Structure Analysis
  • Models
  • Account
    • Edit my profile
    • My List
    • Restart Homepage Tour
    • Restart Company Analytics Tour
    • Restart Filings Tour
  • Log in
  • Ask An Instructor
    • Email Our Experts
    • Felix User Guide
    • Contact Support

Custom Functions, For Loops, Conditional Logic

Create your own custom functions in Python. Learn how to repeat tasks through iterable objects using for loops, and incorporate conditional logic using if statements.

Unlock Your Certificate   
 
0% Complete

11 Lessons (35m)

Show lesson playlist
  • Description & Objectives

  • 1. Custom Functions Learning Objectives

    00:19
  • 2. Creating Custom Functions

    07:51
  • 3. Custom Functions Workout

    02:13
  • 4. Adding Arguments

    03:09
  • 5. For Loops

    04:04
  • 6. Adding Arguments Workout

    03:07
  • 7. Filling a List with a For Loop

    03:20
  • 8. For Loops Workout

    02:37
  • 9. Conditional Logic

    04:48
  • 10. Conditional Logic Workout

    03:44
  • 11. Custom Functions Review

    00:25

Prev: Python Objects Next: NumPy

Creating Custom Functions

  • Notes
  • Questions
  • Transcript
  • 07:51

Learn how to create and utilize custom functions in Python for more efficient code reuse and flexibility, incorporating arguments for broader applicability.

Downloads

Custom Functions Jupyter Notebook Empty

Glossary

-Min Function custom functions Max Function Python Return Values
Back to top
Financial Edge Training

© Financial Edge Training 2025

Topics
Introduction to Finance Accounting Financial Modeling Valuation M&A and Divestitures Private Equity
Venture Capital Project Finance Credit Analysis Transaction Banking Restructuring Capital Markets
Asset Management Risk Management Economics Data Science and System
Request New Content
System Account User Guide Privacy Policy Terms & Conditions Log in
Transcript

You've used a lot of built-in functions in Python to complete different tasks. So for example, you learned how to use the max function and the min function. Right here we have a list and I've already written out max and min to find the maximum and the minimum value in that list. So if I execute that cell, it gives me 1.39 and 0.81. These are the built-in functions that come as a default with Python.

In addition to Python's built-in functions, you can also create custom functions so that you can package and reuse your own code. At a bare minimum, every custom function contains three parts. First, the def keyword, which indicates that a function is being defined. Then the new name of the function followed by parentheses and a colon. And finally, an indented code block that defines what the function does. Let's say that you're using the min and max function often together for efficiency you could create your own custom function to combine those two built-in functions together. So I'd start with the def keyword to indicate that I'm defining a new function. Then I'd write the name, a new name for the function. Let's call this one min max, and I'm gonna have a pair of parentheses and we'll get into what those are for a little bit later. And then we're gonna add a colon. And below that we're just gonna have a code block. So here I wanna combine the two actions that I have in the code block above or in the cell above rather. So I'm just gonna copy those. So there it is. I have created a new custom function called min max. And whenever I wanna call it, I call it just like a normal function. So I can just say min max. And then when I execute that, I'm gonna get the same exact values, those same exact objects from the list beta values.

However, this function has one major limitation. Can you spot it? It's that this function will only work on the list beta values. For a more flexible function, you need to add one more ingredient, an argument. You're already familiar with arguments from Excel. They're the values that you enter in the parentheses of an Excel function in Python, you create an argument in the parentheses after defining the function name. Then inside the indented code block, the argument is used as a variable. Let's create a new min max function that incorporates an argument so that it's more flexible and can be used to calculate the min and the max for different lists. I'm gonna start with that def keyword and then let's say new min max. And in parentheses, this is where we name our argument. And the name of our argument can be anything here. You could use the letter A or B or C, but here I'm going to use a more descriptive name. Let's just call it list argument.

And then the colon. And here is our code block. I'm just going to copy that from above and paste it because we want it to do almost exactly the same thing.

But where it says beta values, I'm going to replace that with my argument list argument.

So now how this works is if I want to call this function, I've defined new min max, I write the name new min max, and then I open my parentheses in min max. We didn't put any arguments in those parentheses because we already indicated in the code block right here what list we're calculating the min and the max of. In new min max, we only have this list argument. So we have to input that argument here to tell the function where to apply the different steps that are in our code block. So here I'm gonna write beta values, the list that we want it to apply to, and now it's going to replace everywhere that says list argument here, it's gonna replace that with our list beta values.

So when I execute that, I get exactly the same answer. But if I were to create a new list, let's say we have list one and I'll make it something easy, one, two, and three. Now I can use new min max and instead of beta values, I can use this new list one, and now it gives me one and three. So that's a much more flexible custom function.

As you know from previous lessons, you can use python's built-in functions to define new variables. For example, if I wanted to create a variable called betamin, that was the minimum value from that list beta values, I could just use the min function, which is a built-in Python function, pass it the beta values list.

And then when I print beta in my new variable, it gives me 0.81. However, if I try to define a variable using the custom function that we just made, I'm gonna run into a problem. So here I've defined a new variable beta min max, which is using our new min max function applied to the beta values list. When I print beta min max, it gives me 0.81, which we know is the minimum value 1.39, which we know is the maximum. And then it gives us this none output that looks kind of funny. Let's print out beta min max by itself now. The value that's been stored in that variable I created is none. Why is that? To solve this problem, we need to tell our function to return a value using the return keyword. And in this case, we're going to remove the print commands and we're gonna define a min variable and a max variable and tell the function to return both those variables as a list. So let's define new new min max. And we're gonna give it list argument so it's flexible and can work on different lists instead of just a single one. And in our code block, we're gonna create a variable for our minimum value, and we're gonna set that variable equal to the minimum that built-in function. The minimum of whatever list we pass it. Then we're gonna create a new max variable, and that's gonna be the max of our list. And then finally, we're gonna return in the form of a list that minimum variable that we created followed by the max variable. So now when I try to define this variable beta bin max, it gives me the minimum and the maximum and the format of a list. In fact, as long as we're including a return value, we actually don't need the code block before it. We could rewrite this function in only two lines. So here, instead of creating those two variables, min var and max var, I'm just gonna start this code block with return and then in square bracket so that it gives us a list. I'm gonna do min of list argument, and then max of list argument. So when I redefine beta min max and print it out, the answer is exactly the same. And we've accomplished that with a custom function written in only two lines.

Content Requests and Questions

You are trying to access premium learning content.

Discover our full catalogue and purchase a course Access all courses with our premium plans or log in to your account
Help

You need an account to contact support.

Create a free account or log in to an existing one

Sorry, you don't have access to that yet!

You are trying to access premium learning content.

Discover our full catalogue and purchase a course Access all courses with our premium plans or log in to your account

You have reached the limit of annotations (10) under our premium subscription. Upgrade to unlock unlimited annotations.

Find out more about our premium plan

You are trying to access content that requires a free account. Sign up or login in seconds!

Create a free account or log in to an existing one

You are trying to access content that requires a premium plan.

Find out more about our premium plan or log in to your account

Only US listed companies are available under our Free and Boost plans. Upgrade to Pro to access over 7,000 global companies across the US, UK, Canada, France, Italy, Germany, Hong Kong and more.

Find out more about our premium plan or log in to your account

A pro account is required for the Excel Add In

Find out more about our premium plan

Congratulations on completing

This field is hidden when viewing the form
Name(Required)
This field is hidden when viewing the form
Rate this course out of 5, where 5 is excellent and 1 is terrible.
Were the stated learning objectives met?(Required)
Were the stated prerequisite requirements appropriate and sufficient?(Required)
Were the program materials, including the qualified assessment, relevant and did they contribute to the achievement of the learning objectives?(Required)
Was the time allotted to the learning activity appropriate?(Required)
Are you happy for us to use your feedback and details in future marketing?(Required)

Thank you for already submitting feedback for this course.

CPE

What is CPE?

CPE stands for Continuing Professional Education, by completing learning activities you earn CPE credits to retain your professional credentials. CPE is required for Certified Public Accountants (CPAs). Financial Edge Training is registered with the National Association of State Boards of Accountancy (NASBA) as a sponsor of continuing professional education on the National Registry of CPE Sponsors.

What are CPE credits?

For self study programs, 1 CPE credit is awarded for every 50 minutes of elearning content, this includes videos, workouts, tryouts, and exams.

CPE Exams

You must complete the CPE exam within 1 year of accessing a related playlist or course to earn CPE credits. To see how long you have left to complete a CPE exam, hover over the locked CPE credits button.

What if I'm not collecting CPE credits?

CPE exams do not count towards your FE certification. You do not need to complete the CPE exam if you are not collecting CPE credits, but you might find it useful for your own revision.


Further Help
  • Felix How to Guide walks you through the key functions and tools of the learning platform.
  • Playlists & Tryouts: Playlists are a collection of videos that teach you a specific skill and are tested with a tryout at the end. A tryout is a quiz that tests your knowledge and understanding of what you have just learned.
  • Exam: If you are collecting CPE points you must pass the relevant CPE exam within 1 year to receive credits.
  • Glossary: A glossary can be found below each video and provides definitions and explanations for terms and concepts. They are organized alphabetically to make it easy for you to find the term you need.
  • Search function: Use the Felix search function on the homepage to find content related to what you want to learn. Find related video content, lessons, and questions people have asked on the topic.
  • Closed Captions & Transcript: Closed captions and transcripts are available on videos. The video transcript can be found next to the closed captions in the video player. The transcript feature allows you to read the transcript of the video and search for key terms within the transcript.
  • Questions: If you have questions about the course content, you will find a section called Ask a Question underneath each video where you can submit questions to our expert instructor team.