McMullen ProgwPython 1e Mod04 PowerPoint
McMullen ProgwPython 1e Mod04 PowerPoint
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved.
May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Module Objectives (1 of 3)
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Module Objectives (2 of 3)
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.1 Primitive Data Types (1 of 3)
• Data types
• Data type: a way of categorizing data
• Primitive data types
• Primitive data types are built into a programming language
• Programming language knows how to allocate memory for storing these data types
• Programming language knows how to manipulate that type of data
• Function: a named procedure that performs a specific task, such as manipulating
data
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.1 Primitive Data Types (2 of 3)
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.1 Primitive Data Types (3 of 3)
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 4.1: Breakout Groups:
Primitive Data Types “20 Guesses” Game
1. Work in pairs or small groups.
2. One person should choose a primitive data type from Figure 4-2, and then the other(s) can
ask “Yes”-or-“No” questions until they correctly guess which data type the first has in mind.
3. Swap roles and repeat the game. Who in the class can guess correctly after the fewest
number of questions?
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.2 Numeric Data Types (1 of 4)
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.2 Numeric Data Types (2 of 4)
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.2 Numeric Data Types (3 of 4)
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.2 Numeric Data Types (4 of 4)
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 4.2: Knowledge Check
1. Python knows how to allocate memory for and manipulate _____ data types.
2. True or False: Add-on libraries of program code are often sources of useful primitive data types.
3. Floating-point numbers are stored in either 4 or 8 bytes of memory in _____, which resembles scientific
notation.
4. True or False: The most appropriate data type for variables that hold values representing money and
that will be used in financial transaction calculations is the integer type.
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 4.2: Knowledge Check Answers
1. Python knows how to allocate memory for and manipulate _____ data types.
Answer: primitive
2. True or False: Add-on libraries of program code are often sources of useful primitive data types.
Answer: False
3. Floating-point numbers are stored in either 4 or 8 bytes of memory in _____, which resembles scientific
notation.
Answer: E notation
4. True or False: The most appropriate data type for variables that hold values representing money and
that will be used in financial transaction calculations is the integer type.
Answer: False
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.3 Mathematical Expressions (1 of 5)
• Arithmetic operators
• Expression: a programming statement that has a value and usually includes one or
more arithmetic operators and one or more operands
• Python arithmetic operators include + (addition), - (subtraction), * (multiplication), /
(division), // (integer division), and % (modulo division)
• Sample statement that stores the result of a calculation in a variable:
discount_price = album_price – 2.99
• Remember that:
• When integer division (with //) produces a fractional part, it may not be included in
the result
• Modulo division (with %) produces the remainder of a division operation
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.3 Mathematical Expressions (2 of 5)
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.3 Mathematical Expressions (3 of 5)
• Order of operations
• The order of operations specifies the sequence in which to perform arithmetic
operations
• Do the math in parentheses first
• Carry out exponentiation and roots
• Perform multiplication, floating-point division, integer division, and modulo division
from left to right
• Execute addition and subtraction from left to right
• Parentheses can be added around the operation you wish to perform first to alter the
order of operations
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.3 Mathematical Expressions (4 of 5)
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 4.3: Knowledge Check
1. A programming statement with a value that includes one or more arithmetic operators and
one or more operands is a(n) _____.
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 4.3: Knowledge Check Answers
1. A programming statement with a value that includes one or more arithmetic operators and
one or more operands is a(n) _____.
Answer: expression
Answer: True
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.3 Mathematical Expressions (5 of 5)
• Compound operators
• Compound operators provide handy shortcuts for some common arithmetic operations
• Sample Python expressions using compound operators (counter is initialized to 10):
• counter += 1 # Result is 11
• counter -= 1 # Result is 9
• counter *= 2 # Result is 20
• counter /= 2 # Result is 5
• counter %= 2 # Result is 0
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 4.4: Breakout Groups:
Mathematical Expressions Practice
1. Form pairs or small groups of students.
3. To identify some common formulas to try, refer to an algebra text or an online reference
(e.g., review Algebraic Formula Sheet (web pdf), or search for “common algebraic
formulas” using a search engine) if necessary.
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.4 Numeric Data Type Conversion (1 of 3)
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.4 Numeric Data Type Conversion (2 of 3)
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.4 Numeric Data Type Conversion (3 of 3)
• Rounding quirks
• Programming languages have some quirks when it comes to numeric data
• E.g., dividing 1 // 2 using Python's integer division operator results in the value 0
• Odd results may stem from a rounding quirk related to the computer’s reliance on binary
numbers
• Binary representation of a floating-point number is an approximation because binary
numbers don’t yield the same fractions as decimals
• Math functions can be used to truncate or round numbers
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.5 Formatting Output (1 of 4)
• Formatted output
• Converting floating-point data to integers risks significant data loss
• You can format output to eliminate trailing zeros or decimal places without changing the
actual data in a variable
• E.g., the value 4.43050 can be displayed as 4, 4.4, 4.305000, or 4.431 while storing
the original value of 4.43050 in a variable
• Formatting parameters
• Python's unique syntax allows you to control several formatting parameters
• Type: integer, floating point, decimal, binary, hexadecimal, character, or E notation
• Precision: places displayed after the decimal point
• Width: number of spaces allocated to the output (e.g., for outputting table columns)
• Alignment: left or right within a specified width
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.5 Formatting Output (2 of 4)
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.5 Formatting Output (3 of 4)
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4.5 Formatting Output (4 of 4)
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 4.5: Knowledge Check
1. A programmer who writes code that performs calculations combining variables with different numeric data types
without including code to make these types consistent is relying on _____.
2. True or False: Because computers rely on binary numbers, which yield different fractions from decimal numbers
during calculations, the results of expressions using floating-point numbers may be different than expected.
3. True or False: The best way to display the results of calculations without trailing zeros or with a reduced number of
decimal places is to type cast them to integers.
4. To format a number in Python, you use the _____, which is applied to a series of letters inside double quotes with
empty spots represented by pairs of curly braces.
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 4.5: Knowledge Check Answers
1. A programmer who writes code that performs calculations combining variables with different numeric data types
without including code to make these types consistent is relying on _____.
Answer: coercion
2. True or False: Because computers rely on binary numbers, which yield different fractions from decimal numbers
during calculations, the results of expressions using floating-point numbers may be different than expected.
Answer: True
3. True or False: The best way to display the results of calculations without trailing zeros or with a reduced number of
decimal places is to type cast them to integers.
Answer: False
4. To format a number in Python, you use the _____, which is applied to a series of letters inside double quotes with
empty spots represented by pairs of curly braces.
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 4.6: Discussion
1. Imagine you are examining two Python programs: one for building (with ingredient choices)
and purchasing a sub-shop sandwich and another for processing data on planets and
other celestial objects. What integer and floating-point data type variables might be used in
these programs?
2. Compare type casting with using formatting parameters. Name some coding situations
where each should be preferred and explain why.
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Self-Assessment
1. What is your attitude toward writing mathematical expressions for your programs—that is,
do you find it interesting, challenging, enjoyable, a chore, et cetera? Would you like, or are
you planning, to strengthen your skills in this area to improve your school or work
performance?
2. Critique Python's approach to formatting numbers that appear in string output. If you are
familiar with similar functions in another programming language, how do they compare?
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Summary
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.