-
Notifications
You must be signed in to change notification settings - Fork 6
Python Basics
Assigning variables is one of the most fundamental processes in any programming language. By assigning a piece of data (such as a number, a string or an array) to a variable you are saving it to memory space, and providing a method to recall that data by using the variable name. Without assigning data to variables, in almost all cases, the data will not be stored and will be irretrievable.
Lets start with a simple case, assigning a few natural numbers to variables named using letters. The variable is opened by asserting it in plain text at the beginning of the line, the data is then assigned using the = operator followed by the number we want.
a = 3
b = 4
c = 5Notice how at no point during variable declaration are strict data-types enforced. This is because Python makes use of dynamic data-typing, a core principal the programming language is built about.
However, if you do need to ensure that the data assigned to a variable is stored as a specific data-type then there are native function in the global namespace to allow you to do this. When dealing with numbers there are 4 native data-types in Python, these being integer, float, long and complex. For this tutorial however, we will only deal with integers and floats.
a = int(3)
b = float(4)This has assigned 3 to a as an integer and 4 to b as a float.
Now we have some variables defined we can start to use them in some operations. Let us start with some basic addition, which is performed using the addition operator +.
print(a+b)
>>> 7
print(b+c)
>>> 9In the example above the print() function has been utilised to return the result of the operation to the console. If you wished to store the result in another variable rather than returning it this can be done by treating the operation as the data and assigning it as shown before.
A = a+b
print(A)
>>> 7Of course we need more numerical operators than just addition and these are provided. Subtraction, multiplication and division are shown respectively below, using the print function to return the result directly to the console.
print(b-a)
>>> 1
print(a*b)
>>> 12
print(a/3)
>>> 1.0Numeric operations can be combined in a single line to construct more complex equations, an important note at this point is that Python follows standard arithmetic ordering. A combination of several numerical operators is demonstrated.
print(a+b*c)
>>> 24
print(a*b+c)
>>> 17
print(a*b/c)
>>> 2.4It is also possible to utilise exponents.
print(a**b)
>>> 81Assigning single pieces of data to variables is useful for computing equations, but often we find ourselves dealing with large data sets where it would not be continent to store every element in a unique variable. In these cases python has a number of data-types to allow for collections of data to be stored in a single variable. The most often used data-type is a list. Lists have many properties that make them the favorable data-type when dealing with collections of data, the first of these is that the order the data is stored (or indexed) is both ordered and changeable. Additionally lists can be multi-dimensional, allowing for layers of data to be stored. Because Python is a proper, grown up programming language indexes start at 0, so the first element in a list is accessed using the index 0.
Let us construct a list of the positive single digit natural numbers. This is done by opening the variable you wish to assign the list to in the same way as you would for a number. The list data-type is called by placing square brackets are the values forming the collection, each element separated with a comma.
A = [1,2,3,4,5,6,7,8,9]Individual elements in the list can be accessed by placing square brackets after the variable name and putting the index of the element you want in those brackets. You can also use negative numbers to access elements, but this reverses the order of the list. Therefore, index -1 would return the final element of your list, -2 the penultimate element, etc.
print(A[0])
>>> 1
print(A[5])
>>> 6
print(A[-1])
>>> 9Lists and arrays both have a number of in-built methods which allow for simple operations to be performed upon themselves. These methods are accessed by placing a . after the name of the variable containing your list or array, then by putting the name of the method you wish to access, followed by parentheses which in some cases contain addition arguments. This tutorial will not contain a complete list of all methods, but rather take a look at some of the most useful ones.
The first method we shall examine is append(). This allows us to add a new item to the end of a list or array. It is important to note that by using methods it is not necessary to assign a new variable to the operation, the action will be directly performed on the dataset already contained within the variable specified.
A.append(10)
print(A)
>>> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]Next, the copy() method is shown. Whilst this method might seem redundant it does have an important role, this is because directly assigning a new variable name to an existing list (i.e. B = A) returns a reference to the original. Hence, if the original list is altered the list in the new variable will also be changed. Therefore, the copy method is utilised to return a shallow copy of the original list.
B = A.copy()
print(B)
>>> [1, 2, 3, 4, 5, 6, 7, 8, 9]To remove items from your list or array a couple of different methods are provided. The pop() method removes an item by specifying its index and returns it. Alternatively you can choose to use the method without specifying an index, in which case the final item from you dataset shall be removed and returned.
A.pop(4)
>>> 5
A.pop()
>>> 9
print(A)
>>> [1, 2, 3, 4, 6, 7, 8]Alternatively, the remove() method is provided in order to remove items from a dataset by their values. The method takes a value as a argument and will remove the first element from a list or array with that value.
A.remove(4)
print(A)
>>>[1, 2, 3, 5, 6, 7, 8, 9]Sub-collections of data can be returned from the larger data set by slicing the list. This is done by placing a range in the square brackets next to the variables with a colon, returning a list containing the values from the first index to the, but not including, final index of the range. Leaving the value before the colon empty will return all values up to the final indicated index and leaving the value empty after the colon returns all values after the index. Just putting a colon will act as a wildcard and return all values.
print(A[3:5])
>>> [4, 5]
print(A[:4])
>>> [1, 2, 3, 4]
print(A[5:])
>>> [6, 7, 8, 9]-Under Construction-