Fundamentals of Python: First Programs Second Edition
Fundamentals of Python: First Programs Second Edition
Fundamentals of Python: First Programs Second Edition
Second Edition
Chapter 1
Introduction
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use
as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for
classroom use. 1
Objectives
1.1 Describe the basic features of an algorithm
1.2 Explain how hardware and software collaborate in a
computer’s architecture
1.3 Give a brief history of computing
1.4 Compose and run a simple Python program
© 2018 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain
product or service or otherwise on a password-protected website for classroom use.
Two Fundamental Ideas of Computer Science:
Algorithms and Information Processing
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
3
Algorithms (1 of 2)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
4
Algorithms (2 of 2)
• Features of an algorithm:
• Consists of a finite number of instructions
• Each individual instruction is well defined
- Action described by the instruction can be performed effectively or be executed by a
computing agent
• Describes a process that eventually halts after arriving at a solution to a problem
• Solves a general class of problems
• Computers can be designed to run a small set of algorithms for performing
specialized tasks
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
5
Information Processing
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
6
The Structure of a Modern Computer System
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
7
Computer Hardware (1 of 3)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
8
Computer Hardware (2 of 3)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
9
Computer Hardware (3 of 3)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
10
Computer Software (1 of 3)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
11
Computer Software (2 of 3)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
12
Computer Software (3 of 3)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
13
A Not-So-Brief History of Computing Systems
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
14
Before Electronic Digital Computers (1 of 4)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
15
Before Electronic Digital Computers (2 of 4)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
16
Before Electronic Digital Computers (3 of 4)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
17
Before Electronic Digital Computers (4 of 4)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
18
The First Electronic Digital
Computers (1940-1950)
• Late 1930s: Claude Shannon wrote paper titled “A Symbolic Analysis of Relay
and Switching Circuits”
• 1940s:
• Mark I (electromechanical)
• ENIAC (Electronic Numerical Integrator and Calculator)
• ABC (Atanasoff-Berry Computer)
• Colossus by a group working under Alan Turing
• John von Neumann: first memory-stored programs
• Mainframe computers consisted of vacuum tubes, wires, and plugs, and filled
entire rooms
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
19
The First Programming Languages (1950-1965)
• The first assembly languages had operations like ADD and OUTPUT
• Programmers entered mnemonic codes for operations at keypunch machine
• Card reader—translated holes in cards to patterns in computer’s memory
• Assembler—translated application programs in memory to machine code
• Compiler – translated programs to machine code
• High-level programming languages: FORTRAN, LISP, COBOL
• common feature: abstraction
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
20
Integrated Circuits, Interaction, and
Timesharing (1965-1975)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
21
Personal Computing and Networks (1975-1990)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
22
Consultation, Communication, and
E-Commerce (1990-2000) (1 of 2)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
23
Consultation, Communication, and
E-Commerce (1990-2000) (2 of 2)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
24
Mobile Applications and Ubiquitous
Computing (2000-present)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
25
Getting Started with Python Programming
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use. 26
Running Code in the Interactive Shell (1 of 2)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
27
Running Code in the Interactive Shell (2 of 2)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
28
Input, Processing, and Output (1 of 5)
• Programs usually accept inputs from a source, process them, and output results
to a destination
• In terminal-based interactive programs, these are the keyboard and terminal display
• In Python, inputs are Python expressions or statements
• Outputs are the results displayed in the shell
• Programmers can also force output of a value by using the print function
• print (<expression>)
• Example:
>>>print (“Hi there”)
Hi there
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
29
Input, Processing, and Output (2 of 5)
• The following example receives an input string from the user and saves it for
further processing:
>>> name = input(“Enter your name:”)
Enter your name: Ken Lambert
>>> name
‘Ken Lambert’
>>> print(name)
Ken Lambert
>>>
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
30
Input, Processing, and Output (3 of 5)
• The input function always builds a string from the user’s keystrokes and returns
it to the program
• Strings that represent numbers must be converted from strings to appropriate
number types
• Two type conversion functions: int (for integers) and float (for floating-point numbers)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
31
Input, Processing, and Output (4 of 5)
• The next session inputs two integers and displays their sum:
>>> first = int(input(“Enter the first number: ”))
Enter the first number: 23
>>> second = int(input(“Enter the second number:”))
Enter the second number: 44
>>> print(“The sum is”, first + second)
The sum is 67
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use. 32
Input, Processing, and Output (5 of 5)
input(<a string prompt>) Displays the string prompt and waits for
keyboard input. Returns the string of characters
entered by the user.
print(<expression>, Evaluates the expressions and displays them,
...,<expression>) separated by one space, in the console window.
<string 1> + <string 2> Glues the two strings together and returns the
result.
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
33
Editing, Saving, and Running a Script (1 of 3)
• We can then run Python program files or scripts within IDLE or from the OS’s
command prompt
• Run within IDLE using menu option, F5 (Windows), or Control+F5 (Mac or Linux)
• Python program files use .py extension
• Running a script from IDLE allows you to construct some complex programs,
test them, and save them in program libraries to reuse or share with others
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
34
Editing, Saving, and Running a Script (2 of 3)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
35
Editing, Saving, and Running a Script (3 of 3)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
36
Behind the Scenes: How Python Works
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
37
Detecting and Correcting Syntax Errors (1 of 2)
• The next statement attempts to print the value of the correctly spelled variable:
>>> print(length)
SyntaxError: unexpected indent
• Final example, programmer attempts to add two numbers, but forgets to
include the second one:
>>> 3 +
SyntaxError: invalid syntax
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
39
Chapter Summary (1 of 3)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
40
Chapter Summary (2 of 3)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
41
Chapter Summary (3 of 3)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license
distributed with a certain product or service or otherwise on a password-protected website for classroom use.
42