Python Forum
Difference between return and print
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Difference between return and print
#9
DragonG Wrote:that is the difference between return and print, explained so that a beginner can understand it?
In example under gone look at some basic usage of function and return,this can make it clearer what return is good for.
In read_file() we don't want to print the result,but return it out for further progressing.
Same with check_gender(read_file) it take in read_file do work and return result.
In result(gender) when all is finish then print out result.

If look at code you see small task function(like black box isolated for outside world),
that take in a argument and return out the result.
This make code easier to read and test,than writing this same code within functions.
def read_file():
    try:
        with open("names.txt", encoding='utf-8') as f:
            return [i.strip() for i in f]
    except (OSError, IOError) as error:
        return f'Error reading file: {error}'

def check_gender(read_file):
    '''Make male and female list output'''
    male = []
    female = []
    for name in read_file:
        if name.lower().startswith('male'):
            male.append(name.split(' ', 1)[1])
        else:
            female.append(name.split(' ', 1)[1])
    return male, female

def result(gender):
    '''Show table of Male Female'''
    male, female = gender
    titles = ['Male', 'Female']
    print('{:>8}{:>15}'.format(*titles))
    print('-'*27)
    for item in zip(male, female):
        print('{:15}{:^10}'.format(*item))

if __name__ == '__main__':
    if 'Error' in read_file():
        print(read_file())
    else:
        gender = check_gender(read_file())
        result(gender)
Output:
Male Female --------------------------- Kent Hollow Emily Hunt Hans Klein Olivia Stock
Input names.txt:
Male Kent Hollow
Female Emily Hunt
Male Hans Klein
Female Olivia Stock 
You need Python 3.6 --> to run this code,so do not use Python 2 as you use in first post Wink
Reply


Messages In This Thread
Difference between return and print - by DragonG - Oct-24-2018, 05:56 PM
RE: Difference between return and print - by snippsat - Oct-25-2018, 09:56 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  What a difference print() makes Mark17 2 619 Oct-20-2023, 10:24 PM
Last Post: DeaD_EyE
  return vs. print in nested function example Mark17 4 1,818 Jan-04-2022, 06:02 PM
Last Post: jefsummers
  output while using return instead of print muza 2 2,181 Apr-23-2020, 09:38 AM
Last Post: muza
  Error with print and return commands TheDark_Knight 2 1,997 Jan-15-2020, 04:59 PM
Last Post: TheDark_Knight
  Embedding return in a print statement Tapster 3 2,342 Oct-07-2019, 03:10 PM
Last Post: Tapster
  print 3 return values from function bluefrog 5 5,313 Mar-05-2017, 07:58 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020