Python NH Lesson#2
Python NH Lesson#2
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
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:
Page 3
Page 4
You get back to the Python Shell with the output of your program as shown below
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.
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)
Solution:
Start IDLE, click FILE, NEW FILE, type the code shown below in the editor, save
the file as example2.py
Page 6
Then click FILE, RUN MODULE in the editor, enter the requested values and you
will get the following screenshot:
Page 7
How to fix the above error? We removed the white spaces before the print as
shown below
We will talk more about indentations when we cover IF statements and loops
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):
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:
Page 9
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:
Page 10
Meaning
\'
\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')
Page 11
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')
Page 12
Example
Close the Python Shell and open a new Shell. Then type the following:
Page 13
print('Draw a square')
print(' ')
print(' * * * *')
print(' *
*')
print(' *
*')
print(' * * * *')
Save and execute the script. You should get the following output:
Page 14