what is different between return and print statement.?
Hi there @jamshed2019,
In Python, the
For example, if you were to run:
my_str = "Hello world" print(my_str)
You would expect the output in the console to be:
This is different from the
return keyword, which is used within functions to terminate the function and return a value to whatever called the function initially.
For example, if you run:
def get_hello_world(): part_one = "Hello" part_two = "world" return part_one + " " + part_two my_str = get_hello_world() print(my_str)
You would expect the output in the console to then be:
What happened here is that the function
get_hello_world() was defined, which the variable
my_str then called. The value
"Hello world" was returned by the function and assigned to the variable. The contents of the variable were then printed as we showed in the previous example.
I highly recommend the tutorial series that we have published on How to Code in Python3 as it covers concepts like this and much more!
Hope that helps in understanding the difference between the two!
Print() is a function that is used to print a value, string or characters on the console. If you want to show value then use the print() function.
Return statement returns control to the calling function and terminates the execution of a function. In other words, when we reached to return statement, Python will stop the execution of the current function, send value to where the function was called.
n1=input(“Enter value of a :”)
n2=input(“Enter value of b :”)
print(“sum of 2 nos:”,result)