Student
Solutions
Manual
To Accompany
An Introduction To Programming
Using Python
By David I. Schneider
,Contents
Chapter 2 Core Objects, Variables, Input, And Output
2.1 Numbers 9
2.2 Strings 10
2.3 Output 13
2.4 Lists, Tuples, And Files – An Introduction 14
Chapter 3 Structures That Control Flow
3.1 Relational And Logical Operators 15
3.2 Decision Structures 16
3.3 The While Loop 19
3.4 The For Loop 22
Chapter 4 Functions
4.1 Functions, Part 1 28
4.2 Functions, Part 2 32
Chapter 5 Processing Data
5.1 Processing Data, Part 1 37
5.2 Processing Data, Part 2 41
5.3 Processing Data With Dictionaries 47
Chapter 6 Miscellaneous Topics
6.1 Exception Handling 51
6.2 Selecting Random Values 52
6.3 Turtle Graphics 55
6.4 Recursion 64
Chapter 7 Object-Oriented Programming
7.1 Classes And Objects 66
7.2 Inheritance 71
Chapter 8 Graphical User Interface
8.1 Widgets 74
8.2 The Grid Geometry Manager 78
8.3 Writing Gui Programs 82
, Comments
Chapter 1
Section 1.1: The Most Important Questions In Section 1.1 Are As Follows:
Question: Many Programming Languages, Including Python, Use A Zero-Based Numbering System.
What Is A Zero- Based Numbering System? (See Page 3.)
Question: What Conventions Are Used To Show Keystrokes? (See Page 4.)
Question: How Can The Programs For The Examples In This Textbook Be Obtained? (See Page 4.)
Question: Where Will New Programs Be Saved? (See Page 4.)
Section 1.3: Flowcharts Appear Primarily In Chapter 3. At That Time, You May Want To Review The
Material On Flowcharts On Page 7.
Section 1.4
Figure 1.30 On Page 20 Illustrates A Most Important Aspect Of Python Programs; The Fact That
Indentation And Blocks Are Heavily Used To Delineate Different Parts Of Programs.
Of The Items In The Menu In Fig. 1.31 On Page 20, I Rely Heavily On The Recent Files Command. I Use
It To Get To The Folder I Am Currently Using To Store And Retrieve My Programs. I Often Open Any
Program In Or Near The Proper Folder. Then The Open Or New File Commands Will Default To That
Folder.
On Page 34 You Are Asked To Run A Program That Was Downloaded From The Pearson Website For
The Book. All Of The Programs Appearing In Examples Can Be Downloaded From That Website.
There Is Never Any Need For You To Manually Type In The Code For A Program In This Textbook. The
Website Also Contains All The Data Files Needed For The Exercises. These Programs And Data Files
Are Contained In The Subfolders “Ch2”, “Ch3”, “Ch4”, And So On. Each Program Has A Name Of The
Form
Chapter Number-Section Number–Example Number. For Instance, The Program In Chapter 3,
Section 1, Example 2 Is Contained In The Folder Ch3 And Has The Name 3-1-2.Py.
Chapter 2
Section 2.1
The First Example Containing A Program Is Example 1 On Page 24. To Open This Program In
Python, Click On Open In The Idle File Menu, Navigate To The Subfolder Ch2 Of The Programs
Folder, And Double-Click On 2-1-1.Py. The Next Time You Click On Open In The Idle File Menu,
Python Will Use The Folder Programs/Ch2 As The Default Folder. That Is, You Will Not Have To
Navigate To That Folder; Its Programs Will Automatically Appear.
A Number Whose Absolute Value Is Less Than 1 Can Be Written With Or Without A 0 Preceding Its
Decimal Point. Both Representations Are Treated The Same. That Is, 0.8 And .8 Are The Same
Number. Python Displays Such Numbers With A 0 Preceding The Decimal Point.
Mathematical Comment: 𝑥 ** .5 Has The Same Value As √𝑥.
Comment 4 Shows An Unexpected Feature Of The Round Function. Most People Expect The Number
2.5 To Be Rounded To 3, Not 2.
A Common Error Is To Forget To Include The Closing Parenthesis At The End Of A Print Function.
, Section 2.2
We Favor Using Double-Quotes To Delimit Strings Since Strings Sometimes Contain Contractions;
That Is, Words Such As It’s And Can’t.
Correction: On Page 41, The Value Of Int(-4.8) Should Be -4.
Section 2.3
Mastering The Format Method (Discussed On Pages 52 And 53) Is A Bit Challenging. However, Once
You Have Become Comfortable Using It, You Will Appreciate Its Power. It Is The Modern Way Of
Formatting Output Will Be The Preferred Method Of Formatting Output In This Book.
The Ljust, Rjust, And Center Methods Discussed On Page 51 Are Much Less Important Than The
Format Method. They Are Presented To Introduce The Concept Of A Field.
Section 2.4
The Material In The First Three Sections Of This Chapter Is Fundamental And Usually Is Introduced
Early In Computer Programming Books. I Made The Atypical Decision To Introduce Lists And Tuples
Earlier (In Section 2.4) Than Other Python Textbooks And Also To Show How To Fill Them With The
Contents Of A File. So Doing Allows Us To Solve Interesting And Non-Trivial Programs Early In The
Book. For Now, Just Think Of The Three-Line Method For Filling A List With The Contents Of A Text
File As Boilerplate. Later You Will Understand Exactly How The Three-Line Method Carries Out Its
Mission. With Many Python/Operating System Combinations, The Three Lines Can Be Replaced
With A Single Line Of The Following Form:
List Name = [Line. Rstrip() For Line In Open("Data.Txt", 'R')]
We Have Learned Four Ways To Fill A List. Let's Illustrate The Ways By Filling A List With The Names
Of The First Three Presidents.
(a) Pres = ["Washington", "Jefferson", "Adams"]
(b) Infile = Open ("Pres.Txt", 'R')
Pres = [Line. Rstrip() For Line In Infile]
Infile.Close
Where The Text File Pres.Txt Is Located In The Same Folder As The Program And Has Three
Lines With Each Line Containing The Name Of A President.
(c) Strpres = "Washington,Jefferson,Adams"
Pres = Strpres.Split(",")
(d) Pres = []
Pres.Append("Washington")
Pres.Append("Jefferson")
Pres.Append("Adams")
Lists Are Incredibly Useful Structures And Are Used Extensively Throughout This Book. The Join
Method (As Used In Example 3) Provides An Excellent Way To Display The Items In A List.
The Main Idea To Take Away From The Discussion Of Mutable Objects Is That Numbers, Strings, And
Tuples Are Handled Differently In Memory Than Are Lists. This Fact Will Rarely Affect The Programs
That You Write. However, It Will Give You Some Insights Into What Is Going On In The Background.
In Chapters 4 And 5, The Concept Of Mutability Becomes Important. Mutable Objects Can Have
Their Values Changed When Passed To Functions. Also, Mutable Objects Cannot Be Used As Keys In
Dictionaries.
Solutions
Manual
To Accompany
An Introduction To Programming
Using Python
By David I. Schneider
,Contents
Chapter 2 Core Objects, Variables, Input, And Output
2.1 Numbers 9
2.2 Strings 10
2.3 Output 13
2.4 Lists, Tuples, And Files – An Introduction 14
Chapter 3 Structures That Control Flow
3.1 Relational And Logical Operators 15
3.2 Decision Structures 16
3.3 The While Loop 19
3.4 The For Loop 22
Chapter 4 Functions
4.1 Functions, Part 1 28
4.2 Functions, Part 2 32
Chapter 5 Processing Data
5.1 Processing Data, Part 1 37
5.2 Processing Data, Part 2 41
5.3 Processing Data With Dictionaries 47
Chapter 6 Miscellaneous Topics
6.1 Exception Handling 51
6.2 Selecting Random Values 52
6.3 Turtle Graphics 55
6.4 Recursion 64
Chapter 7 Object-Oriented Programming
7.1 Classes And Objects 66
7.2 Inheritance 71
Chapter 8 Graphical User Interface
8.1 Widgets 74
8.2 The Grid Geometry Manager 78
8.3 Writing Gui Programs 82
, Comments
Chapter 1
Section 1.1: The Most Important Questions In Section 1.1 Are As Follows:
Question: Many Programming Languages, Including Python, Use A Zero-Based Numbering System.
What Is A Zero- Based Numbering System? (See Page 3.)
Question: What Conventions Are Used To Show Keystrokes? (See Page 4.)
Question: How Can The Programs For The Examples In This Textbook Be Obtained? (See Page 4.)
Question: Where Will New Programs Be Saved? (See Page 4.)
Section 1.3: Flowcharts Appear Primarily In Chapter 3. At That Time, You May Want To Review The
Material On Flowcharts On Page 7.
Section 1.4
Figure 1.30 On Page 20 Illustrates A Most Important Aspect Of Python Programs; The Fact That
Indentation And Blocks Are Heavily Used To Delineate Different Parts Of Programs.
Of The Items In The Menu In Fig. 1.31 On Page 20, I Rely Heavily On The Recent Files Command. I Use
It To Get To The Folder I Am Currently Using To Store And Retrieve My Programs. I Often Open Any
Program In Or Near The Proper Folder. Then The Open Or New File Commands Will Default To That
Folder.
On Page 34 You Are Asked To Run A Program That Was Downloaded From The Pearson Website For
The Book. All Of The Programs Appearing In Examples Can Be Downloaded From That Website.
There Is Never Any Need For You To Manually Type In The Code For A Program In This Textbook. The
Website Also Contains All The Data Files Needed For The Exercises. These Programs And Data Files
Are Contained In The Subfolders “Ch2”, “Ch3”, “Ch4”, And So On. Each Program Has A Name Of The
Form
Chapter Number-Section Number–Example Number. For Instance, The Program In Chapter 3,
Section 1, Example 2 Is Contained In The Folder Ch3 And Has The Name 3-1-2.Py.
Chapter 2
Section 2.1
The First Example Containing A Program Is Example 1 On Page 24. To Open This Program In
Python, Click On Open In The Idle File Menu, Navigate To The Subfolder Ch2 Of The Programs
Folder, And Double-Click On 2-1-1.Py. The Next Time You Click On Open In The Idle File Menu,
Python Will Use The Folder Programs/Ch2 As The Default Folder. That Is, You Will Not Have To
Navigate To That Folder; Its Programs Will Automatically Appear.
A Number Whose Absolute Value Is Less Than 1 Can Be Written With Or Without A 0 Preceding Its
Decimal Point. Both Representations Are Treated The Same. That Is, 0.8 And .8 Are The Same
Number. Python Displays Such Numbers With A 0 Preceding The Decimal Point.
Mathematical Comment: 𝑥 ** .5 Has The Same Value As √𝑥.
Comment 4 Shows An Unexpected Feature Of The Round Function. Most People Expect The Number
2.5 To Be Rounded To 3, Not 2.
A Common Error Is To Forget To Include The Closing Parenthesis At The End Of A Print Function.
, Section 2.2
We Favor Using Double-Quotes To Delimit Strings Since Strings Sometimes Contain Contractions;
That Is, Words Such As It’s And Can’t.
Correction: On Page 41, The Value Of Int(-4.8) Should Be -4.
Section 2.3
Mastering The Format Method (Discussed On Pages 52 And 53) Is A Bit Challenging. However, Once
You Have Become Comfortable Using It, You Will Appreciate Its Power. It Is The Modern Way Of
Formatting Output Will Be The Preferred Method Of Formatting Output In This Book.
The Ljust, Rjust, And Center Methods Discussed On Page 51 Are Much Less Important Than The
Format Method. They Are Presented To Introduce The Concept Of A Field.
Section 2.4
The Material In The First Three Sections Of This Chapter Is Fundamental And Usually Is Introduced
Early In Computer Programming Books. I Made The Atypical Decision To Introduce Lists And Tuples
Earlier (In Section 2.4) Than Other Python Textbooks And Also To Show How To Fill Them With The
Contents Of A File. So Doing Allows Us To Solve Interesting And Non-Trivial Programs Early In The
Book. For Now, Just Think Of The Three-Line Method For Filling A List With The Contents Of A Text
File As Boilerplate. Later You Will Understand Exactly How The Three-Line Method Carries Out Its
Mission. With Many Python/Operating System Combinations, The Three Lines Can Be Replaced
With A Single Line Of The Following Form:
List Name = [Line. Rstrip() For Line In Open("Data.Txt", 'R')]
We Have Learned Four Ways To Fill A List. Let's Illustrate The Ways By Filling A List With The Names
Of The First Three Presidents.
(a) Pres = ["Washington", "Jefferson", "Adams"]
(b) Infile = Open ("Pres.Txt", 'R')
Pres = [Line. Rstrip() For Line In Infile]
Infile.Close
Where The Text File Pres.Txt Is Located In The Same Folder As The Program And Has Three
Lines With Each Line Containing The Name Of A President.
(c) Strpres = "Washington,Jefferson,Adams"
Pres = Strpres.Split(",")
(d) Pres = []
Pres.Append("Washington")
Pres.Append("Jefferson")
Pres.Append("Adams")
Lists Are Incredibly Useful Structures And Are Used Extensively Throughout This Book. The Join
Method (As Used In Example 3) Provides An Excellent Way To Display The Items In A List.
The Main Idea To Take Away From The Discussion Of Mutable Objects Is That Numbers, Strings, And
Tuples Are Handled Differently In Memory Than Are Lists. This Fact Will Rarely Affect The Programs
That You Write. However, It Will Give You Some Insights Into What Is Going On In The Background.
In Chapters 4 And 5, The Concept Of Mutability Becomes Important. Mutable Objects Can Have
Their Values Changed When Passed To Functions. Also, Mutable Objects Cannot Be Used As Keys In
Dictionaries.