Python introduction: Difference between revisions

From Sensors in Schools
Jump to navigation Jump to search
No edit summary
No edit summary
Line 79: Line 79:


[[File:Screen Shot 2021-12-21 at 9.22.26 pm.png]]
[[File:Screen Shot 2021-12-21 at 9.22.26 pm.png]]
* Comments or addition information that does not require students to do anything. E.g. “This may take several moments to load
=== Working with Variables ===
* Computer programming languages can also manipulate variables.
* Variables are a bit like the variables we use in algebra.
* Variables can represent numbers, text and other entities.
* Let’s start with some simple numerical variables.
* Enter '''a = 7''' and '''b = 3'''
* This will '''assign''' the number 7 to the variable name a.
* It will also assign the number 3 to the variable name b.
* Finally, just like in algebra, we can add two variables together. In this case '''a + b''' equals 10.
[[File:Screen Shot 2021-12-21 at 9.24.53 pm.png]]
* We can also use variables to hold text or Strings.
* Enter '''name = “Sarah”'''
* To print out the value held in the variable name we use the print statement.
* Enter '''print(name)''' – this will print out the value currently held in the variable called name.
* We can also add some additional text within the print statement to make the output more readable.
* Two variations are shown.
** '''print(“My name is “ + name)'''. This statement adds two Strings together using the plus '''“+”''' symbol
** '''print(“My name is “, name)'''. This statement uses a special feature of the print statement.
* Strings and variables are separated by a comma.
[[File:Screen Shot 2021-12-21 at 9.26.52 pm.png]]
* One of the reasons we use variable to hold values is because these values may change.
* If we enter '''name = “Donny”''' and then enter '''print(name)''' we will see that “Sarah” has been replaced with “Donny”
[[File:Screen Shot 2021-12-21 at 9.27.59 pm.png]]
=== For loops in Python ===
* Sometimes you need to repeat a certain operation a specific number of times within a program.
* To do this we use loops.
* One such loop operation is called a '''For Loop'''.
* In this example we will create a For Loop that will repeat itself five times.
* Enter the following code and observe the result.
[[File:Screen Shot 2021-12-21 at 9.34.19 pm.png]]
* This '''For Loop''' will repeat 5 times because the '''range''' is set to '''5'''.
* We can tell what code is within the loop structure because the code is indented within the loop.
* The only code within the loop is the '''print(“A”)''' statement.
=== For loop with variables ===
* When we declare a '''for loop''' we always include a variable.
* In the '''for loop''' example above the variable name was i
* The '''variable i''' gives us a running count of the number of times we have looped.
* Enter the following example:
[[File:Screen Shot 2021-12-21 at 9.37.49 pm.png]]

Revision as of 10:38, 21 December 2021

Overview

  • The Python programming language is one of the most universal computer languages used today.
  • The makers of the Raspberry Pi though so highly of the language that the name “Pi” was named after Python.
  • It is an easy language to learn and will help us to write programs to collect and process data from environmental sensors.
  • We will be using the latest version of Python, which is Python3 (Python 3.7.3).

Learning Objectives

  • Learn how to install the Python3 Integrated Development and Learning Environment (IDLE3).
  • Run python3 code in the Python Shell Window
  • Run python programs in the Python Editor Window

Installing Python3 IDLE

  • To make programming in Python3 easier we will be using an Integrated Development and Learning Environment (IDLE). * There are many different development environments available for Python.
  • Other common environments include: Thonny, PyCharm and iPython.
  • In this example we will install the Python3 IDLE using the Terminal on the Raspberry Pi.
  • When we need to install programs on the Raspberry Pi we always use the Terminal.
  • To install software, we use an application manager called apt.

Installing Python3 IDLE

  • Open the Terminal on the Raspberry Pi
  • Update our existing software libraries by entering the command sudo apt-get update
  • We always need to update our software libraries before we install new software.
  • This because software sometimes needs other software to help make it work.
  • The additional software components are called dependencies.
  • If we install an older version of the software, some of the dependencies may have changed and this could make our program run erratically.

  • Now we can install the idle3 software by entering the command sudo apt-get install idle3
  • If the software is already installed (as in the example below) the Terminal will tell you.

  • If you don’t know what software to install or if the software exists, you can search for software in the library by entering the command sudo apt-cache search idle3
  • If you are interested in installing a game of chess you could enter the command sudo apt-cache search chess or use the wildcard symbol * to expand your search, sudo apt-cache search chess*

  • If the installation of Python3 IDLE has worked correctly you should see the Python3 software link appear in the main Raspberry Pi menu under Programming.

Using the python shell to enter code

  • The Python3 Integrated Development and Learning Environment (IDLE) is a common environment for most programming languages.
  • Sometimes it is shortened to IDE (Integrated Development Environment).
  • These interfaces make editing and running programs easier. Python3 IDLE is very simple and easy to use and it was the first environment installed on the Raspberry Pi for learning Python.
  • In this example, we will start using the Shell Window.
  • The Shell Window allows single Python instructions (or small instruction sets) to be entered and tested.
  • We will run through a few examples.

Explore the Python Shell Window and the Python Language

  • Go to the Python3 IDLE icon in the Raspberry Pi > Programming menu and open IDLE3.
  • The program will open to the Shell Window.

  • We can now start to enter python language instructions directly in the Shell Window.
  • Let’s start with some simple arithmetic instructions. Type in 1 + 3 and press Enter.
  • Try some other operations.

  • More complex maths will require some special instructions and the import of additional libraries.
  • We will cover this in future lessons.


Printing Words, Text and Strings

  • In addition to numbers, we can also print out words and text.
  • To make python print out text you enter the command print(“Hello”)
  • In the older days when there were no computer monitors, all output from computers went to printers.
  • This legacy continues today, and when we want to display the output from a computer, we use the print statement.
  • To tell a computer that we are working with text we need to include the text in quotation marks “Hello”.
  • Another way we refer to words or text in computer languages, is to call them Strings.
  • Computers manipulate the characters in a word or sentence like beads threaded on a string. Hence the term String.

  • Comments or addition information that does not require students to do anything. E.g. “This may take several moments to load

Working with Variables

  • Computer programming languages can also manipulate variables.
  • Variables are a bit like the variables we use in algebra.
  • Variables can represent numbers, text and other entities.
  • Let’s start with some simple numerical variables.
  • Enter a = 7 and b = 3
  • This will assign the number 7 to the variable name a.
  • It will also assign the number 3 to the variable name b.
  • Finally, just like in algebra, we can add two variables together. In this case a + b equals 10.

  • We can also use variables to hold text or Strings.
  • Enter name = “Sarah”
  • To print out the value held in the variable name we use the print statement.
  • Enter print(name) – this will print out the value currently held in the variable called name.
  • We can also add some additional text within the print statement to make the output more readable.
  • Two variations are shown.
    • print(“My name is “ + name). This statement adds two Strings together using the plus “+” symbol
    • print(“My name is “, name). This statement uses a special feature of the print statement.
  • Strings and variables are separated by a comma.

  • One of the reasons we use variable to hold values is because these values may change.
  • If we enter name = “Donny” and then enter print(name) we will see that “Sarah” has been replaced with “Donny”

For loops in Python

  • Sometimes you need to repeat a certain operation a specific number of times within a program.
  • To do this we use loops.
  • One such loop operation is called a For Loop.
  • In this example we will create a For Loop that will repeat itself five times.
  • Enter the following code and observe the result.

  • This For Loop will repeat 5 times because the range is set to 5.
  • We can tell what code is within the loop structure because the code is indented within the loop.
  • The only code within the loop is the print(“A”) statement.

For loop with variables

  • When we declare a for loop we always include a variable.
  • In the for loop example above the variable name was i
  • The variable i gives us a running count of the number of times we have looped.
  • Enter the following example: