Python Functions Containing a Few Arguments: Exercise

Join over 2 million students who advanced their careers with 365 Data Science. Learn from instructors who have worked at Meta, Spotify, Google, IKEA, Netflix, and Coca-Cola and master Python, SQL, Excel, machine learning, data analysis, AI fundamentals, and more.

Start for Free
Martin Ganchev Martin Ganchev 15 Apr 2024 1 min read

Functions Containing a Few Arguments Exercise:

Create a function that determines the type of a triangle based on the lengths of its sides.

If you have followed our exercises, you may have seen a variation of this one – to create an IF statement that checks the same thing. This exercise can be considered its ‘more advanced form’ (although for some of you it may still be easy to code).

The tutorial you may need: An Introduction to Python Functions

python-solution-image

 

Now, sometimes we need to use an algorithm several times and typing it out over and over can cause inconvenience and lead to mistakes. This is a prime example, so it is a great idea to transform an algorithm into a function.

We start by defining a function that takes 3 parameters.

For instance: x, y and containing the lengths of each side of the triangle. We follow that with an IF statement with multiple conditions that test whether the triangle is equilateral.

def triangle_type(x,y,z):

Since equality is transitive, if x equals y and y equals z, then z also equals x. Thus, our IF statement needs to account for only two of these conditions. Suppose we choose to test whether x equals y and x equals z, then our code would look like this:

def triangle_type(x,y,z):
 
    if x==y and x==z:

If both conditions are satisfied, the function returns the message “Equilateral”.

Remember to indent the return command, so that it is only executed within the IF statement.

def triangle_type(x,y,z):
 
    if x==y and x==z:
 
        return("Equilateral")

Next, we add another IF statement checking whether the triangle is isosceles.

Note that returning a value automatically exits the function, so using an ELIF statement is not necessary in this case. All the triangles we will be testing are isosceles or obtuse since we already accounted for the equilateral ones. Thus, simply checking whether x equals y, y equals z or x equals z guarantees a triangle being isosceles. We introduce an IF statement which does precisely that and looks like this:

def triangle_type(x,y,z):
 
    if x==y and x==z:
 
        return("Equilateral")
 
    if x==y or x==z or y==z:

Just as with our first IF statement, we indent and return the appropriate type of triangle as a message.

def triangle_type(x,y,z):
 
    if x==y and x==z:
 
        return("Equilateral")
 
    if x==y or x==z or y==z:
 
        return("Isosceles")

By utilizing the fact that returning a value exits the function, we simply add another return statement at the end of the function to indicate an obtuse triangle. Make sure not to indent it under any of the prior IF statements. Reaching this last return line indicates that the parameters (x, y and z) contradict both the IF and ELIF statements preceding it. Therefore, using an ELSE statement here is unnecessary. You can do so if you wish, but there really isn’t any value to it!

def triangle_type(x,y,z):
 
    if x==y and x==z:
 
        return("Equilateral")
 
    if x==y or x==z or y==z:
 
        return("Isosceles")
 
    return("Obtuse")

To make sure our work is complete, you can define 3 variables x, y and z and assign them numerical values.

Afterwards, call the function using these parameters and check whether our code works as intended. Change the values of the variables to test if all possible scenarios are covered correctly.

And that was the end of this exercise. Note that this is only one possible solution. Using ELIF and ELSE statements in this exercise is not wrong, but we omit them since they are not necessary. Furthermore, the order in which we test for the various types of triangles is irrelevant so long as we write appropriate conditional statements!

Learn more about conditional statements, functions, operators, etc. in our Introduction to Python course.

Martin Ganchev

Martin Ganchev

Instructor at 365 Data Science

Martin holds an MSc degree in Economic and Social Sciences from Bocconi University. His diverse academic and research experience combined with his friendly and explanatory approach to teaching have made him one of the most beloved instructors on our team. Some of the courses he has authored include: SQL, SQL + Tableau, SQL+Tableau+Python, Introduction to Python, Introduction to Jupyter, to name a few.

Top