Milandeep Bassi

Understanding Variables and Data Types in Python Programming

15th, January, 2020


Variables

Variables are the labels for data essentially. The data is then stored to this label. If you wanted a programme to remember someones name then when asking for the input you assign a variable to it. 

<variable name> = <data>

The syntax above is how you assign a variable to some data. The first time you assign data to a variable this is called declaration. Some examples variables are below.

name = "Code Easy"
age = 21

These variables can then be printed by using the print statement. 

>>> print (name)
Code Easy

Replacing the variable inside the print() function will change what is outputted by the interpreter. With variables, you can reassign the data type later in the code. For example if the age was reassigned later in the code to 22, The original value of 21 is essentially lost. An example of this is below.

>>> age = 21
>>> print (age)
21
>>> age = 22
>>> print (age)
22

We can also use these variables with mathematics operations. 

>>> age = 21
>>> age = age * 2
>>> print (age)
42

The code segment above simply doubles the age. Note how the age variable is now also reassigned to be 42. This is useful when calculating totals. We call code like this an expression. We essentially alter a variable by using the variable inside the expression, then storing the data back to the original variable. Variables in Python have data types. Data type refers to the type of data stored to a variable. We have Strings these are simply just words. There are also Integers these are whole numbers. Lastly we have Float these are numbers that involve decimals. When naming a variable it's wise to name it something relevant to the data it will store. We can also use the underscore (_) to name variables with longer names. Variables are also case sensitive, therefore Age and age are not the same variable.

Strings in python are sequences of characters. We can find the length of words in python using len() and we can also combine words in python using + between the words. You can also use indexes to select particular characters in a string. This is done by using [x] after a variable name. In Python numbering begins with 0. Ranges of characters can also be selected using [x:x]. 

>>> name = "Code"
>>> name = name + "Easy"
>>> print ( name )
Code Easy
>>> print ( name[0] )
C
>>> print ( name[1] )
o
>>> print ( name[2:7] )
de Ea

Note here that when selecting the range for the variable. The character of name[7] is not selected. 


Operations

Operators are symbols that represent computations such as addition and multiplication. Some of these operands that you can use are +, -, *, /, **. The symbols in python represent exactly what they represent in mathematics. The usage of ** is to illustrate raising a number to another (power).

Python follows the exact same rules as maths for its mathematical operations. Python works on the B.I.D.M.A.S order. This stands for Brackets, Indices, Division, Multiplication, Addition, Subtraction.

Mathematical operations do not work on strings in general. However the usage of multiplication and addition does work.

>>> name = "Code"
>>> name_second = "Easy"
>>> print ( name + name_second )
Code Easy
>>> name = "Code Easy" * 2
>>> print ( name )
Code Easy Code Easy