First steps pure Python: objects, types and variables
Python is a dynamically strongly typed language. It is fundamental to understand the three notions of objects, types and variables.
Objects and types¶
We will first start with example without variables. The function type returns the type
of an object:
type("hello")strtype(2)inttype(2.0)floattype(2 + 2)inttype(2 + 2.0)floattype(True)boolWe see that there are objects ("hello" or 2.0) which live in the memory of the
computer. All object have a type (str and float in this case).
Variables and name spaces¶
Variables are just tags pointing towards objects. New variables can be used when needed, without declaration. In Python, they are commonly called names. Indeed, they are not associated with a type but only with an object (which has a type)... They can be thought as a tag attached to an object, or as an arrow pointing towards an object. We say that a variable is a reference of an object.
myvar = 1
print(myvar, type(myvar))1 <class 'int'>
With the assignment myvar = 1, the name myvar has been created and assigned to
the object 1.
myvar = "hello"
print(myvar, type(myvar))hello <class 'str'>
With the assignment myvar = "hello", the name myvar has been assigned to the
object "hello".
We will soon see that the names (the variables) live in name spaces.