This article will take a closer look at some of the more intermediate operations that you can carry out using Python 3. To start, I’ll give a brief introduction to operator precedence. This is simply ordered in which Python will carry out operations. It is similar to the order precedence in mathematics, with multiplication, or *, being of higher precedence than other mathematical functions like adding and subtracting. However, in Python we can use parentheses, (), to designate higher precedence. This is shown in the example below. Both values are arrived at via the same operations, but the value of x is determined by standard operator precedence, and the value of y is determined using parentheses to alter the precedence.
Now to dive deeper into the use of Booleans within Python 3. Booleans, as we discussed in the previous article, are simply True or False values. We use them in Python to make comparisons, and they come in very handy in certain operations s we will see later on. They take advantage of the mathematical logic studied by George Boole, hence the name Boolean. They can be used in accordance with numbers and with strings. A simple example of Boolean logic would be as follows. If we take two variables, x and y, and we set them to two distinct values, 5 and 10 respectively, we can demonstrate the basic nature of Booleans in Python.
First, you need to be aware of the different comparison operators that are used in Python programming. The main ones that you need to know at this stage are ==, !=, <, >, <= and >=.
As can be seen from the example, == and =! correspond to equal to and not equal to. The less than, greater than, less than or equal to and greater than or equal to signs are also used for comparisons, denoted by their usual mathematical symbols >, <, <= and >= respectively. Booleans can also be used to compare strings, as shown below.
Where Booleans really start to come in handy is with different statements and loops. The first of these that you will come across is the if statement. This is used as a conditional statement. This means that you can use if statements to carry out operations within your program only if certain conditions are met. For example, you could create a programme that will print a simple statement about 2 numbers entered by the user. We will use the input operation that we learned in the last article to do this. Next, we will use an if statement to define a condition that will need to be met for a specific phrase to be printed. We will then use something called an elif statement to print the opposite of the first phrase if, and only if, the opposite conditions are met. We will finish this part with an else statement; the final possibility of conditions within our program. We will take the user input for both numbers first of all, and then we will code in our conditions.
We will now create the if, elif and else statements to determine what phrase will be printed when the numbers are inputted. For our example, we will use 3 sets of numbers to illustrate each possibility.
By using if,
- You must follow every condition with a colon :
- The conditions can involve strings or numbers
- You can include as many elif statements as you want, or you can have none at all and just use an else statement
Lists are another very useful aspect of Python programming. Lists are formed by placing brackets () around a set of ordered data. For example, you could have a list called “friends” containing all of your friends’ names.
You can access the items in the list by asking the program to print an index value. Indexes are used to refer to the items in the list as the computer stores them. The first item in the list is index 0, followed by 1, 2 etc. In this case, we want to print the third friend in the list, so we use the index number 2.
A useful operation using lists is the range of operation. This allows you to specify a range within the list, and then print only the values within this specified range. In this example, we will code it so that it prints all of the items except the first and last ones.
The next part of Python 3 programming involves loops. The first loop type is called a while loop. While loops are used to execute lines of code while a certain condition is satisfied. For example, we will set a variable x equal to 5. We will ask the program to print the value of x, then add one to it. However, we only want it to do this until x reaches 10. To do this, we use a while loop. We say that while x is less than 10, we will print the value and then add 1. This results in the program printing the initial value of 5, then adding 1, printing the second value of 6, adding 1 and so on until x equals 10. When x equals 10 the value is printed, 1 is added to it, but the value of x is now greater than 10 and so the condition of the while loop is broken. This causes the program to stop running and doesn’t print the next value of 11.
This is a very useful loop as it can detect when conditions are broken, so can be used in applications that you want to stop running when, say, price increases too much on stock and so you want it to stop buying or sell all the stock you own.
Another useful loop is the
The second example uses our list of friends from earlier and prints each friend in the list in order.