CS110:Lab01

From IPRE Wiki
Revision as of 00:41, 2 September 2009 by Ttthomas (Talk | contribs) ('''Using Variables''')

Jump to: navigation, search

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

Objectives

  • Write and run programs in Python’s IDLE
  • Write simple functions
  • Create Python Scripts/Modules

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:

Idle1.JPG

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:

>>> print “Hello World!”
Hello World!
>>> print “I love python”
I love python
>>> print “My name is Sarah”
My name is Sarah

The statements should look like this in IDLE (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:

>>> x = 3
>>> y = x * x
>>> print y
9
>>> words = "This is a string of text."
>>> words
'This is a string of text.'
>>> 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’.