Print() or return()? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Print() or return()?

When implementing a function in your code how do you know when to call a print statement or return statement? For some reason this is a little confusing for me

9th Mar 2017, 8:23 PM
Aaron Nelson
Aaron Nelson - avatar
2 Answers
+ 2
thanks! I think seeing it written out like this helps to stick it in my brain lol
9th Mar 2017, 8:41 PM
Aaron Nelson
Aaron Nelson - avatar
+ 1
I use print inside a function when it job is to print something, for example: def print_3_lines(): print(end = '\n\n\n') when the function does some calculation i use the return, for example: from math import log def is_power_of_2(n): """ return True if n is a power of 2, False otherwise """ return log(n, 2) == int(log(n, 2)) example of use: number = int(input("insert a number: ")) print_3_lines() if is_power_of_2(number): print(number, "is a power of 2") else: print(number, "isn't a power of 2")
9th Mar 2017, 8:36 PM
Deep Serket
Deep Serket - avatar