+ 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
2 ответов
+ 2
thanks! I think seeing it written out like this helps to stick it in my brain lol
+ 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")