Do I need to be utilizing return or print?

If we use print instead of return why does the word none comes in the console?

I also have the same question.

In previous questions when i would type an if function and a return a string it would print to the console.

Now, in this question it seems like we need to add Print and run the function again?

E.G.
def greater_than(x,y):
if x>y:
return x
if y>x:
return y
if x==y:
return “These numbers are the same”
#New Function

def graduation_reqs(credits):
if credits >= 120:
return “You have enough credits to graduate!”

print(graduation_reqs(120))

A function will print to the console only if you explicitly call print().

  • We use print() when we want to see the data on the screen.

  • We use return when Python needs to have the data - to assign to a variable, perhaps, or pass to another function

In general, you’ll find that you use print() less and return more as you go on. (One exception is in development and debugging, where print() is invaluable.)

To view returned output, call the function within a print function, so, the famous “hello world” should be something like:

def hello(name):
    return "Hello " + name + "!"

print(hello("world"))
6 Likes

Ok. So return and then “Strng” will never print. Thanks!

Correct. The returned value can be assigned to a variable for future use, passed directly as an argument to another function (in the example above, the print() function), or used as one value within an expression. If the calling expression does nothing with the return value, it will be lost.

3 Likes

If you want to print - “These numbers are the same”
You have to type something like:

print(greater_than(2, 2))
1 Like

I don’t get it. Why can’t a print be part of the function?

it can be , but if you checked above reply by patrickd314 you will understand .
check below.
so yes in our exercises here sure you can use it but normally in real life apps you will depend more on return , unless your program is required to print something on screen once function is called.