Skip to content
Advertisement

What is the difference between a variable and a parameter

I am learning python 3 and programming in general for the first time, but I can’t seem to distinguish a parameter and a variable?

Advertisement

Answer

A variable is just something that refers/points to some data you have.

x = 5

Here x is a variable. Variables can point to more kinds of data than just numbers, though. They can point to strings, functions, etc.

A parameter is something that is passed into a function

def my_function(y):
    print(y)

Here y is a parameter. It doesn’t contain a value yet. But if I want to call the function, I need to provide an argument to the function.

An argument is the actual value you provide to the function that replaces the parameter.

my_function(5)

Here, 5 is the argument. Of course, since x points to the value “5”, I can do this too:

my_function(x)

which also prints 5

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement