Python Forum
question about python3 print function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
question about python3 print function
#1
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)
Reply
#2
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
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
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
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#5
@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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
(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.
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  print doesnt work in a function ony 2 233 Mar-11-2024, 12:42 PM
Last Post: Pedroski55
  Question on dir() function Soorya25 1 1,109 Jan-16-2023, 09:33 PM
Last Post: deanhystad
  How to print variables in function? samuelbachorik 3 851 Dec-31-2022, 11:12 PM
Last Post: stevendaprano
  Question: print issue python202209 3 922 Sep-18-2022, 11:51 AM
Last Post: jefsummers
  How to print the output of a defined function bshoushtarian 4 1,237 Sep-08-2022, 01:44 PM
Last Post: deanhystad
  Function not scriptable: Noob question kaega2 3 1,136 Aug-21-2022, 04:37 PM
Last Post: kaega2
  input function question barryjo 12 2,637 Jan-18-2022, 12:11 AM
Last Post: barryjo
  Why does absence of print command outputs quotes in function? Mark17 2 1,342 Jan-04-2022, 07:08 PM
Last Post: ndc85430
  return vs. print in nested function example Mark17 4 1,674 Jan-04-2022, 06:02 PM
Last Post: jefsummers
Exclamation question about input, while loop, then print jamie_01 5 2,616 Sep-30-2021, 12:46 PM
Last Post: Underscore

Forum Jump:

User Panel Messages

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