Below is a quick overview of key Python functions. ‘….’ is used to show indentation.
Single line comment
Working With Variables
To define variables in Python, simply type the variable name, the equals sign & then define it’s value
In Python, we can check an object type by using functions such as the below. We can use the isinstance function to help us handle & avoid type errors (e.g. if result = false then cast as int).
So in our above example, if the result is false & the variable is not an int, we can cast it as such. Let’s change our code a bit and create MyNumber as a string (by adding apostrophies). In this instance, our isinstance function returns false. So, we cast that value to be int & then run the test again – this time it returns true.
You may well be asking ‘when would this ever be an issue’. Well, let me demonstrate:
You can see above that we have the dreaded type error. But, if I knew that it was an int, I would have already casted it & therefore had the below:
isinstance also accepts multiple datatypes. So in the below, I am checking whether the MyNumberInt variable is of type int or float.
Print values & variables
You can print values on screen through either your variable names or by explicitly stating the value to be printed
Let’s define some variables to work with:
We can take this a step further and use powers. For example, using the below, my variable “MyPower” is equal to 8, this is because its value is 2 to the power of 3.
We can also automatically calculate the remainder from a division. So the below would output 1.
In many cases, we will run into issues when using apostrophes. We will therefore need to escape them from the string, ensuring no issues are encountered. To escape characters, we can use \ just before the apostrophe.
Finding character X of a string
Now let’s find the fourth letter of my name (starting from position zero)
Calculate string length
Now, let’s find out how long the full string is. The below would output 6
Make a string upper / lower case
For consistency, we can make a string all upper or all lower case:
Alter data types
Now, sometimes, we may want to turn an integer into a string, we can do that by using str()
We can easily concatenate strings using plus signs
User input & variable concatenation
Now, let’s get a bit interactive. The user can enter variables in the terminal. We can also use %s to replace variables in a string concatenation & rather concatenate all variables at the end of the string, concatenated using %
Lists are a comma separated list of values stored within a variable. Lists are mutable, which means, we can modify, add or remove items at any time. We use lists when we need an ordered set of items to iterate over – where the items need to be modifiable.
In the below, we create a list called ‘location’. We then, create a new variable called counties & make it equal the location variable. We then append the location list with a new location, using the append function. Finally, we print the revised list to the screen.
In the above, we’ve appended the new value to the end of the list. We can however, use the insert function to insert the new value into a specific place within the list.
We can also remove items from a list, using the pop command. In the below, I am removing item 3 from the list, which is ‘berkshire’.
In the event that we don’t know what index our item is at in the list, we can use the remove function & specify the exact object that we wish to remove:
A set is not ordered & hence you cannot select an item from a particular index. We define a set as below, using curly brackets. Note that we use the add, rather than the append function to add an item to the set.
If we wish to find a particular item within a set, we must loop through the set until we find it.
Do I need a set or a list?
- I need an ordered sequence of objects – you need a list
- I need to lookup to see if a value exists in a sequence of objects. However, I do not need the sequence to be ordered and I don’t need to store duplicate values – you need a set
Tuples are very similar to lists. They’re a comma separated list of values stored within a variable. Tuples are immutable, which means, once created they cannot be edited (nothing can be added or removed), but, we can search for elements within a tuple.
So, when do we use a tuple over a list?
- Tuples are more efficient (faster) than a list. If we have a constant set of values that we need to iterate through, we should use a tuple, rather than a list. If those values are not static, then we need to use a list.
- If the data does not need to be changed, then we should write protect it by using a tuple, to protect our applications from problems arising from an incorrectly altered list.
Creating a Tuple
Accessing an object within the tuple
We can access the elements of a tuple by using square brackets. For example, below we are calling tuple1 which will extract the fifth letter of the string, as tuple element indexes start with zero.
Convert object to a tuple
Can convert any sequence to a tuple, by using the tuple() command. So, in the below, we have each letter of a string, entered as a separate entry within the tuple:
Creating a list within a tuple
A tuple can store any kind of variable. In the below example, it stores two strings and a list.
The important thing to note here is, that while the tuple itself is immutable (cannot be modified), the list stored within it, can be modified. The above example, shows the addition of the number 4 to the contents of the list within the tuple.
Concatenating Multiple Tuples
We now have two tuples defined above: tuple1 and newtuple. We can concatenate these lists using the + sign to make a longer list.
We can also duplicate the items within a tuple by multiplying it. For example, in the below, we multiply the tuple by 10 & hence, it repeats each entry 10 times.
Count appearances of a value in a tuple
Below, you can see that I can count the number of appearances of the letter K within my tuple.
A dictionary is a key value store in Python. An example usecase for this could be an address book. Where the key is the customer name & the value is their phone number. We could therefore look up against thousands of customers names to retrieve their contact details.
Below, we can see that we have the keys: domain, server, list and their respective values:
If we want to change the value of an item within the dictionary, we can by using the below call:
We can retrieve values from our dictionary in a number of ways. We can print just the key names; just the values or both.
We can also check whether a particular key exists in the dictionary:
Using functions enables you to re-use code, without typing the same thing in several places. It gives you one function, which can be called in a variety of scenarios. The below, shows an example of a function, which prints the name provided by the user.
To call the function, we simply type the below into the ipython terminal:
The below is the syntax for an if statement. In this example, we’ve already created a variable called ‘name’ with the value ‘kieran’.
Below, I’ve included 2 for loop examples. The first, loops through the list of cities & prints each.
The second (below) loops through each letter in the word “london” and prints each letter.
We can take this a step further and add some logic to the loop:
In the below example we run a script to say, while the age is less than 10, print the word child plus the current age. Age is incremented by one each loop.