0% found this document useful (0 votes)
30 views34 pages

McMullen ProgwPython 1e Mod04 PowerPoint

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views34 pages

McMullen ProgwPython 1e Mod04 PowerPoint

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Programming with Python

Module 4: Numeric Data Types


and Expressions

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)

• 4.1 Primitive Data Types


• 4.1.1 Define the term “data type.”
• 4.1.2 List four common primitive data types.
• 4.1.3 Explain the purpose of primitive data types.
• 4.1.4 Distinguish between primitive data types and composite data types.
• 4.2 Numeric Data Types
• 4.2.1 List the characteristics of integer data.
• 4.2.2 Provide examples of integer data that might be incorporated in a computer
program.
• 4.2.3 Explain the difference between a signed and an unsigned integer.
• 4.2.4 State the memory requirements for signed and unsigned integers.
• 4.2.5 List the characteristics of floating-point 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.
Module Objectives (2 of 3)

• 4.2 Numeric Data Types (continued)


• 4.2.6 Provide examples of floating-point data that might be incorporated in a computer
program.
• 4.2.7 Recall that floating-point data is not stored in conventional binary format.
• 4.2.8 Compare and contrast the use of integer data with floating-point data.
• 4.3 Mathematical Expressions
• 4.3.1 List the symbols used for the following mathematical operations: addition,
subtraction, multiplication, division, exponentiation, and modulo division.
• 4.3.2 Provide examples of program statements in which the result of a mathematical
operation is stored in a variable.
• 4.3.3 Describe the mathematical order of operations.
• 4.3.4 Provide examples illustrating the use of parentheses to change the order of
operations.
• 4.3.5 Explain the use of compound operators such as += -= *= /= and %=.
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 (3 of 3)

• 4.4 Numeric Data Type Conversion


• 4.4.1 Provide examples of when a programmer might convert integer or floating-point
data to a different data type.
• 4.4.2 Explain the disadvantage of converting floating-point data to integer data.
• 4.4.3 Explain the significance of rounding for floating-point calculations.
• 4.5 Formatting Output
• 4.5.1 Explain the advantage of formatting output.
• 4.5.2 List the elements that can be specified when formatting numeric output.
• 4.5.3 Use formatting parameters to specify the output format of numeric 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 (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)

Primitive Data Type Python Code Description Examples


Integer var = 5 Whole number without any 1
decimal places 128
3,056
-2
Floating point var = 5.0 Number that includes decimal 12.99
places 0.26
-3.56
Boolean var = False Logical value of True or False True
False
Valueless var = None No value No real-world equivalent
examples

Figure 4-2 Primitive data types

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)

• Composite data types


• Composite data types are not built into programming language, but can be constructed
from primitive data types
• Programmer defined or available in add-on libraries of program code

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)

• Integer data types


• Whole numbers are classified as integer data types
• Must be whole numbers without decimal places or fractions
• Can be positive or negative
• Unsigned integers don't have a plus or minus symbol and are assumed to be
positive
• Signed integers begin with a plus or minus symbol
• Can be a decimal, binary, or hexadecimal number
• Examples:
• 23 is an unsigned integer
• -40 is a negative signed integer
• 0b00000101 is an unsigned integer (binary 5)
• 0x6C is an unsigned integer (hexadecimal 108)

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)

Figure 4-4 Integer storage allocation

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)

• Floating-point data types


• Numbers with decimal places (i.e., a decimal point and values to the right of it) are
classified as floating-point data types
• Can be positive or negative
• Can be expressed in decimal notation or E notation
• Examples:
• 19.99 is an unsigned floating-point number
• -4.5967E2 is a signed floating-point number in E notation
• Single precision: refers to four-byte floating-point numbers
• Double precision: refers to eight-byte floating-point numbers
• All floating-point numbers in Python are double precision, inferred from the decimal
point in the assignment statement, e.g., price = 5.99

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)

Figure 4-6 Floating-point storage


allocation

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)

Figure 4-8 Checking whether a number


is even or odd

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)

Figure 4-9 Changing 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.
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) _____.

2. True or False: The expression 10 - 8 / 2 * 4 + 5 has the value -1.

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

2. True or False: The expression 10 - 8 / 2 * 4 + 5 has the value -1.

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.

2. To gain experience writing Python code to store various calculations (expressions) in


variables, practice translating parts of some common algebraic formulas into Python.

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)

• Convert integers and floating-point numbers


• Sometimes mathematical expressions contain different data types, such as an integer
and a floating-point number
• Coercion automatically decides which data type to use for a calculation
• Doesn't change the data type of the variable
• Python always uses coercion during normal division with the / symbol
• Type casting is the process of manually converting a value to a different data type
• Python's int() function converts a floating-point value into an integer
• The int() function simply truncates the number—it doesn't round it
• Python's float() function converts numbers to floating point

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)

Figure 4-13 Converting a floating-point


number to an integer

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)

• Formatting parameters (continued)


• Syntax for the Python format() method
• Sample statement:
"Your lucky numbers are {:d} and {:d}.".format(42, 7.3)
• Data inside parentheses in format() method are placed in the empty spots
represented by the pairs of curly braces
• Formatting parameters are inserted inside the curly braces
• Formatting parameters can be used in combination

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)

Parameter Letter or Symbol Example Code Output


Type d for decimal integers "{:d}".format(100) 100
f for floating-point "{:f}".format(15.3) 15.300000
Precision Decimal point followed by the "{:.3}".format(15.3) 15.3
precision; only works for float
numbers
Width A number representing the "{:10}".format(2) _________2
number of characters for the total
width
Left-aligned < "{:<10}".format(2) 2_________
Centered ^ "{:^10}".format(2) ____2_____
Right-aligned > "{:>10}".format(2) _________2

Figure 4-16 Formatting parameters

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)

Figure 4-17 Formatting


numeric output

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.

Answer: format() method

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

Click the link to review the objectives for this presentation.


Link to Objectives

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.

You might also like