MyFirstProgram Assignment

kavya
Lesson1-IntroToProgrammingWithPython.pdf

An Introduction to Programming Using Python David I. Schneider

An Introduction to Computing and Problem Solving

Lesson 1

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Questions and Answers

• How do we communicate with the computer?

– Programming languages

• How do we get computers to perform complicated tasks?

– Tasks are broken down into a sequence of instructions

• Why Python?

– Powerful, easy to download, write and read

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Questions and Answers • How did the language Python get its name?

– Named for the British comedy group Monty Python (really!)

• Book uses the editor IDLE to create programs. How did IDLE get its name?

– Stands for Integrated DeveLopment Environment

• What is an interpreted language?

– Uses an interpreter, translates high-level language one statement at a time into machine language and then runs

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Questions and Answers

• What are the meanings of the terms “programmer” and “user”?

– Programmer: a person who solves problems by writing programs on a computer

– User: any person who runs a program

• What is the meaning of the term “code”?

– Python statements that the programmer writes

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Questions and Answers

• Are there certain characteristics that all programs have in common?

– Input, processing, output

• What are the meanings of the terms “hardware” and “software”?

– Hardware: physical components of the computer

– Software: the programs

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Questions and Answers

• How are problems solved with a program?

– Step-by-step procedure devised to process given data and produce requested output.

• What is a zero-based numbering system?

– Numbering begins with zero instead of one

• Prerequisites to learning Python?

– Be familiar with how folders and files are managed

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Questions and Answers

• What is an example of a program developed in this textbook?

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

FIGURE 1.1 A possible output for a program in Lesson 2.

Questions and Answers

• How does the programmer create such a program?

– About ten lines of code that search a text file named USpres.txt and extract the requested names.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Questions and Answers

• What conventions are used to show keystrokes?

– key1+key2 means “hold down key1 and then press key2”

• Ctrl+C places selected material into the Clipboard

– key1/key2 means “Release key1 and then press key2”

• Alt/F opens the FILE menu on a menu bar.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Questions and Answers

• How can the programs for the examples in this textbook be obtained?

– See the preface for information

• Where will new programs be saved?

– Create a special folder to hold your programs

• Where can I research questions I have about Python?

– Documentation at https://www.python.org/doc/

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Program Development Cycle

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

FIGURE 1.2 The problem solving process.

Program Planning

1. Analyze: Define the problem.

2. Design: Plan the solution to the problem.

3. Code: Translate the algorithm into a programming language.

4. Test and correct: Locate and remove any errors in the program.

5. Complete the documentation: Organize all the material that describes the program.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Programming Tools

• Algorithms

• Flowcharts

• Pseudocode

• Hierarchy charts

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Algorithm Development

Algorithm to determine number of stamps for a letter

• Rule of thumb: 1 stamp for every 5 sheets of paper

1. Request sheets of paper

2. Divide by 5

3. Round quotient up to next whole number

4. Reply with number of stamps

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Problem Solving for Stamps

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

FIGURE 1.3 The problem solving process for the stamp problem.

Flowcharts

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Example Flowchart

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

FIGURE 1.4 Flowchart for the postage-stamp

problem.

Pseudocode

• Abbreviated plain English version of actual computer code

• Symbols used in flowcharts replaced by English-like statements

• Allows programmer to focus on steps required to solve problem

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Example Pseudocode

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

FIGURE 1.5 Pseudocode for the postage stamp problem.

Hierarchy Chart

• Shows the overall program structure

• Depict organization of program, omit specific processing logic

• Describe what each part, or module, of the program does

• Each module subdivided into a succession of submodules

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Example Hierarchy Chart

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

FIGURE 1.6 Hierarchy chart for the postage-stamp problem.

Decision Structure

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

FIGURE 1.7 Pseudocode and flowchart for a decision structure.

Direction of Numbered NYC Streets Algorithm

• Problem: Given street number of one-way street in New York City, decide direction of street, either eastbound or westbound.

• Discussion: There is a simple rule to tell the direction of a one-way street in New York City: Even-numbered streets run eastbound.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Direction of Numbered NYC Streets Algorithm

• Input: Street number.

• Processing: Decide if the street number is divisible by 2.

• Output: “Eastbound” or “Westbound”.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Direction of Streets Algorithm

FIGURE 1.8 Flowchart for the

numbered New York City

streets problem.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Direction of Streets Algorithm

FIGURE 1.9 Pseudocode for the numbered New York City streets problem.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Direction of Streets Algorithm

FIGURE 1.10 Hierarchy chart for the numbered New York City streets problem.

Repetition Structure

• A programming structure that executes instructions many times

– Repetition structure

– Looping structure

• Need a test (or condition) to tell when the loop should end

– Check condition before each pass through loop

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Direction of Streets Algorithm

FIGURE 1.11 Pseudocode and flowchart for a loop.

Class Average Algorithm

• Problem: Calculate and report the average grade for a class.

• Discussion: Average grade equals sum of all grades divided by number of students.

– Need loop to read and then add (accumulate) grades for each student in class.

– Inside the loop, we also need to total (count) number of students in class.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Class Average Algorithm

• Input: Student grades.

• Processing: Find the sum of the grades; count the number of students; calculate average grade = sum of grades / number of students.

• Output: Average grade

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Class Average Algorithm

FIGURE 1.12 Flowchart for the class average problem.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Class Average Algorithm

FIGURE 1.13 Pseudocode for the class average problem.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Class Average Algorithm

FIGURE 1.14 Hierarchy chart for the class average problem.

How to get Python compiler

• Go to: https://www.python.org/downloads/

• Follow instruction’s on how to install python on your favorite Operating system.

• If this is your first time using a programming compiler you are encourage to do the tutorials.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Starting IDLE

• Windows: Invoke with double click of

• MAC: Open Finder, select Applications, select the Utilities folder, select Terminal, and then enter IDLE at the prompt

• LINUX and UNIX: usually be found at /usr/bin/idle3

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Starting IDLE

FIGURE 1.16 The Python shell.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Starting IDLE

FIGURE 1.17 The Python shell after the expression 2 + 3 has been evaluated.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Starting IDLE

FIGURE 1.18 The Python shell after the statement print(“hello CS625 Class ") has been executed.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

A Python Code Editor Walkthrough

FIGURE 1.19 The File drop-down list.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

A Python Code Editor Walkthrough

FIGURE 1.20 The code editor window generated after New Window is clicked on.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

A Python Code Editor Walkthrough

FIGURE 1.21 The code editor window containing a three-line Python program.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

A Python Code Editor Walkthrough

FIGURE 1.22 A Save As dialog box.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

A Python Code Editor Walkthrough

FIGURE 1.23 The code editor window containing a three-line Python program.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

A Python Code Editor Walkthrough

FIGURE 1.24 Press the F5 key to execute. The outcome of the Python program in Fig. 1.22.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

A Python Code Editor Walkthrough

FIGURE 1.25 A Save message box.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

An Open-a-Program Walkthrough

FIGURE 1.26 An Open dialog box.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Click on desired program

An Open-a-Program Walkthrough

FIGURE 1.27 Example 10 of Section 3.4.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

An Open-a-Program Walkthrough

FIGURE 1.28 Request for input from Example 10 of Section 3.4.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

An Open-a-Program Walkthrough

FIGURE 1.29 Complete output for Example 10 of Section 3.4.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Block-Structure

Figure 1.30 Example 10 of Section 3.4.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

The File Drop-down Menu

FIGURE 1.31 The File drop-down menu.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.