Objectives

Main Concepts and Terminology

IDLE, Program, variables, modules

Lab Exercises

Starting IDLE

IDLE which stands for “Interactive DeveLopment Environment” is an environment in which you can write and execute python programs.

To start IDLE:
Double click the “Start Python.pwd” icon on your desktop OR
Go to the start menu, find Python, and run the program called IDLE.
When you first start IDLE, you may see something like this:

The >>> is the Python prompt. It indicates that the computer is waiting for us to give it a command.

Writing Programs

A program is a set of statements or instructions that tell the computer what and how it should execute a task. The following instructions tell the computer to print a statement using the print command:

In the code example above, x, y and words are all variables. Variables can store any data type. In the previous example, x and y store integers but the variable ‘words’ stores a string (any piece of text enclosed in quotes). You can see the value of the variable by simply writing the name of the variable at the prompt or by typing print ‘variable name’.

Using the Myro Module

Myro, which stands for My Robotics, is a software package developed by the Institute for Personal Robots in Education (IPRE). It is a platform designed to enable beginners to program robots while learning about computer science. It consists of several python modules that you will use to control your personal robots and carry out other tasks. To use the functions in the Myro library type the following:

>>> from myro import *

Try using the following myro functions:

speak("message") - computer will say the text message

stopSpeaking() - computer stops talking

getVoices() - get the list of available voices

setVoice("voice name") - set the voice to a named voice

getVoice() - get the name of the current voice

>>> speak("Today is the greatest day of my life")
Today is the greatest day of my life
>>> getVoices()
['MS-Anna-1033-20-DSK']
>>> getVoice()
u'MS-Anna-1033-20-DSK'
>>> setVoice('MS-Anna-1033-20-DSK')

Another function to try is:

askQuestion('Are you ready?',['yes', 'no', 'maybe'])

You should see the following:

Defining Functions

Python allows us to put a sequence of statements together in a function. Functions are defined using the following format:

def functionName(arg1, arg2, ...):
statement1
statement2
...

The keyword def is always used at the beginning of a function definition. This is followed by the name of the function and the function’s arguments (listed as arg1, arg2, … above) in parentheses. Arguments are input values that the function needs to perform a task.