Posts: 8
Threads: 3
Joined: Jan 2021
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 ...
Posts: 379
Threads: 2
Joined: Jan 2021
Jan-31-2021, 09:06 PM
(This post was last modified: Jan-31-2021, 09:08 PM by BashBedlam.)
*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
Posts: 8,162
Threads: 160
Joined: Sep 2016
Jan-31-2021, 09:13 PM
(This post was last modified: Jan-31-2021, 09:13 PM by buran.)
// 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
Posts: 8,162
Threads: 160
Joined: Sep 2016
Assuming you are not allowed to use sum() , you can iterate over args and calculate the result adding one number at a time.
Posts: 6,802
Threads: 20
Joined: Feb 2020
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)
Posts: 8
Threads: 3
Joined: Jan 2021
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
Posts: 8
Threads: 3
Joined: Jan 2021
(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,
Posts: 6,802
Threads: 20
Joined: Feb 2020
Feb-01-2021, 02:54 AM
(This post was last modified: Feb-01-2021, 02:54 AM by deanhystad.)
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
Posts: 8,162
Threads: 160
Joined: Sep 2016
Feb-01-2021, 09:11 AM
(This post was last modified: Feb-01-2021, 09:11 AM by buran.)
(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
|