+ 1

Function argument

How do we choose the type (string, int, float) for the argument?? What is the default variable type in Python?

8th Jan 2020, 10:39 PM
Bao Vo
Bao Vo - avatar
1 ответ
+ 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.
8th Jan 2020, 10:58 PM
HonFu
HonFu - avatar