Python: Simple Programs - Creating and Executing

Python: Simple Programs - Creating and Executing ? Typing expressions and individual instructions into the interpreter is fine for

learning how things work in Python, but this is not programming ? You could write a program by entering a one instruction at a time to the

interpreter, letting it execute, then repeating until all the program's statements have been entered, but this is not good for several reasons: 1. If the program has multiple lines of output, they will be interspersed with

the statements that created them 2. You don't have a saved program - to rerun it you'd have to type it in all

over again ? You could also write a function (to be discussed later) in the interpreter, and

then call it ? But again, you don't have a saved program

? The simplest Python program is created by simply creating a file of statements ? This can be done using your favorite editor, or Idle ? Python program files must be text files ? Python program file names should use the extension .py

? Strictly speaking, such a program is called a script ? To create and execute a program using Idle:

1. Creating the program (a) Start Idle (b) Open a new file using File/New (or an existing one using Open) (c) In the new window, simply type in the program instructions (or edit the existing file) (d) Save the file using File/Save As (or File/Save for an existing file)

2. Executing the program (a) Select Run/Run Module (b) The program will execute in the interpreter window

1

Python: Simple Programs - Creating and Executing (2)

? To create and execute a program using general tools:

1. Creating the program (a) Create a file of Python statements using a text editor of your choice (e.g., Windows Notepad, Mac TextEdit, Linux Text Editor) ? Do NOT use a word processor (e.g., MS Word) (b) Save the file with a .py extension

2. Executing the program (a) From the command line: ? Type in python3 f ilename.py ? It's easiest if you are in the same directory/folder where the file was saved ? If you are in a different directory, you can move to the file's directory (using the cd command), or attach a path to the file name (e.g., python3 djmoon/courses/python/lab4/dmooney-lab4.py) (b) From within the interpreter: ? Type in exec(open( quote f ilename endquote.py).read()) ? For example: exec(open('tst.py').read())

? A sample program:

#*************************************

#

A simple example program

*

#

Author: D Mooney

*

#*************************************

#Calculates area of a circle given diameter

import math

diameter = input('Enter diameter of a circle: ') diameter = float(diameter) radius = diameter / 2 area = math.pi * math.pow(radius, 2) print('The area of circle with diameter ', diameter, ' = ', area)

2

Python: Simple Programs - Creating and Executing (3)

? Many programmers encapsulate a program in terms of a function (about which we will have much discussion about later in the course)

? The above example coded as a function would look like this:

#*************************************

#

A simple example program

*

#

Author: D Mooney

*

#*************************************

def main(): #Calculates area of a circle given diameter

import math

diameter = input('Enter diameter of a circle: ') diameter = float(diameter) radius = diameter / 2 area = math.pi * math.pow(radius, 2) print('The area of circle with diameter ', diameter, ' = ', area)

main()

? The program starts with def main(): and includes the indented lines that follow

? The last line main() causes the program to execute

3

Python: Simple Programs - Program Structure and Conventions ? The recommended structure of a Python program is described in PEP 8 (Python

Enhancement Proposal 8) ? There is a link in the Resources page of the course site

? Rules and conventions will be presented throughout the course as appropriate ? For now, the following are relevant:

1. Statements should have no leading white space (indentation rules will be discussed later)

2. Lines should be no longer than 79 characters ? If a line is longer, it can be broken using a backslash (\) ? The continued line should be indented (the rule re preceding white space does not apply here)

3. Import statements should appear first 4. Comments should be used liberally

? Comments start with a pound sign (#) ? Everything from the pound to the end of the line is ignored by the

interpreter ? See course site for guidelines for commenting class work 5. Variable names ? Should be meaningful (unlike in algebra) ? Use all lower case with individual words separated by underscores:

area, length of time, last name

4

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download