What if we used print instead of return for each if statement?

Question

In the context of this exercise, if we had a similar function with multiple if statements, and used print instead of return for each one, what would happen?

Answer

In most of the exercises within this lesson, each if statement contains a return statement. A return statement immediately exits a function when it runs, so even with multiple if statements with True conditions, when one of them runs their code block, none of the other statements would run.

If instead, we used print functions in each if statement without using return, this can change how the code would behave. If you had multiple if statements with conditions evaluating to True, then each one would run their code block, because a print does not exit the function.

Example

# Using return statements
def evaluate(score):
  if score >= 9:
    return "Excellent"
  if score >= 5:
    return "Good"
  if score >= 1:
    return "Bad"

# returns "Excellent"
evaluate(10) 

# Using print statements
def evaluate(score):
  if score >= 9:
    print("Excellent")
  if score >= 5:
    print("Good")
  if score >= 1:
    print("Bad")

evaluate(10) 
# prints out in the console
# Excellent
# Good
# Bad
32 Likes

6 posts were split to a new topic: Can I have more than one function with the same name?

Excuse me but i do not get it.
Why when using the print statement the code prints all 3 options? (excellent,good, bad) ? As i see it it should only print the one that’s true, because we make it print only one out of 3, the one that is correct.

Haha, after thinking it for a little more i understand it. I will reply to myself just to make it more clear for other people that got through the same misunderstanding as me.

When using the print statement, all of the 3 options are true. For example, if we evaluate it to 10, that means that 10 is >=9 and at the same time is >=5 and >=1.

So it prints ALL the options that are true.

28 Likes

Exactly! The way to avoid that behavior if we wanted to only resolve a single case would be to use else if which would only run if the previous if did not run.

9 Likes

A brief technical update to your initial question…
All values will be printed because print statement does not EXIT a function and will remain in the function and print every True option while return EXITS as soon as a condition is True.

10 Likes

return exits as soon as it is encountered. End of story. Whether it has a return value or not is up to us. If none is given, then None is returned.

13 Likes

Isn’t it a good good practice to use parentheses to conditionals because it may be redundant or unnecessary in python but it is must in JavaScript and may be in other languages also. So, we will not have to worry in which we have to use and in which we have to not use. As a beginner I’m asking this. Thank you.

3 Likes

Different languages have different conventions, and there are different use cases for parenthesis in conditionals; there is no catch-all answer to your question.

1 Like

The less convoluted the condition, the less likely the need for grouping (with parenthesis). Keep the conditions simple in Python and never worry about parens. Just remember the colon before the block.

4 Likes

Couldn’t the print function still act the same as a return function if you change the kind of if statement? Say for example, I code it this way, using elif:

def evaluate(score):
    if score >= 9:
        print('Excellent')
    elif score >= 5:
        print('Good')
    elif score >= 1:
        print('Bad')

evaluate(1)
# prints out 'Bad'

It will go through the code and act the same way as if there were a return function. What would make this way be incorrect or not proper? Is it simply a matter of semantics?

1 Like

return is not a function, but a statement that ends execution of a function and resumes flow at the caller. It gives a function a channel to pass data back to the caller for future use by the program.

print() is immediately executed and gives nothing back to the caller save the default return value, None.

8 Likes

Thank you for the concise and prompt response! That makes sense.

1 Like

You must understand that each if statement will be a new code block, therefore all your ifs will be executed, if you want to have a result for several checks in the next courses the “else” statements will be taught that allows us to show a result if the checks have been False and “elif” works like an “if” but within a block (it will not create a new block), which allows you to create several conditions without having the problem that you tell us, greetings and good luck!

I find the question confusing only because the exercise is using the print function but is asking what would happen if we used print instead of return.

# Enter a user name here, make sure to make it a string
user_name = "angela_catlady_87"

if user_name == "Dave":
  print("Get off my computer Dave!")

if user_name == "angela_catlady_87":
  print("I know it is you, Dave! Go away!")

Question:

  1. If we had a similar function with multiple if statements, and used print instead of return for each one, what would happen?

like…where (tf) did this “return” function come from?

1 Like

but if the first code runs, the code execution stops there at the printed out code, else it will go to the next code and the next code till it finds a true statement to execute.

thanks a lot.very helpful.

If you click the “exercise” hyperlink, you’ll see that it no longer points to the same If Statement exercise. Looks like the original exercise was replaced with an updated version

1 Like

Technically, (and according to Community Guidelines) it’s suggested that users don’t revive old topics (those older than a few months).
Not all topics are self-closing, so it’s up to the community to not revive them.

1 Like

@lisalisaj I would love to agree with you and to be absolutely clear, it’s not like I am dumpster diving for old threads to revive.

This thread is directly linked to a current well-used course and recommended as something to help with the exercise:


https://www.codecademy.com/journeys/data-scientist-ml/paths/dsmlcj-22-data-science-foundations/tracks/dsmlcj-22-python-fundamentals-for-data-science-part-i/modules/dsf-python-control-flow-47867702-d252-41e9-9c8c-a882098501c4/lessons/python-control-flow/exercises/if-statement

Clearly within this thread, someone is coming by every few months and getting confused because what’s discussed here no longer apply to the course that appears to have been revamped. I don’t think I am causing issues by trying to point out the curriculums team should probably remove the link and update accordingly.

Report it to CS with details. Then it will get tracked in their system/CQR.

1 Like