Lambert FunPython 3e Ch02 PowerPoint v2
Lambert FunPython 3e Ch02 PowerPoint v2
Python: First
Programs 3e
Chapter 2: Software
Development, Data Types,
and Expressions
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 1
Chapter Objectives
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 2
The Software Development Process (1
of 4)
• Software development: process of planning and organizing a program
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 3
The Software Development Process (2
of 4)
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 4
The Software Development Process (3
of 4)
• Programs rarely work as hoped
the first time they are run
• Must perform extensive and
careful testing
• The cost of developing software
is not spread equally over the
phases
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 7
Data Types (1 of 2)
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 8
Data Types (2 of 2)
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 9
String Literals (1 of 2)
• Double-quoted strings are handy for composing strings that contain single quotation
marks or apostrophes:
>>> "I'm using a single quote in this string!”
"I'm using a single quote in this string!”
>>> print("I'm using a single quote in this string!")
I'm using a single quote in this string!
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 10
String Literals (2 of 2)
• When you evaluate a string in Python without the print function, you can see the
literal for the newline character, \n, embedded in the result:
>>> """This very long sentence extends
all the way to the next line."""
'This very long sentence extends\nall the way to the next line.'
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 11
Escape Sequences
Escape Meaning
Sequence
\b Backspace
\n Newline
\t Horizontal tab
\\ The \ character
\' Single quotation mark
\" Double quotation mark
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 12
String Concatenation
• You can join two or more strings to form a new string using the
concatenation operator +
>>> "Hi " + "there, " + "Ken!"
'Hi there, Ken!'
• The * operator allows you to build a string by repeating another string
a given number of times
>>> " " * 10 + "Python"
' Python'
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 13
Variables and the Assignment
Statement
• A variable associates a name with a(1
value of 3)
− Makes it easy to remember and use later in the program
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 14
Variables and the Assignment
Statement
(2
• Programmers use all uppercase letters of 3)constants (variables that contain
for symbolic
values that the program never changes)
− Examples: TAX_RATE and STANDARD_DEDUCTION
• Variables receive initial values and can be reset to new values with an assignment
statement
<variable name> = <expression>
− Subsequent uses of the variable name in expressions are known as variable references
• Example:
>>> firstName = "Ken"
>>> secondName = "Lambert"
>>> fullName = firstName + " " + secondName
>>> fullName
'Ken Lambert'
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 15
Variables and the Assignment
Statement
(3 of 3)
• Variables serve two purposes:
− Help the programmer keep track of data that change over time
− Allow the programmer to refer to a complex piece of information
with a simple name (a process called abstraction)
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 16
Activity 2.1: Knowledge Check
1. What is a literal?
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 17
Activity 2.1: Knowledge Check
Answers
1. What is a literal?
Answer: A literal is the way a value of a data type looks to a
programmer.
2. Which operator allows you to build a string by repeating another
string a given number of times?
Answer: The * operator
3. Which types of characters are allowed in variable names?
Answer: Letters, digits, or underscores (_)
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 18
Program Comments and Docstrings (1
of 3)
• Program comment
− A piece of program text that the computer ignores but that provides useful
documentation
• Docstring
− A multi-line string of the form:
"""
Program: circle.py
Author: Ken Lambert
Last date modified: 10/10/22
The purpose of this program is to compute the area of a circle. The input
is an integer or floating-point number representing the radius of the
circle. The output is a floating-point number labeled as the area of the
circle.
"""
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 19
Program Comments and Docstrings (2
of 3)
• End-of-line comments
− Begin with the # symbol and extend to the end of a line
− Might explain the purpose of a variable or the strategy used by a
piece of code
− Example:
>>> RATE = 0.70 # Conversion rate for Canadian to US
dollars
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 20
Program Comments and Docstrings (3
of 3)
• Tips related to comments and docstrings:
− Begin a program with a statement of purpose and other
information helpful to programmers
− Accompany a variable definition with a comment that explains the
variable’s purpose
− Precede major segments of code with brief comments that explain
their purpose
− Include comments to explain the workings of complex or tricky
sections of code
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 21
Numeric Data Types and Character
Sets
• The first applications of computers were to crunch numbers
• The use of numbers in many applications is still very important
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 22
Integers
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 23
Floating-Point Numbers (1 of 2)
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 24
Floating-Point Numbers (2 of 2)
1
−3
0.00378 3.78e−3 3.78 ×10
3.78 times 10 to the minus
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 25
Character Sets
• Python character literals look like string literals and are of type string
− They belong to several different character sets, among them the ASCII set and the
Unicode set
• Example:
>>> ord('a')
97
>>> ord('A')
65
>>> chr(65)
'A'
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 26
Activity 2.2 Discussion
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 27
Expressions
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 28
Arithmetic Expressions (1 of 3)
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 29
Arithmetic Expressions (2 of 3)
• Precedence rules:
− ** has the highest precedence and is evaluated first
− Unary negation is evaluated next
− *, /, and % are evaluated before + and −
− + and − are evaluated before =
− With two exceptions, operations of equal precedence are left associative, so they
are evaluated from left to right
** and = are right associative
− You can use () to change the order of evaluation
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 30
Arithmetic Expressions (3 of 3)
• When both operands of an expression are of the same numeric type, the resulting
value is also of that type
• When each operand is of a different type, the resulting value is of the more general
type
• Example:
>>> 3 + 4 * \
2 ** 5
131
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 31
Mixed-Mode Arithmetic and Type
Conversions (1 of 4)
• Mixed-mode arithmetic involves integers and floating-point numbers:
>>> 3.14 * 3 ** 2
28.26
• You must use a type conversion function when working with the input of
numbers
− It is a function with the same name as the data type to which it converts
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 32
Mixed-Mode Arithmetic and Type
Conversions (2 of 4)
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 33
Mixed-Mode Arithmetic and Type
Conversions (3 of 4)
• Note that the int function converts a float to an int by truncation,
not by rounding
>>> int(6.75)
6
>>> round(6.75)
7
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 34
Mixed-Mode Arithmetic and Type
Conversions (4 of 4)
• Type conversion also occurs in the construction of strings from numbers and other
strings
>>> profit = 1000.55
>>> print('$' + profit)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'float' objects
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 35
Using Functions and Modules
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 36
Calling Functions: Arguments and
Return Values
• A function is chunk of code that can be called by name to perform a task
• When a function completes its task, it may return a value back to another part of the
program
• Functions like abs and round from the _builtin_ module are always available to
use
• The math module includes functions that perform basic mathematical operations
• To use a resource from a module, you write the name of a module as a qualifier,
followed by a dot (.) and the name of the resource
− Example: math.pi
>>> math.pi
3.1415926535897931
>>> math.sqrt(2)
1.4142135623730951
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 38
The math Module (2 of 2)
• You can avoid the use of the qualifier with each reference by
importing the individual resources
>>> from math import pi, sqrt
>>> print(pi, sqrt(2))
3.14159265359 1.41421356237
• You may import all of a module’s resources to use without the qualifier
− Example: from math import *
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 39
The Main Module
• To differentiate this script from the other modules in a program, we call it the main
module
− Like any module, the main module can be imported
>>> import taxform
Enter the gross income: 120000
Enter the number of dependents: 2
The income tax is $20800.0
• After importing a main module, view its documentation by running the help function
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 40
Program Format and Structure
• Start with a comment including the author’s name, the purpose of the program, and
other relevant information
− In a docstring
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 41
Running a Script from a Terminal
Command Prompt (1 of 4)
• A way to run a Python script:
− Open a terminal command prompt window
− Click in the “Type here to search” box, type Command Prompt,
and click Command Prompt in the list
− Navigate or change directories until the prompt shows the
directory that contains the Python script
− Run the script by entering:
python3 scriptname.py
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 42
Running a Script from a Terminal
Command Prompt (2 of 4)
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 44
Running a Script from a Terminal
Command Prompt (4 of 4)
• Python installations enable you to launch Python scripts by double-
clicking the files from the OS’s file browser
− May require .py file type to be set
− Fly-by-window problem: window will close automatically
Solution: Add an input statement at end of the script that
pauses until the user presses the enter or return key
>>> input("Please press enter or return to quit the
program. ")
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 45
Self Assessment
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 46
Summary
Lambert, Fundamentals of Python: First Programs, 3rd Edition. © 2024 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part. 47