You are on page 1of 14

Python Programming Lessons

LESSON 2:
WRITING PYTHON SCRIPTS USING IDLE
Version 1.1

Prepared by:
Nur Dirie Hersi Fursade, Ph.D.
Senior Trainer and Consultant
Toronto, ON, Canada

June 8, 2016

Python Programming Lessons


TOPICS:
Reading Keyboard Input (raw_input function)
Data Conversion
Writing, Saving, and Executing Python Scripts in IDLE
Multiple Assignments
Program Comments (Single and Multiple Lines)
White spaces and Indentations
Introduction to Escape Codes
Introduction to MATH Module
Examples and Exercises
READING INPUT FROM THE KEYBOARD
We use the function raw_input() to read a line from keyboard input (i.e. the user).
The function returns a string. The syntax is:
Firstname = raw_input('What is your first name? ')
You can then print the Firstname as shown below:
print (Firstname)
In the above example, a variable called Firstname stores the input data. See below
screen for an example of how it works

Writing Python Scripts Using IDE: Lesson 2

Nur Dirie Hersi Fursade, Ph.D.

Page 2

In Python version 2.7, the raw_input function takes the input as a string. If the
input will be used as a number, it must be converted. In Python 3.x, raw_input
has been replaced with input
DATA CONVERSION
If you want to convert a string to an integer, you need to use the int() function as
shown in the following screenshot. You will use float() if you want to convert into
floating number
In the screenshot given below, we enter the values of x and y. And we will add
them and store the result into z:

Why did we get 32?

Now we got the correct answer after converting the


input into integers through int function

Writing Python Scripts Using IDE: Lesson 2

Nur Dirie Hersi Fursade, Ph.D.

Page 3

WRITING PROGRAMS IN IDLE


If you have to do more than a small calculation, it is better to write a script (a
program in Python). This can be done in the editor window of IDLE.
To write and run a program in IDLE, you follow the following steps:
1) Click FILE, NEW WINDOW in Python Shell (a new window will open which is
the editor)
2) Write your script in the editor
3) Then click FILE, SAVE and provide the name of the script with extension
.py, for example test1.py)
4) Finally click RUN, then RUN MODULE in the editor to run the program (you
can alternatively click F5)
5) You get back to the Python Shell with the output of your program

PYTHON SHELL WINDOW

Step 1: Click FILE, NEW FILE in the Shell. The editor


window given below will be opened

Step 2: You write your script in the editor window


given below

PYTHON EDITOR WINDOW

Step 3: You then click FILE, SAVE in the editor


Step 4: You then click RUN, RUN MODULE

Writing Python Scripts Using IDE: Lesson 2

Nur Dirie Hersi Fursade, Ph.D.

Page 4

You get back to the Python Shell with the output of your program as shown below

PYTHON SHELL WINDOW

This is the output of your script in Python Shell

Note:
a) The editor allows us to create, edit, run, and save our scripts or programs
b) The editor understands the syntax of the Python language and uses
different colors to highlight the various components that comprise a script
or a program.
c) Much of the work of program development occurs in the editor
d) The interactive interpreter is most useful for experimenting with small
statements of Python code.

Writing Python Scripts Using IDE: Lesson 2

Nur Dirie Hersi Fursade, Ph.D.

Page 5

EXAMPLE
Create and execute a script that implements the following instructions (Name the
script as example2.py):
1)
2)
3)
4)
5)
6)

Enter the integer 10 from the keyboard and store in variable x


Enter the integer 6 from the keyboard and store in variable y
Multiply the variables x and y and store the result in variable z
Print x
Print y
Print z

Solution:
Start IDLE, click FILE, NEW FILE, type the code shown below in the editor, save
the file as example2.py

Writing Python Scripts Using IDE: Lesson 2

Nur Dirie Hersi Fursade, Ph.D.

Page 6

Then click FILE, RUN MODULE in the editor, enter the requested values and you
will get the following screenshot:

Writing Python Scripts Using IDE: Lesson 2

Nur Dirie Hersi Fursade, Ph.D.

Page 7

SPACES AND TABS IN PYTHON


1) It is important that no whitespace (spaces or tabs) come before the
beginning of each statement.
2) In Python the indentation of statements is significant and must be done
properly.
3) If we try to put a single space before a statement in the interactive shell, we
get an error as shown in the following screenshot
In the following screenshot, we write two statements (note the space before print):
x=2
print x
The second failed due to the white spaces before the print

Note the error in this line


The error is due to indentation or the
spaces between the prompt and print x

How to fix the above error? We removed the white spaces before the print as
shown below

Note we removed the whitespace before print and


the statement run successfully

We will talk more about indentations when we cover IF statements and loops

Writing Python Scripts Using IDE: Lesson 2

Nur Dirie Hersi Fursade, Ph.D.

Page 8

MULTIPLE ASSIGNMENTS
As discussed in Lesson#1, an assignment statement associates a value with a
variable. The key to an assignment statement is the symbol = which is known as
the assignment operator. For instance, the following assignment statement
assigns the integer value 5 to the variable A:
A=5
In other words, this statement binds the variable named A to the value 10. At this
point, the type of A is integer because it is bound to an integer value.
A variable may be assigned and reassigned as often as necessary. The type of a
variable will change if it is reassigned to an expression of a different type. For
example, the following statement changes the type of variable A into a string as
we re-assigned to a string:
A='Nur'
As shown below, you can also assign more than one variable in one statement.
This type of assignment is known as multiple assignments. In the following
screenshot, we assign x and y to the values of 2 and 6 in one assignment
statement (so we did multiple assignments):

Note we defined two variables in one assignment


statement. This is known as Multiple Assignment

You can also assign two variables to the same value at the same time and then
re-assign to different data types as shown below:

Note we defined two variables in one statement. But


as shown, the two variables have the same value:
both variables are equal to 15
Note we re-assigned the same variables into different
data types: a and b were integers before and equal to 15.
Now a and b are strings and equal to Nur

Writing Python Scripts Using IDE: Lesson 2

Nur Dirie Hersi Fursade, Ph.D.

Page 9

PROGRAM COMMENTS (SINGLE AND MULTIPLE LINES)


In a Python, anything after a # symbol is a comment. The symbol can be placed
at the beginning of a line or at the end of a statement. The interpreter will ignore
any thing following the symbol.
The following example shows two comments placed at the beginning of a line and
one placed at the end of a statement:

Comment in one line (note the #


at the beginning of the line)
Comment after a statement
Comment in one line

Comments are not part of the command, but rather intended as documentation
for anyone reading the code.
Multiline comments are also possible, and are enclosed by triple double-quote
symbols:

A triple quote initiates a multi-line


comment and another triple quote
ends the multiple-line comment

Note the triple-quotes at the beginning


and end of the comment

Writing Python Scripts Using IDE: Lesson 2

Nur Dirie Hersi Fursade, Ph.D.

Page 10

INTRODUCTION TO ESCAPE CODES


Escape codes are codes that are inserted inside string literals and start with a
backslash character (\). They are used to insert characters that are either
unprintable or have a special syntactic meaning to Python.
Escape Code Meaning:
Escape code

Meaning

\'

' (Single Quote)

\n

New Line

\\

Example:
Using escape codes, print the three letters a, b, and c in three different lines:

The newline character indicates further text will appear on a new line when
printed. When you use a print function, you get the actual printed meaning of the
escaped coded character.
Answer the following three questions without using Python:
a) Can you predict what will be the output of the following print function?
print('a\n\nb\n\nc\n')
b) Can you predict what will be the output of the following print function?
print('\\a\\b\\c')
c) Can you predict what will be the output of the following print function?
print('\'a\n\'b\n\'c')

Writing Python Scripts Using IDE: Lesson 2

Nur Dirie Hersi Fursade, Ph.D.

Page 11

Here are the answers:


a) Can you predict what will be the output of the following print function?
print('a\n\nb\n\nc\n')

Output

b) Can you predict what will be the output of the following print function?
print('\\a\\b\\c')

c) Can you predict what will be the output of the following print function?
print('\'a\n\'b\n\'c')

Writing Python Scripts Using IDE: Lesson 2

Nur Dirie Hersi Fursade, Ph.D.

Page 12

INTRODUCTION TO MATH MODULE


Python has a very extensive library of commands which are organized into
modules. A module is a collection of Python codes that we can use in other
programs. One of the available modules is especially useful for us is known as
math module.
The math modules contain the square root, sine, cosine, tangent, etc. which we
can use. To use them, however, it is necessary to import them from the math
module as shown below:
>>> from math import *
Then you can use the square root as shown below:

This line of code is used to import all


functions included in math module

Example
Close the Python Shell and open a new Shell. Then type the following:

The statement failed because sqrt


function is not known since we did not
import the math module

Before using the function, it necessary


to import the function in that session
Writing Python Scripts Using IDE: Lesson 2

Nur Dirie Hersi Fursade, Ph.D.

Page 13

SCRIPT GENERATING GEOMETRIC FIGURE


Create a script called square.py with the following commands (just copy and
paste into the editor):

print('Draw a square')
print(' ')
print(' * * * *')
print(' *
*')
print(' *
*')
print(' * * * *')

Save and execute the script. You should get the following output:

We have a square consisting of stars (*)

Writing Python Scripts Using IDE: Lesson 2

Nur Dirie Hersi Fursade, Ph.D.

Page 14

You might also like