SC1003 Ex 2 - Basic Python Programming
SC1003 Ex 2 - Basic Python Programming
for
SC1003
Introduction to Computational Thinking and
Programming
The following is a simple program that should be intuitive obvious on what it is doing.
Line
1
2
3
4
5
6
7
Ways to edit and run the exercises: (Choose either option 1 or Option 2)
1. Use cloud9 IDE
Connect RPi to PC, run “Putty” and start the Cloud9 IDE (refer to Practical Exercise
manual #2 ), and use PC web browser to access cloud9 IDLE. Check that cloud9
IDE is configured for Python 3 – See Appendix A below for the procedure.
Or
2. Use “Remote Desktop Connection”
Connect RPi to PC, run “Remote Desktop Connection” on PC desktop to connect to
Rpi, and run the Python 3 IDLE within Rpi directly. Refer to Appendix B for steps.
Key in the above program code. Remember to save your program file in the folder of
your group
Run the program and observe its output (which is shown in ….)
Line 2 is a python program assignment statement that involve a variable, a function and the
assignment symbol.
On the right side of the assignment symbol ‘=’, input()is a built-in small Python
program known as a function.
It prints the characters indicated in the parentheses and then waits for you to input
character(s) through the keyboard.
Once you type the sequence of character(s) through the keyboard and press the
Enter key, the function stores the sequence of characters somewhere in the memory,
and assign this sequence of character to the variable radius_str, which is on
the left side of the ‘=’ symbol.
The sequence of characters is known as a string in computing. That is the data type
of this variable is string (str n short).
Hence the variable radius is appended with _str to make it explicitly clear to the
reader that this variable is a string.
Line 4 is a statement to calculate the circumference of a circle using the formula as shown,
and assign it to the variable circumference
The value of the pi is available through the math module as math.pi.
Line 5 is a statement to calculate the area of a circle as shown and assign it to the variable
area.
Line 6 is to use the built-in function print to display the quoted string, follows by the value
of the variable circumference onto the monitor.
The backslash character ‘\’ indicates that the statement is to continue onto the next
line, for situation when the statement is too long to fix on one line.
2.1 The first thing to do when coding the program for the SH board is to import the module
that contains the various functions required to operate the SH board. This is done by
using the following two statements.
This is similar (but not the same) to create a variable and assign it with certain initial
value - but the sense here is known as an object in Python. (And of course you can
use other name of your choice.)
Side Note (for those who are curious - the following provide a bit more detail
about what the two lines of code are about):
Slightly different from the math module you saw earlier, here you first import a
‘class’ definition SenseHat from the sense_hat module. A ‘class’ contains
code that describe the functions (more correctly, it is known as methods in
Python) that can be used by our program.
The way to use the class definition is you first create an object and initialize it to
have the characteristics of the class. In this case, we create an object with the
name sense and assign it to have the SenseHat class. This is described as to
instantiate sense to be a SenseHat object.
This is somewhat similar (but not exactly the same) to when you create a
variable b , you can’t use it before you assign a value to it. (see below example
that was entered in IDEL3 to illustrate the point)
2.2 With the object sense just created, you can now use it with the various commands
(i.e. functions, or methods) provided by the sense_hat module. The following are
some of these commands that you can use to manipulate the display on the SH board.
You can use the command set_rotation() to change the orientation of the
display shown on the SH board using one of the parameters (0, 90, 180, 270).
sense.set_rotation(180)
You can change how the message is displayed by adding extra parameters to
the show_message command.
text_colour to change the colour of the message, such as
sense.show_message(“Hello”, text_colour = (255,255,0))
You can use the command sense.clear() to clear the display on the SH
board, or set it to a specific colour.
sense.clear() or sense.clear(100,100,100)
b) Use of variables
Now create three variables, color_msg, color_bg, speed in your program such that
you can display messages using the statement as follows.
Tip: You may want to use the built-in print() function to print messages in the console
when debugging your code.
There are plenty of differences between version 2 and version 3 of the Python language.
Two of the differences you will notice when you code programs in this course are the
print() and input() functions.
In Python 2, the print statement is written as print “Hello world”, while in Python
3, print statement is replaced as a function and is written as print (“Hello
world”).
In Python 2, the input()function evaluates the data it receives and returns the data
type based on what it ‘thinks’ it should be. In Python 3, input() function always returns
the data as str (i.e. string) data type object.
Cloud9 IDE supports both Python 2 and Python 3, but it is obvious that we should learn the
latest, which is Python 3. As such, you will configure Cloud9 IDE for Python 3 in this course,
using the steps as described below.
ii) The Preferences window pane will be shown. Look for the Python Version setting.
iv) In addition, when you later run your program, check that the setting for Runner is set to
Python (and NOT Python 2 ).
Session : Xorg
3 Username : pi
Password : raspberry
Click on the Resize Button on the Top blue bar to resize the remote desktop for ease
of accessing the PC and remote session together
To execute the code, select “Run” “Run Module F5” from the editor menu bar,
this will invoke a new python shell window where you can interact with the
game. (Remarks : you can use the <F5> function key to execute the code as
well)
Keep both the editor and python shell side by side for ease of use