What are "arguments" in python [closed]

I am new to python and I am reading an online book. There is a chapter which explains the arguments of what they are and when they are used but I don't understand the explanations that well. Can anyone explain better what arguments are?
And please try to explain as simple as you can, because I am a beginner and English is not my native language

5

2 Answers

An argument is simply a value provided to a function when you call it:

x = foo( 3 ) # 3 is the argument for foo
y = bar( 4, "str" ) # 4 and "str" are the two arguments for bar

Arguments are usually contrasted with parameters, which are names used to specify what arguments a function will need when it is called. When a function is called, each parameter is assigned one of the argument values.

# foo has two named parameters, x and y
def foo ( x, y ): return x + y
z = foo( 3, 6 )

foo is given two arguments, 3 and 6. The first argument is assigned to the first parameter, x. The second argument is assigned to the second parameter, y.

1

Python functions have to kinds of parameters.
args (arguments) and kwargs (keyword arguments) args are required parameters, while kwargs have default values set

The following function takes arg 'foo' and kwarg 'bar'

def hello_world(foo, bar='bye'): print(foo) print(bar)

This is how you can call the function

>>> hello_world('hello')
hello
>>> hello_world('hello', bar='cya')
hello
cya
3

You Might Also Like