Home » PythonPi Introductory Course » PythonPi: Lesson Four

PythonPi: Lesson Four

Today we are going to be learning about logic. Be sure to check you’ve submitted last weeks coursework to the classroom section of the PythonPi website.

To begin with, we will be introducing how a program runs. You may think that a program runs consistently and at all times, but what actually happens is the processor in your computer is only actually running one thing at a time. Each time it runs something it is known as a process. The process goes through many of your computers different sections before returning the result to the section it intended.

When you write a program on your Raspberry Pi, using the Python language, you must write each program in a set order. For example if you have the code:

print(“Python”);
print(“Raspberry Pi”);

The code will always print Python then Raspberry Pi. This is because programming happens in a way that is considered logical and in the sequence they are written. The computer will read the code in the sequence it is written.

Programming is considered a logical skill. We also use things known as operators in programming. They are also known as Logical Operators.

A logical operator can be any of many things. There is a guide to them here.  The most common logical operators across any program language are:

  • and – to check two things against each other for example if A and B are the same values as c and d
  • or – to check if something matches to one thing or another for example to check if A is the same as B or C
  • not – to check if something is not the same as something else. For example to check that A is not the same as B.

When coding we can use logical operators with functions. It is now time for you to be introduced to one of the most important concepts in coding. Statements. A statement is a comparing function that uses conditions to decide how to react.

For the lab today, you will be required to write your first if statement. Something that is very common in programming languages across the board

pythonpi

The above will print only if the value x is actually equal to 10. If it is not. The program will not print anything. For example, if you typed into your IDLE3, x = 11 before typing the if statement, you would not see the printed result. This is the foundation of an if statement. An if statement will only process the next line of code if the conditions are met.  This is shown above. If x is not actually equal to ten, then the next line won’t be processed.

Hint:

  • When typing your if statement into the IDLE3 or Shell, use shift and enter keys on your keyboard in order to not process the code straight away and start a new line.
  • When doing a comparison in an if statement we use two equals signs (==) instead of one. This tells the computer that you are comparing and not defining a variable to be an equals sign.
  • When doing the practical, != is the operator for not equal to. For example, if  a != 10 would check to see is a was not equal to 10.

Leave a comment

Your email address will not be published. Required fields are marked *