+ 2
im confused
one of my lessons on python was talking about a new command called "type" means. for axaple (i took it out of a lesson) balance = "780" type(balance) what does that mean? also i dont understand what "input" means coding wise
6 ответов
+ 4
Hi print ("JUMP_LINK__&&__python__&&__JUMP_LINK"),
(1) type: It shows the kind of data, like “str” for text or “int” for numbers.
(2) input: It asks the user to type something and saves it in your code.
+ 4
type() will returns the type of that value is string, integer, float or boolean,
The code up there will return "str" means string, because with quotation marks (", ') it's now a "str", but if it's not, then 780 is now a number, so the output is int (integer), now if you add .0 after 780, which is now 780.0, it's a float
Note that integer for whole numbers and float for decimal numbers
For input, it's ask for user like their names, etc
note again that the user input, when use it with type(), or:
age = input()
print(type(age))
will always a string, so if you want it to be an integer or float, then use int() or float() for decimal
the code will looks like:
age = int(input())
print(type(age))
+ 2
print ("JUMP_LINK__&&__python__&&__JUMP_LINK") there's code behind the type() function, which someone else wrote so that future users could use it easily, like input() or print().
The code behind it must run some kind of test to evaluate the variable it is told to check.
+ 2
print ("JUMP_LINK__&&__python__&&__JUMP_LINK") ,
here is a short description (simplified) that may help you:
when we create a variable in python by assigning a value to it, not only this value is stored in the object (instance of a class), but also some metadata like the object type. one field of this metadata is `__class__`.
type() can read the content of this field.
> how python can detect the type of the data that we assign:
when creating an object, python can detect a values type in different ways. it can be done by using :
> literals in the code like 42 or 3.24 or 'hello'.
> elements like brackets () or [] or {}
> constructors like: int('42')
>...
+ 1
Thanks for the iput
But how does type know what the type of thing is...im confused
0
why does this link take me to a not python code???