Python Forum
Two Questions, *args and //=
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Two Questions, *args and //=
#1
Hello,

Question one:
I want to use *args function, for multiple argument in math operation.
For example:

def plus(a,b):
    return a+b

#lets try

a=int(input("A: "))
b=int(input("B: "))
plus(a,b)
Its looking good but I want to use more arguments,not just a or not just b, add as many as you want

def plus(*args):
    return ?????
# am I think right or am I doing wrong :), what Can I do ?
And here is different easy question,

a=10
b=5

b//=a
print(b)

#now b = 0, why? whats this operator mean "//=" and in which situations it is used ?
Thank you ...
Reply
#2
*args allows you to have an unknown number of arguments and stores them in a tuple. Run this and see.

def show_args (x, *args) :
	print (f'x equals {x}')
	print (f'args equals {args}')

show_args (1, 3, 7, 13)
Output:
x equals 1 args equals (3, 7, 13)
// is called floor division and rounds the result of the division down to the nearest whole number.

print (13 / 2) # answer -> 6.5
print (13 // 2) # answer -> 6
Reply
#3
// is floor division.
//= is in-place version of floor division i.e. augmented assignment expression

b //= a
is the same as

b = b // a
PEP 577 Augmented assignment expressions
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
Assuming you are not allowed to use sum(), you can iterate over args and calculate the result adding one number at a time.
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
#5
def func(*args):
    print(args)

func('Hello')
func('a', 1, 3.14)

def func2(a, b, *args):
    print(args)

func2('a', 1, 3.14)
Output:
('Hello',) ('a', 1, 3.14) (3.14,)
As you can see, args is a tuple that contains all the unbound position arguments. For your plus function you could do something like this:
def plus(*args):
    total = 0
    for arg in args:
        total += arg
    return total

print(plus(1, 2, 3, 4, 5))
Output:
15
Of course you would be better off using the sum() function which takes any number of arguments.

// is "floor" division. This is often mistakenly called integer division, but it works for integers and floats. In floor division you are guaranteed to not have a fraction in the result.
4//3 == 1, not 1.33333
5.0 // 2 == 2.0, not 2.5

Floor division has a lot of uses. You can use it to make change.
total = 3.68
dollars = total // 1
total -= dollars
quarters = total // 0.25
total -= quarters * 0.25
dimes = total // 0.1
total -= dimes * 0.1
nickels = total // 0.05
total = total - nickels * 0.05
pennies = total // 0.01
print(dollars, quarters, dimes, nickels, pennies)
Floor division is often used with modulo to get the remainder. There is even a function that combines the two; divmod()
total = 3.68
total = int(total * 100)
dollars, total = divmod(total, 100)
quarters, total = divmod(total, 25)
dimes, total = divmod(total, 10)
nickels, pennies = divmod(total, 5)
print(dollars, quarters, dimes, nickels, pennies)
Reply
#6
Thanks everyone...

(Jan-31-2021, 09:31 PM)deanhystad Wrote:
def func(*args):
    print(args)

func('Hello')
func('a', 1, 3.14)

def func2(a, b, *args):
    print(args)

func2('a', 1, 3.14)
Output:
('Hello',) ('a', 1, 3.14) (3.14,)
As you can see, args is a tuple that contains all the unbound position arguments. For your plus function you could do something like this:
def plus(*args):
    total = 0
    for arg in args:
        total += arg
    return total

print(plus(1, 2, 3, 4, 5))
Output:
15
Of course you would be better off using the sum() function which takes any number of arguments.

// is "floor" division. This is often mistakenly called integer division, but it works for integers and floats. In floor division you are guaranteed to not have a fraction in the result.
4//3 == 1, not 1.33333
5.0 // 2 == 2.0, not 2.5

Floor division has a lot of uses. You can use it to make change.
total = 3.68
dollars = total // 1
total -= dollars
quarters = total // 0.25
total -= quarters * 0.25
dimes = total // 0.1
total -= dimes * 0.1
nickels = total // 0.05
total = total - nickels * 0.05
pennies = total // 0.01
print(dollars, quarters, dimes, nickels, pennies)
Floor division is often used with modulo to get the remainder. There is even a function that combines the two; divmod()
total = 3.68
total = int(total * 100)
dollars, total = divmod(total, 100)
quarters, total = divmod(total, 25)
dimes, total = divmod(total, 10)
nickels, pennies = divmod(total, 5)
print(dollars, quarters, dimes, nickels, pennies)

Thanks, what can we do If we want users give us this arguments?
I want to use input function but also I want to use args

#this is normal type example, can you write again with args?
def plusplus():
    toplam=0
    while True:
        print("To exit press Q")
        a = (input("Number: "))
        if a=="q":
            break
        else:
            a=int(a)
            toplam+=a

        print(toplam)
    print("Toplama Tamamlandı")

plusplus()
Sorry for my weird questions, I am just trying to learn
Reply
#7
(Jan-31-2021, 09:16 PM)buran Wrote: Assuming you are not allowed to use sum(), you can iterate over args and calculate the result adding one number at a time.

Thanks, can you give me a little example which have "sum()" function in it
or you can add sum() in my example,
Reply
#8
Try looking it up. Looking things up, and more importantly, learning where to look will be more useful than somebody telling you how to do things.
beginner721 likes this post
Reply
#9
(Jan-31-2021, 11:19 PM)beginner721 Wrote: can you give me a little example which have "sum()" function in it
def plus(*args):
    return sum(args)

print(plus(1, 23, 4)) 
print(plus(*range(10)))
Output:
28 45
Basically this is just obfuscation of sum(), so I would not advise to do it for any reason other than pure learning
beginner721 likes this post
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


Possibly Related Threads…
Thread Author Replies Views Last Post
Question How to compare two parameters in a function that has *args? Milan 4 1,259 Mar-26-2023, 07:43 PM
Last Post: Milan
  *args implementation and clarification about tuple status amjass12 10 3,997 Jul-07-2021, 10:29 AM
Last Post: amjass12
  [SOLVED] Good way to handle input args? Winfried 2 2,055 May-18-2021, 07:33 PM
Last Post: Winfried
  does yield support variable args? Skaperen 0 1,669 Mar-03-2020, 02:44 AM
Last Post: Skaperen
  is there a way: repeat key word args Skaperen 2 2,231 Feb-03-2020, 06:03 PM
Last Post: Skaperen
  Using function *args to multiply multiple arguments allusernametaken 8 6,062 Nov-20-2019, 12:01 AM
Last Post: allusernametaken
  Passing string args to Popen CardBoy 3 4,239 Jan-16-2018, 09:22 AM
Last Post: Gribouillis
  Why args type is always tuple, when passed it as argument to the function. praveena 5 5,317 Jan-16-2018, 09:07 AM
Last Post: praveena
  Discord bot that asks questions and based on response answers or asks more questions absinthium 1 38,544 Nov-25-2017, 06:21 AM
Last Post: heiner55
  subprocess with args haye 0 4,421 Oct-23-2017, 10:54 AM
Last Post: haye

Forum Jump:

User Panel Messages

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