Matt Hines Development

Matt Hines.
Software Engineering Student.
London.
This blog is mainly to display my different projects, as well as a place for me to share and discuss technology news.

Recent Tweets @MattDevUK

rmtsoftware:

In every programming languages you use data types to store your data/information. As an easy example we will create an “Array”. An array is a data type in most programming languages that stores information sequentially. For a reason that I do not know python calls arrays “lists” and other languages like Java call them arrays but also have lists which is another sort of abstract data type. The way to create an array is by using the square brackets “[]”. Inside the square brackets you put your information. 

Example: 

testArray = [“hello”,”this”,”is”,”a”,”test”]

This code snippet creates an array of length 5 (5 words). Each word is separated by a comma. You are not limited to strings in array you can add other data types like integers or doubles in them as well.You can even put different types inside an array, as an example, 2 strings and an int. If you are feeling even more adventurous you can even put an array inside an array. This is called “Nested Arrays”. Arrays are a data type that is used quite often. 

The way to access an element in an array is by something called indexing.

print testArray[2]  

This will print out the word “is”. A common question is Why does it print out “is” and not “this” since “this” is the second element in the list. When it comes to indexing, python uses 0 based counting (Like every programming language). So if you want to access the first element in the list you have to type:

print testArray[0]

To add an element in the array you use the “append” method of the array.Example:

testArray.append(“new Element”)

This will add it to the end of the list, if you want to add anywhere in the list you have to use the “insert” method.

testArray.insert(2,”new element”)

The 2 is the position in where I would like to insert my element. Again this uses 0-based counting.

Removing an element if you cannot already guess is the “remove” method. All you have to do is supply the element in which you want to be removed. This method will give an error if the element is not already in the list.

testArray.remove(“new element”)

There are other useful method that you can use like “count” which returns the number of elements in the array, and “sort” which will sort the elements in the array.

If for what ever reason you are looking to create an array of integers from 1-40. instead of creating the array, and starting from 1 adding all the numbers into the array, python gives you a easy way of doing it. the method is called “range”

print range(1,40)

This will display an array with the values from 1-40. If you add a 3 parameter you can even put an increment value.

print range(1,40, 2) 

This will display an array with the values from 1-40 skipping every other number.

These is just one of the data types that python has to offer. If you have any questions about arrays or want to learn more, please don’t hesitate to ask.

  1. mattdevuk reblogged this from rmtsoftware
  2. rmtsoftware posted this