+ 1
Function argument
How do we choose the type (string, int, float) for the argument?? What is the default variable type in Python?
1 Respuesta
+ 7
You don't declare types in Python.
You define like this:
def f(x):
...
And you call like this:
f(5) or f('a') or whatever.
The literal determines the type.
x = 5 # x is an int now
x = 'z' # now x is a str
x = [1, 'v'] # now a list with stuff in it
A variable is not a 'container for a value' like in languages like C or something.
Every name is just a pointer to something that is created and destroyed in the background without you doing anything.
So x can be all sorts of types within the same program.