Basically, Variables are memory space on computer reserved to store values. Based on the datatype of the value assigned to the variable interpreter allocates the memory. We don't have to worry about specifying the datatype of the variable when you initialize it or declare it, interpreter will automatically interpret it when we assign the values to the variable.
Below are the different numerical datatypes python supports
- int (eg: 69, 49)
- float (eg: 3.14)
- complex (eg: 3+4j)
As you can see below, I have declared 3 variables of 3 different datatypes without specifying them explicitly and python interpreter identifies, manages and prints it beautifully.
What I have done below is, I have assigned values of different datatypes one after another to a same variable and printed it out. Python beautifully manages it and overrides the values of a as you keep reassigning it with different values of different datatype one after other, which is not possible in most of other programming languages.
Strings and its manipulation
Python identifies content within double or single quotes as String. Using slice operator ( and [:]) we can take subset of strings with index 0 representing the beginning of character and -1 representing last character in String. Try out the below example.
|a[0:1]||Tells interpreter to select the character at 0th index to character before the first index, which is H.|
|a[0:2]||Tells interpreter to select the character from 0th index till character before the second index which is He.|
|a[-1]||Tells interpreter to select the last character in the string which is o (Note: you could use -1,-2,-3... to acces list elements from backwards).|
|a[:]||Tells interpreter to select all the characters in the String Hello.|
|a[2:]||Tells interpreter to select all the chars in the string starting from char at index 2, which will give us output llo.|
|a[1:-1]||Tells interpreter to select characters from 1st index till character before the last index, which is ell.|
List in python can contain items of any datatype separated by comma(,) and enclosed on both sides by a square brackets([ ]). You could access the content of the elements in the list using the same slice operator we have used to manipulate the string elements above. Let us take a list a = [1,2,3,4,5,3,2,1].
|len(a)||Returns the total number of items in the list in our case, it is 8.|
|max(a)||Returns the item max value in the list that has . In order for this to work all the items in the list should be of same datatype.|
|min(a)||Returns the min value in the list and a condition is, all the items in the list should be of same datatype.|
Below is the list of methods for manipulating list.
|a.append(5)||append function appends 5 to the end of the list.|
|a.count(3)||count function returns number of times value 3 present in the list.|
|a.index(3)||Returns the index of value 3 appearing first in the list.|
|a.insert(2,20)||Insert function takes 2 arguments first is the index to insert and second is the value to insert. In our case we inserted value 20 at index 2.|
|a.pop(-1)||pop function removes the element specified in the index from the list. Here the last element in the index have been removed.|
|a.remove(3)||remove function removes first occurrence of element specified as argument from our list and in this case it is 3.|
|a.reverse()||Simply reversers the contents in the list.|
List as multidimensional array
And finally, beauty of this list is its freedom to create multi-dimensional array at ease. Below we have created a 3 dimensional array using list and we can access all the elements of the array using typical indexing method we use to access list elements.
In the next article we will discuss about Tuples, dictionaries and sets.