Learning Python – Part II

Python-logo

In the second part of my Learn Python series of articles we will be going over some basic math operations and then String and their operations. If you haven’t already – please read Part I of this series before starting this article. In the last article we went over why Python is a great language for beginners, some of the theory behind Python and finally how to install Python and run your first few commands.

In the future I want the tutorials be more laid out so that we have a problem to solve and we use a Python program to solve that problem. But for right now since we are just getting started – I think its important to quickly go over the real basics in a very classroom-like manner. It’s unfortunate and a little bit boring, but we have to go over them to make sure we go at a pace for those who know nothing about programming.

Let’s Math

In the last tutorial we learned how to install Python and run commands either through IDLE (the program bundled with Python) or via the command line. To start, open up IDLE or your command line interactive shell and get ready to do some basic math operations.

Addition & Subtraction

We ran an addition command in IDLE in the first tutorial – but to go over it once again addition and subtraction work exactly how you think they would. I’m only going over this for the sake of being thorough – I’m sure all of you know how to add and subtract just fine.

IDLE-Math

Multiplication & Division

If you are new to programming you might not know what characters to type to multiply and divide, so I feel it’s worth mentioning just to make sure. To multiply two numbers, you put a “*” between them:

IDLE-Multiplication

To divide two numbers, you put a “/” between them:

IDLE-Division

Order of Operations

One of the most important things to note about Python (and most programming languages) is the fact that they will follow order of operations when doing calculations (BEDMAS anyone?!). This means if I type something like “6 + 4 * 10” then Python will output “46” and NOT “100“. This is because multiplication is done before addition in the order of operations. If you want to modify the order of operations – just use brackets like you would in math class.

IDLE-Order-of-Operations

Division Tricks

You may have noticed in our above example that division works pretty well in Python and generally gives you the answer you want – this isn’t always the case in all programming languages so be grateful. However there are times when we don’t want an exact answer but rather a rounded whole number. You can achieve this by adding an extra slash and typing “//” as your division. This will give you the rounded down answer.

Using the double “//” division gives you the answer you would get when long dividing the numbers. This may make you wonder how we would get the remainder of these numbers. Of course that leading sentence brings us to the modulus operator which gives us exactly that! By typing a “%” we can get the remainder of the two numbers. Note that this is only the remainder without the rest of the division. This operation may seem rather strange right now – but it is actually very useful in a lot of different applications.

IDLE-More-Division

Exponents

I also want to mention that you can do exponents by using two “**“. For example, typing “2 ** 4” is the equivalent of 24 = 2 x 2 x 2 x 2 = 16.

IDLE-Exponents

That pretty much finishes up all the basic math operations I wanted to cover to start. We will revisit some of these and even look at some different ways to use them in later articles – but you should have a solid foundation to work with now. Next we’re going to look at collections of text – which in programming are called Strings.

Strings

Strings in any programming language are just collections of text characters. When we ran our “Hello World!” program in the first tutorial we actually used a String. To create a String in Python – all you have to do is type the characters between quotes.

Strings have a number of operations just like numbers – but we will look at them a little later on in this article. First we want to look at the quotes.

Quotes & Escape Characters

In Python you can use either single quotes or double quotes to make your Strings. The first quote will tell Python where the String begins and the second quote will tell Python where the String ends. Which type of quote you should use depends on the situation and the String. For example, if you have a simple string like Hello World! then I often use double quotes.

IDLE-Basic-Strings

But now what if I want to have a String that contains double quotes in it, something like She said, “I think I know”. This would cause a bit of a problem because the first double quote in “I think I know” would tell Python that the string ends there. In a situation like this you want to use single quotes to make your String. When using single quotes to make your String Python will read double quotes like normal characters so the String will work.

IDLE-Single-Quotes

Finally – what if we have a situation where our String is She said, “I don’t know”? In this situation we also have a single quote in the don’t, so the String will end there and we will get an error. Luckily Python has something called an escape character. An escape character is put in front of any other character and makes it so Python will read it as a normal character for the most part. Note that some escape characters such as \n have a special meaning (\n is the escape character for a line break). In this example we want to type don\’t to escape the single quote. Note that I will start to use the print method (we’ll get into methods more later) because that will show what the String will be output as rather than its literal value.

IDLE-Escape-Characters-v2

 

This technique can also be used for the double quotes. If you would rather – you can always use single or double quotes for all Strings and then just use escape characters to make them work. We could have just continued using double quotes and escape characters in the above examples rather than switching to single quotes.

IDLE-Escape-Characters-2-v2

 

You may remember above that I mention that some escape characters such as \n have special meanings. This can cause another problem when creating Strings – but luckily the solution is pretty simple. Let’s say you have a directory path you want to save as a String – something like “C:\alex\pictures\mommydaddypics\booty.png”. Let’s see what happens if we type that into Python…

IDLE-Escape-Escape-Character

As you can see – the solution is to escape the escape character. By using \\ we will get a normal \ as our output.

String Operations

Just like numbers, Strings also have operations that can be performed on them. We’ll take a look at a few of these now and that will be it for this tutorial!

Concatenation (+)

String concatenation is the combining of two Strings to make a new String. It uses the + operator like addition. Take a look at some examples below.

IDLE-String-Concatenation

There are a few things I want you to notice from the examples above. Firstly, notice that can you do more than one concatenation such as in the line “Python” + ” ” + “Programming”. Also note that I had to specifically add the empty space in the last 3 examples in order to have a space in the final String. I can do this either by having the space as its own String, or adding it to the end of “Python “ or the start of ” Programming”.

String concatenation is one of the most useful things in programming – so keep it in the back of your head as we move forward. We’ll be using it frequently to solve problems.

Multiplication (*)

I’ve always kind of wondered why Python included this feature, but you can indeed multiply Strings to a certain extent. You can multiply a String by a number which will cause the String to be duplicated by the number given. See an example below.

IDLE-String-Multiplication

You may have noticed at the bottom of this example that you cannot multiply a String by another String. You can only multiply Strings by numbers. Also note that these numbers have to be integers (ie whole numbers).

Getting Characters

Another useful feature of the String is the ability to retrieve individual characters from the String. You can do this by simple adding [#] where # is equal to the index you want to retrieve. There is one very important thing to remember here – computers start counting at 0. This means you will retrieve the first character of the String by adding [0]. Take a look at the example below.

IDLE-Retrieve-Characters

Notice towards the bottom of the example that if you use negative numbers then it will go from the end of the String backwards. Note that these start at -1 and not -0 since -0 = 0.

Getting String Substrings

Not only can we get individual characters out of Strings – we can also get Substrings. Substrings are smaller strings contained within the larger String. We can retrieve these using a very similar notation as the one we used to retrieve characters but this time we add a semicolon like this [#:#]. The first # is the starting index of the substring and the second # is the ending index. Take a look at the example below to see this in action.

IDLE-Substrings

You may have noticed that if you want to get the Substring from the start of the String to a certain index all you need to do is leave out the first number (eg. [:6]). The same technique can be used to go from a certain index to the end of the String by leaving out the second number (eg. [6:]).

If you leave out both numbers then Python will just return the entire String as a Substring. Also note that negative numbers work the same way as when retrieving individual characters, they index from the end of the String starting at -1.

String Length

The final thing we will look at for this tutorial is a method that returns the length of a String. We’ve looked at the print method for Strings before – and we will go into more detail about exactly what a method is later – but for now what you need to know is a method is like a mathematical function. You put something into the method – in this case a String – and it returns something else based on the input.

In this case, the method we are using is typed len() and between the brackets we want to give the method a String. The method will then return the length of that String. As I’ve said before we will go into more detail about how methods work as the tutorials progress. For now just take a look at an example and try using the len() method yourself!

IDLE-String-Length

And that’s it! We went through quite a bit of stuff in this tutorial so be sure to play around with everything and get a feel for it yourself. In the next tutorial we will be looking at programming outside of IDLE and writing our first whole program that actually does something! We will also be going over variables, lists, user input (hopefully) and if statements so there will be a lot going on. It’s gonna be wild!

As always thank you for reading and please share it around as much as you can! Please feel free to put any suggestions or ideas for future articles in the comments section below.