Programmer
python programming
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hi there @jamshed2019,
In Python, the print function outputs the given contents to the console window.
For example, if you were to run:
my_str = "Hello world"
print(my_str)
You would expect the output in the console to be:
OutputHello world
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:
OutputHello world
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! - Matt.
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. For example:
def add(p,q): sum=p+q; return(sum)
n1=input(“Enter value of a :”) n2=input(“Enter value of b :”) num1=int(n1) num2=int(n2) result=add(num1,num2) print(“sum of 2 nos:”,result)
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.