Difference between revisions of "CS110:Lab01"

From IPRE Wiki
Jump to: navigation, search
('''Objectives''')
 
(46 intermediate revisions by 3 users not shown)
Line 1: Line 1:
='''Lab 1: Writing Programs, Defining Functions and Creating Modules in Python''' =
+
='''Lab 1: Writing Programs, Defining Functions and Creating Modules in Calico Python''' =
  
 
=='''Objectives'''==
 
=='''Objectives'''==
*Write and run programs in Python’s IDLE
+
*Write and run programs in Calico Python’s Editor
 
*Become familiar with the Myro Library
 
*Become familiar with the Myro Library
 
*Write simple functions
 
*Write simple functions
*Create Python Scripts/Modules
+
*Create Calico Python Scripts/Modules
  
 
=='''Main Concepts and Terminology'''==
 
=='''Main Concepts and Terminology'''==
*IDLE, Program, variables, modules
+
*Calico Editor, Calico Shell, Program, variables, modules
  
 
='''Lab Exercises'''=
 
='''Lab Exercises'''=
  
=='''Starting IDLE'''==
+
=='''Starting Calico'''==
  
IDLE which stands for “'''I'''nteractive '''D'''eve'''L'''opment '''E'''nvironment” is an environment in which you can write and execute python programs. <br>
+
Calico is an environment in which you can write and execute Python "scripts" (and other languages, too). <br>
  
'''To start IDLE:''' <br>
+
'''To start Calico:''' <br>
Double click the “Start Python.pwd” icon on your desktop '''OR'''<br>
+
Double click the “StartCalico” icon on your desktop.
Go to the start menu, find Python, and run the program called IDLE.
+
When you first start IDLE, you may see something like this:
+
  
[[Image:Idle1.JPG|center]]
+
When you first start Calico, 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.
+
[[Image:CalicoShell.jpg|center]]
 +
 
 +
The text "python>" is the Python prompt. It indicates that the computer is waiting for you to give it a command.
 +
 
 +
For more details on the Calico interface, please see: [[Calico: Getting Started]]
  
 
=='''Writing Programs'''==
 
=='''Writing Programs'''==
Line 30: Line 32:
  
 
<pre>
 
<pre>
>>> print “Hello World!
+
python> print("Hello World!")
 
Hello World!
 
Hello World!
>>> print “I love python”
+
python> print("This is python")
I love python
+
This is python
>>> print “My name is Sarah”
+
python> print("My name is Sarah")
 
My name is Sarah
 
My name is Sarah
 
</pre>
 
</pre>
  
The statements should look like this in IDLE (notice the color code):
+
The statements should look like this in Calico (notice the color code):
  
 
[[Image:Hello.JPG|center]]
 
[[Image:Hello.JPG|center]]
Line 46: Line 48:
 
''Variables'' are like containers that store different values that can be looked up or changed at a later time. Below is an example of how variables are defined and used:
 
''Variables'' are like containers that store different values that can be looked up or changed at a later time. Below is an example of how variables are defined and used:
 
<pre>
 
<pre>
>>> x = 3
+
python> x = 3
>>> y = x * x
+
python> y = x * x
>>> print y
+
python> print(y)
 
9
 
9
>>> words = "This is a string of text."
+
python> words = "This is a string of text."
>>> words
+
python> words
 
'This is a string of text.'
 
'This is a string of text.'
>>> print words
+
python> print(words)
 
This is a string of text.
 
This is a string of text.
 
</pre>
 
</pre>
  
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’.
+
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'''==
 
==''' Using the Myro Module'''==
Line 64: Line 66:
  
 
<pre>
 
<pre>
>>> from myro import *
+
python> from Myro import *
 
</pre>
 
</pre>
 +
 +
Try using the following myro functions:
 +
* speak("message")      - computer will say the text message
 +
* getVoiceNames()            - get the list of available voices
 +
* setVoiceName("voice name") - set the voice to a named voice
 +
* getVoiceName()            - get the name of the current voice
 +
 +
<pre>
 +
python> speak("Today is the greatest day of my life")
 +
Today is the greatest day of my life
 +
python> getVoiceNames()
 +
...
 +
python> sorted(getVoiceNames())
 +
...
 +
python> getVoiceName()
 +
'default'
 +
python> setVoiceName("Cantonese Female")
 +
python> speak("Today is the greatest day of my life")
 +
</pre>
 +
 +
Another function to try is:
 +
* askQuestion(question, [answerList]) - This function prompts a question and returns answer.
 +
<pre>
 +
askQuestion('Are you ready?',['yes', 'no', 'maybe'])
 +
</pre>
 +
 +
You should see the following:
 +
[[Image:Ready.JPG|center]]
 +
 +
==''' Defining Functions '''==
 +
 +
Python allows us to put a sequence of statements together in a function. Functions are defined using the following format:
 +
 +
<pre>
 +
def functionName(arg1, arg2, ...):
 +
statement1
 +
statement2
 +
...
 +
</pre>
 +
 +
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.
 +
 +
Now it's time to create your own functions. Try the following. Feel free to modify the functions to create your own.
 +
<pre>
 +
python> def juliet(message):
 +
    setVoiceName('English-US Female')
 +
            speak(message)
 +
 +
python> def romeo(message):
 +
    setVoiceName('English-US Male')
 +
            speak(message)
 +
</pre>
 +
 +
Note that you may need to press the TAB key to get speak() command to line up with setVoiceName(). Also, you need to press the ENTER key after speak() twice, to get back to the Python prompt.
 +
 +
Now that you have defined these functions, you can use them. Here is a mini play starring Romeo and Juliet:
 +
 +
<pre>
 +
python> juliet("Hey Ro, where have you been?")
 +
Ok
 +
python> romeo("Hey J, I was chillin")
 +
Ok
 +
</pre>
 +
 +
==Creating Python Modules==
 +
 +
One problem with defining functions interactively at the Calico Python prompt like this is that the definitions go away when we quit Calico. If we want to use them again in the future, we have to retype them. Python allows you to create '''modules''' or files that contain your function definitions and other commands. This file is saved on a disk so that it can be used over and over again.
 +
 +
A ''module'' is just a text file that you can create in any text editor such as Notepad (but not Microsoft Word, as that is a word processor, at least by default). Python files must be saved with a ‘.py’ extension so that the computer knows it is a python module. Let’s illustrate the use of a module by creating our own and running it.
 +
 +
You can put "comments" in a Python program, or module, by putting a pound-sign (#) on a line. Anything after the pound-sign is a message from you to other humans, not to Python.
 +
 +
You can open up python’s editor in Calico by going to the menu item '''File > New Python Script'''. Copy and paste the functions you have written into a new file and save it. You should add the '.py' extension to your file name when saving it. This tells the computer that it is a python file. Your module should look something like this:
 +
 +
<pre>
 +
#File: miniPlay.py
 +
#Author: Sarah Reddington
 +
#Description: a mini play/conversation between Romeo and Juliet
 +
 +
from Myro import *
 +
 +
def juliet(message):
 +
    setVoiceName('English-US Female')
 +
    speak(message)
 +
 +
def romeo(message):
 +
    setVoiceName('English-US Male')
 +
    speak(message)
 +
 +
# Calling the functions:
 +
 +
juliet("Hey Ro, where have you been?")
 +
romeo("Hey J, I was chillin")
 +
</pre>
 +
 +
=== Running a Python Module ===
 +
To run a python module from the Calico Editor go to '''Script > Apply''' in your menu options.
 +
 +
= '''Assignment 01''' =
 +
 +
# '''Playing With Voices:''' Using the voice function, write a Python module that is an "audio play" in which your computer will "read" it. Your "play" should be a 1.5 minute reading of anything that has dialogue. Your "play" should have at least two characters. You may create your own dialogue, or use other sources (e.g., Shakespeare, lyrics, movie scripts, etc.).
 +
#'''Initial Reactions:''' How did you find the first lab? Was it manageable or overwhelming? How did you enjoy the topic? You can write or type your initial reactions at the end of your Python play module.
 +
#'''Submit Your Module:''' When you are finished, print out a copy of the module you wrote and submit it to me next Monday.
 +
#'''For next time:''' Read chapter 1 ("The World of Robots") of your text, [[Learning Computing With Robots Using Calico Python]]. You will need to be familiar with this chapter in order to do lab 2.
 +
 +
= Links =
 +
 +
* Back to [http://wiki.roboteducation.org/CS110_Lab Lab Home Page]
 +
* Back to [http://cs.brynmawr.edu/cs110-2 Course Home Page]

Latest revision as of 00:06, 1 September 2011

Lab 1: Writing Programs, Defining Functions and Creating Modules in Calico Python

Objectives

  • Write and run programs in Calico Python’s Editor
  • Become familiar with the Myro Library
  • Write simple functions
  • Create Calico Python Scripts/Modules

Main Concepts and Terminology

  • Calico Editor, Calico Shell, Program, variables, modules

Lab Exercises

Starting Calico

Calico is an environment in which you can write and execute Python "scripts" (and other languages, too).

To start Calico:
Double click the “StartCalico” icon on your desktop.

When you first start Calico, you may see something like this:

CalicoShell.jpg

The text "python>" is the Python prompt. It indicates that the computer is waiting for you to give it a command.

For more details on the Calico interface, please see: Calico: Getting Started

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:

python> print("Hello World!")
Hello World!
python> print("This is python")
This is python
python> print("My name is Sarah")
My name is Sarah

The statements should look like this in Calico (notice the color code):

Hello.JPG

Using Variables

Variables are like containers that store different values that can be looked up or changed at a later time. Below is an example of how variables are defined and used:

python> x = 3
python> y = x * x
python> print(y)
9
python> words = "This is a string of text."
python> words
'This is a string of text.'
python> print(words)
This is a string of text.

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:

python> from Myro import *

Try using the following myro functions:

  • speak("message") - computer will say the text message
  • getVoiceNames() - get the list of available voices
  • setVoiceName("voice name") - set the voice to a named voice
  • getVoiceName() - get the name of the current voice
python> speak("Today is the greatest day of my life")
Today is the greatest day of my life
python> getVoiceNames()
...
python> sorted(getVoiceNames())
...
python> getVoiceName()
'default'
python> setVoiceName("Cantonese Female")
python> speak("Today is the greatest day of my life")

Another function to try is:

  • askQuestion(question, [answerList]) - This function prompts a question and returns answer.
askQuestion('Are you ready?',['yes', 'no', 'maybe'])

You should see the following:

Ready.JPG

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.

Now it's time to create your own functions. Try the following. Feel free to modify the functions to create your own.

python> def juliet(message):
	    setVoiceName('English-US Female')
            speak(message)	

python> def romeo(message):
	    setVoiceName('English-US Male')
            speak(message)

Note that you may need to press the TAB key to get speak() command to line up with setVoiceName(). Also, you need to press the ENTER key after speak() twice, to get back to the Python prompt.

Now that you have defined these functions, you can use them. Here is a mini play starring Romeo and Juliet:

python> juliet("Hey Ro, where have you been?")
Ok
python> romeo("Hey J, I was chillin")
Ok

Creating Python Modules

One problem with defining functions interactively at the Calico Python prompt like this is that the definitions go away when we quit Calico. If we want to use them again in the future, we have to retype them. Python allows you to create modules or files that contain your function definitions and other commands. This file is saved on a disk so that it can be used over and over again.

A module is just a text file that you can create in any text editor such as Notepad (but not Microsoft Word, as that is a word processor, at least by default). Python files must be saved with a ‘.py’ extension so that the computer knows it is a python module. Let’s illustrate the use of a module by creating our own and running it.

You can put "comments" in a Python program, or module, by putting a pound-sign (#) on a line. Anything after the pound-sign is a message from you to other humans, not to Python.

You can open up python’s editor in Calico by going to the menu item File > New Python Script. Copy and paste the functions you have written into a new file and save it. You should add the '.py' extension to your file name when saving it. This tells the computer that it is a python file. Your module should look something like this:

#File: miniPlay.py
#Author: Sarah Reddington
#Description: a mini play/conversation between Romeo and Juliet 

from Myro import *

def juliet(message):
    setVoiceName('English-US Female')
    speak(message)

def romeo(message):
    setVoiceName('English-US Male')
    speak(message)

# Calling the functions:

juliet("Hey Ro, where have you been?")
romeo("Hey J, I was chillin")

Running a Python Module

To run a python module from the Calico Editor go to Script > Apply in your menu options.

Assignment 01

  1. Playing With Voices: Using the voice function, write a Python module that is an "audio play" in which your computer will "read" it. Your "play" should be a 1.5 minute reading of anything that has dialogue. Your "play" should have at least two characters. You may create your own dialogue, or use other sources (e.g., Shakespeare, lyrics, movie scripts, etc.).
  2. Initial Reactions: How did you find the first lab? Was it manageable or overwhelming? How did you enjoy the topic? You can write or type your initial reactions at the end of your Python play module.
  3. Submit Your Module: When you are finished, print out a copy of the module you wrote and submit it to me next Monday.
  4. For next time: Read chapter 1 ("The World of Robots") of your text, Learning Computing With Robots Using Calico Python. You will need to be familiar with this chapter in order to do lab 2.

Links