diff --git a/native-datatypes.html b/native-datatypes.html index 8fa0ba0..5893d58 100644 --- a/native-datatypes.html +++ b/native-datatypes.html @@ -20,7 +20,7 @@ body{counter-reset:h1 2}
Cast aside your first Python program for just a minute, and let’s talk about datatypes. In Python, every variable has a datatype, but you don’t need to declare it explicitly. Based on each variable’s original assignment, Python figures out what type it is and keeps tracks of that internally. +
Cast aside your first Python program for just a minute, and let’s talk about datatypes. In Python, every value has a datatype, but you don’t need to declare the datatype of variables. How does that work? Based on each variable’s original assignment, Python figures out what type it is and keeps tracks of that internally.
Python has many native datatypes. Here are the important ones:
True or False.
@@ -59,14 +59,17 @@ body{counter-reset:h1 2}
>>> type(1) ① <class 'int'> ->>> 1 + 1 ② +>>> isinstance(1, int) ② +True +>>> 1 + 1 ③ 2 ->>> 1 + 1.0 ③ +>>> 1 + 1.0 ④ 2.0 >>> type(2.0) <class 'float'>
type() function to check the type of any value or variable. As you might expect, 1 is an int.
+isinstance() function to check whether a value or variable is of a given type.
int to an int yields an int.
int to a float yields a float. Python coerces the int into a float to perform the addition, then returns a float as the result.