Python Forum
question about python3 print function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: question about python3 print function (/thread-27070.html)



question about python3 print function - jamie_01 - May-25-2020

Hey all i've started with python 3 after using C for more than 2 years. Quick question, are the parenthesis normal after running this script? How would I eliminate them? Thanks?

#!/usr/bin/python3

def first_three_multiples (num):
    times_1 = num * 1
    times_2 = num * 2
    times_3 = num * 3

    return times_1, times_2, times_3

print (first_three_multiples (7))    # output 7, 14, 21
Program output:

(7, 14, 21)



RE: question about python3 print function - menator01 - May-25-2020

You could do it this way. It will unpack the tuple.
def multi(num):
...   times1 = num *1
...   times2 = num *2
...   times3 = num * 3
...   return times1, times2, times3
print(*multi(7))
Output:
7 14 21



RE: question about python3 print function - buran - May-25-2020

Your function returns tuple - it's the comma that makes a tuple, the parenthesis are optional, except in certain cases (the docs).
So you print tuple - the parenthesis are displayed (to indicate it's a tuple).
you can unpack as @menator01 show, use extra arguments in print function, or use str.join() or string formatting
print(', '.join(str(num) for num in first_three_multiples(7)))
print('{}, {}, {}'.format(*first_three_multiples(7)))
Note that you can write function better, e.g. one example:
def first_three_multiples(num):
    return [num * multiple for multiple in range(1, 4)] # this is list, not tuple like in your code
also have a look at PEP8 regarding style - e.g. you have some extra spaces where not required/not recommended


RE: question about python3 print function - pyzyx3qwerty - May-25-2020

You could also do this
 
def first_three_multiples (num):
    times_1 = num * 1
    times_2 = num * 2
    times_3 = num * 3
 
    print (times_1, times_2, times_3)
 
first_three_multiples (7)    
output:
Output:
7 14 21



RE: question about python3 print function - buran - May-25-2020

@pyzyx3qwerty, normally it's better that the function return and then you print the output. You can reuse the function, print as you like, use values later in code, etc.
If you print inside the function you cannot do anything with values - just print them and you cannot reuse the function in other code that does not need to print to stdout


RE: question about python3 print function - pyzyx3qwerty - May-25-2020

(May-25-2020, 09:25 AM)buran Wrote: @pyzyx3qwerty, normally it's better that the function return and then you print the output. You can reuse the function, print as you like, use values later in code, etc.
If you print inside the function you cannot do anything with values - just print them and you cannot reuse the function in other code that does not need to print to stdout
I am aware of that indeed, @buran , and I also prefer using return inside the functions most of the time.
However, OP just wanted to print all three multiples, and therefore, i showed him that way.