2. Data type
type() is a built-in function. Its signature is:
It returns the type of the value you pass.
Common types:
int— whole numbers:10,-5,0float— decimal numbers:3.14,1.0str— text in quotes:"hello"bool—TrueorFalseNoneType— the type ofNone, which means "no value"
Printing a type shows it as <class 'int'>, <class 'str'> and so on.
Coming from JavaScript? JS has one number type; Python separates whole numbers (int) and decimals (float). None covers JS null and undefined, and type(x) prints a class instead of returning a string like typeof.
Integers
Whole numbers such as 10 are of type int. Pass the number to type() and print the result.
Print exactly:
Floats
Numbers with a decimal point, such as 3.14, are of type float. Pass one to type() and print the result.
Print exactly:
Strings
Text inside quotes, such as "hello", is of type str. Pass it to type() and print the result.
Print exactly:
Booleans
True and False are of type bool. Pass one to type() and print the result.
Print exactly:
None
None is a special value that means "no value". Its type is NoneType. Pass it to type() and print the result.
Print exactly:
Same text, different type
42 and "42" look similar, but one is a number and the other is text. Print the type of both in a single print() call.
Print exactly: