DEV Community

Wassim TOUIR
Wassim TOUIR

Posted on

Finally, the difference between print() and return just clicked for me

This is one of the most important distinctions in Python, and beginners often get confused.
When I started I kept using print() everywhere, it's true that my code worked but sometimes I was not getting the results I was expecting.
I thought they were the same because they both give back a result until I run this code:

`def add(a, b):
print(a + b)

result = add(2, 3) #output: 5
print(result) #output: None(I expected to be 5)`

I searched on the web and what I learned is that:

  • print() displays something on the screen
  • return sends a value back from a function so we can use it elsewhere and we can assign it to any variable

I’m still early in my Python journey, but writing this helped me clarify my own thinking. If you find my post helpful comment down bellow your struggles with Python, I’d love to hear what confused you the most at the beginning!

Top comments (0)