Hi everybody, this is a small tutorial I want to show you before introducing arrays in Python because we will use it in the next tutorial.
To put it simply, we can think of a variable as a name of a value which can be a number, string, list, tuple, or even dictionary. In fact, every time when we declare a variable, the interpreter will allocate memory for its value.
A value can be assigned to a variable by using equal sign = and it can be any type of data such as number, string, or array data type in Python (list, tuple, and dictionary).
>>> a = 8 >>> b = 9.0 >>> c = float(a) >>> print c >>> 8.0 >>> d = 'Hello ' >>> e = 'Adele'
To access a value from a string or number type variable, we simply call and perform operations with its name.
>>> s = a + b >>> print s >>> 17.0 >>> st = d + e >>> print st >>> Hello Adele
See that, whatever you can do with 2 numbers or 2 strings, you can also do with 2 variables which stores 2 numbers or 2 strings.
However, it is different when accessing value from array data types in Python. In the next tutorial, I will introduce to you how to deal with it.
Sometimes, you may make a long code in your project and it will be tough to work with it again after a long time if you don’t note the meaning of parts in your program. That’s why we need to use comments in our program. A comment is made by putting a hash mark # before a message.
>>> theta = 5.0 # initializing theta >>> bias = 1.3 # initializing bias >>> x = 2 # initializing variable >>> y = theta*x + bias # estimating the model
Although all comments are ignored by Python interpreter, it is an crucial part in a Python program. The more you make good comments in your code, the more you save your time.
That’s all. Is it easy?
In the next tutorial I will introduce array data types used in Python. Well, it will be a long tutorial and you will have a lot of things to do.
Hope you enjoy it,