Data Types, Numbers and Characters
Data Types, Numbers and Characters
Module Objectives (3 of 4)
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1
5/22/2024
• Tricky literals
• Literals containing number characters can be either numbers or strings
• When working with numerals in a context where they are not manipulated
mathematically, you can enclose them in double or single quotes to designate them
as strings
• Example use cases: area codes, PINs, Social Security numbers
• Boolean literal: a literal with a value of either True or False that is not a string, and
Figure 3-2 Common numeric literals thus not enclosed in quote marks
• Example use case: setting a fact as true
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Figure 3-3 Character literals and string Figure 3-4 A variable is a named
literals memory location that temporarily holds
data
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
2
5/22/2024
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
• Constants (continued)
Snake case Camel case • Sample Python constants:
MINUTES_IN_HOUR = 60
elven_rings elvenRings MAXIMUM_USERS = 1024
made_in_mordor madeInMordor OHIO_ABBREVIATION = "OH"
hobbit hobbit
multiples_of_2 multiplesOf2
• Using constants for formulas facilitates easier code changes/maintenance
• The memory connection
• Both variables and constants correspond to memory locations where data can be stored
• These memory locations have unique addresses, expressed in binary or hex numbers
• When you specify the name for a variable or constant, your programming language ties it
to a memory location (the address) where you can put data
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
3
5/22/2024
5. Three key characteristics of _____ are that they have names, have corresponding memory
locations, and contain changeable data.
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
4
5/22/2024
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
• Input to a variable 1. True or False: Runtime errors can be caused by a program's attempt to use an undefined
• To collect data from a user, you typically: variable.
• Initialize a variable to hold the user's input
• Supply a prompt that explains what you want the user to enter 2. To _____ a variable means to specify both its name and its value.
• Collect the data entered by the user in the initialized variable
• Python allows you to include an initialization, a display, and an input instruction on the 3. Because Python is a _____ language, you can initialize a variable with a value of one data
same line type, then use an assignment statement to store a value of a different data type in that
same variable.
• Output from a variable
• When variables are sent as output to a display or printer, the output is the value of the
4. In a Python program, you can collect input from a user in a variable called
variable, not the variable name
favoriteColor with the statement favoriteColor = _____ ("What is your
• E.g., if dwarf_rings is assigned the value 5, then when the statement print("Is", favorite color? ").
dwarf_rings, "your final answer?") executes, the text displayed is: Is 5
your final answer?
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1. True or False: Runtime errors can be caused by a program's attempt to use an undefined
variable.
Answer: True
2. To _____ a variable means to specify both its name and its value.
Answer: initialize
3. Because Python is a _____ language, you can initialize a variable with a value of one data
type, then use an assignment statement to store a value of a different data type in that
same variable.
Answer: dynamically typed
4. In a Python program, you can collect input from a user in a variable called
Figure 3-11 Loading a variable with
favoriteColor with the statement favoriteColor = _____ ("What is your
data entered by a user
favorite color? ").
Answer: input
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5
5/22/2024
1. Do you expect to find making a distinction between constants and other variables useful as • 4.1 Primitive Data Types
you write programs? Why or why not? • 4.1.1 Define the term “data type.”
• 4.1.2 List four common primitive data types.
2. Module 3 presents a basic algorithm for collecting input from a program's user. Briefly • 4.1.3 Explain the purpose of primitive data types.
describe a program of interest to you that would require input from the user, and critique
• 4.1.4 Distinguish between primitive data types and composite data types.
this algorithm based on how you imagine using user-supplied input in that program.
• 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 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Click the link to review the objectives for this presentation. • 4.2 Numeric Data Types (continued)
Link to Objectives • 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 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Module Objectives (3 of 3)
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
6
5/22/2024
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
7
5/22/2024
• Floating-point data types 1. Python knows how to allocate memory for and manipulate _____ data types.
• Numbers with decimal places (i.e., a decimal point and values to the right of it) are Answer: primitive
classified as floating-point data types
• Can be positive or negative 2. True or False: Add-on libraries of program code are often sources of useful primitive data types.
• Can be expressed in decimal notation or E notation
Answer: False
• Examples:
• 19.99 is an unsigned floating-point number 3. Floating-point numbers are stored in either 4 or 8 bytes of memory in _____, which resembles
• -4.5967E2 is a signed floating-point number in E notation scientific notation.
• Single precision: refers to four-byte floating-point numbers Answer: E notation
• Double precision: refers to eight-byte floating-point numbers
• All floating-point numbers in Python are double precision, inferred from the decimal 4. True or False: The most appropriate data type for variables that hold values representing
point in the assignment statement, e.g., price = 5.99 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 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
• 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
Figure 4-6 Floating-point storage
• Modulo division (with %) produces the remainder of a division operation
allocation
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
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 Figure 4-8 Checking whether a number
scientific notation. is even or odd
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 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
8
5/22/2024
• Order of operations 1. A programming statement with a value that includes one or more arithmetic operators and
• The order of operations specifies the sequence in which to perform arithmetic one or more operands is a(n) _____.
operations
Answer: expression
• Do the math in parentheses first
• Carry out exponentiation and roots
2. True or False: The expression 10 - 8 / 2 * 4 + 5 has the value -1.
• Perform multiplication, floating-point division, integer division, and modulo division
from left to right
Answer: True
• 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 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
• 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
Figure 4-9 Changing the order of • counter /= 2 # Result is 5
operations • counter %= 2 # Result is 0
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
9
5/22/2024
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
• Rounding quirks
Parameter Letter or Symbol Example Code Output
• Programming languages have some quirks when it comes to numeric data
Type d for decimal integers "{:d}".format(100) 100
• E.g., dividing 1 // 2 using Python's integer division operator results in the value 0 f for floating-point "{:f}".format(15.3) 15.300000
• Odd results may stem from a rounding quirk related to the computer’s reliance on binary
Precision Decimal point followed by the "{:.3}".format(15.3) 15.3
numbers
precision; only works for float
• Binary representation of a floating-point number is an approximation because binary numbers
numbers don’t yield the same fractions as decimals
Width A number representing the "{:10}".format(2) _________2
• Math functions can be used to truncate or round numbers number of characters for the total
width
Left-aligned < "{:<10}".format(2) 2_________
Centered ^ "{:^10}".format(2) ____2_____
Right-aligned > "{:>10}".format(2) _________2
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
10
5/22/2024
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.
Figure 4-17 Formatting
numeric output
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1. A programmer who writes code that performs calculations combining variables with different numeric data 1. What is your attitude toward writing mathematical expressions for your programs—that is,
types without including code to make these types consistent is relying on _____.
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. 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.
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?
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 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1. A programmer who writes code that performs calculations combining variables with different numeric data Click the link to review the objectives for this presentation.
types without including code to make these types consistent is relying on _____.
Answer: coercion Link to Objectives
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 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
11
5/22/2024
Module Objectives (3 of 3)
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May
May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
12
5/22/2024
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
13
5/22/2024
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
• String manipulation
• Common ways to manipulate strings:
• Find the length of a string
• Change the case of a string
• Check if a string contains a specific character
• Retrieve a substring from a longer string
• String length
• Strength length is useful for reversing strings, ensuring they are not too long, et cetera
• Python's len() function returns a string's length as an integer
Figure 5-13 Each character in a string
is stored in a memory location
referenced by an index
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
2. True or False: If the variable my_three is assigned the value of "3", the method
my_three.isdigit() returns the Boolean True.
3. To include a double quotation mark, backslash, or blank line within a string, you use a(n) _____.
4. Within the string "apple", the character e is located at index _____. Figure 5-15 Finding the length of a
string
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
14
5/22/2024
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
15
5/22/2024
• Coercion and typecasting 1. The Python _____ returns the number of characters in a string stored within a variable that is
passed in to it as a parameter.
• With most programming languages, strings can be concatenated with other strings and
character data, but not with integer or floating-point data
• To fix an integer-string type mismatch, use typecasting to convert the integer to a string
2. The two values you supply when writing a Python statement to retrieve a substring are the
or vice-versa starting index and _____ of the substring.
• You can use the str() and int() functions to perform these typecasting operations
3. To combine the strings in two or more variables together to produce a new string that is either
output or stored in a variable, you use the process of _____.
4. In Python, when the letters variable has the string value of "26", you can use the statement
number = _____(letters) to assign the numeric value of 26 to number.
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1. The Python _____ returns the number of characters in a string stored within a variable that is
passed in to it as a parameter.
Answer: len() function
2. The two values you supply when writing a Python statement to retrieve a substring are the
starting index and _____ of the substring.
Answer: ending index
Figure 5-19 3. To combine the strings in two or more variables together to produce a new string that is either
Typecasting: output or stored in a variable, you use the process of _____.
Integer to string
Answer: concatenation
4. In Python, when the letters variable has the string value of "26", you can use the statement
number = _____(letters) to assign the numeric value of 26 to number.
Answer: int
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
McMullen/Matthews/Parsons, Programming with Python, 1st Edition. © 2023 Cengage. All Rights Reserved. May 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. not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
16
5/22/2024
1. Imagine you would like to write a program that mixes up either the words in a sentence or
the characters in a single word and returns the mixed-up version. Which string operations
and manipulation techniques described in Module 5 could you use to accomplish this task?
2. Write an algorithm for the program option you chose for the previous question in either
narrative or pseudocode form.
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. How are strings used in some of the computer applications you use regularly? Can you
identify use cases for the string manipulation methods and functions you learned in this
module within that context?
2. Some programming languages have a separate data type just for single characters (strings
of length 1). As a software developer, do you think you would prefer using two separate
data types for characters and strings, or simply using string data as you do in Python?
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.
17