1.1 variables
Contents
Note
Click here to download the full example code or to run this example in your browser via Binder
1.1 variables#
This lesson introduces data types and variables in python.
A variable is a way to link some data to a memory location. The memory here, does not
mean the storage such as hard drive or USB etc. rather memory such as RAM.
The memory size which is allocated for a variable depends on the kind and
the amount of data linked to that variable. For example, a variable consisting 10 integers will
hold less memory as compared to a variable consisting of a million integers.
Similarly, a variable holding an integer (e.g. 92) will have different amount/size of memory
as compared to a variable holding a string say Ali
.
When we define a variable in python, the python checks the type of the variable
and allocates some memory for that variable.
a = 10
So what has been done above is that a variable named a
is assigned a value
of 10. Behind the scenes python created an object
and the variable
name a
is a reference for that object.
a = 20
When the variable a
is redefined, it means the location of memory which was
holding the value 10 before, now holds 20. This means we have changed the contents of memory.
30
The print
is a (builtin) function in python which we can use to print the value
of a variable. This is not always true but more about it will come in
1.11 print and 3.16 magic methods.
The kind of object python creates, depends upon the type of data. We can check
the type of a variable by using the command/function type(VariableName)
print(type(a))
<class 'int'>
A function
is a different creature in python. We will cover more about it later in
1.12 functions . At this point, just keep in mind that
when some object is a function, we can call by appending paranthesis ()
after its name
Above we have called the print
function. Don’t worry if you don’t understand the meaning
of calling a function at this point.
The function type
is the most important function in python. Whenver you don’t know about
some object in python, the first thing you should do is to check its type
using type(object)
<class 'float'>
Question
Both a
and b
contained the value thirty, then why they had different types?
60.0 <class 'float'>
60 <class 'int'>
It is important to note that the python is able to change the type of new variable based on the kind of value assigned to it. If a float value is assigned to a variable, python will change the type of this variable to float.
Another significant thing is that, we can assign any type of data to a variable.
For example, we can assign int
to variable a
, later we can assign float
to the variable a
and then we can assign a completely different type like string
to the variable a
.
This is a blessing (in terms of ease of use) as well as a curse (in terms of its slow speed) of python and for python users.
<class 'str'>
Question
Find out 14 different types
in python. We have already seen three types above
i.e. str
, int
and float
.
When we assign a value to a variable and then assign that variable to a new
variable, then both of these variables actually refer to the same object.
We can verify this using the function id(VariableName)
(140715684653712, 140715684653712)
(140715684653712, 140715684653776)
So when we assigned a different data to b
, a new object was created and now b
refers to this new object and thus its identity changes now.
Following are valid variable names
2
A variable name must not start from a number.
# uncomment following line
# 1_ali = 29
We can not name certain keywords as variable names. These keywords can be seen official python docs website 1
Data Types#
Data types signifies the type of operation that can be performed on that data. Python has the following data types
Numbers#
- To represent numerical values, python has three types
integer
float
complex
1 <class 'int'>
5 <class 'int'>
12 <class 'int'>
14 <class 'int'>
print(bin(5))
print(oct(12))
print(hex(14))
0b101
0o14
0xe
<class 'float'>
<class 'complex'>
<class 'bool'>
Total running time of the script: ( 0 minutes 0.007 seconds)