McMullen ProgwPython 1e Mod03 PowerPoint
McMullen ProgwPython 1e Mod03 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 4)
• 3.1 Literals
• 3.1.1 Define the term “literal” in the context of programming.
• 3.1.2 Identify numeric literals.
• 3.1.3 Provide examples of integer literals and floating-point literals.
• 3.1.4 Identify character and string literals.
• 3.1.5 Provide use cases for string literals that look like numbers.
• 3.1.6 Identify Boolean literals.
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 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.
Module Objectives (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.
Module Objectives (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.
3.1 Literals (1 of 5)
• Numeric literals
• Literal: any element of data specified for a program
• Numeric literal: one or more digits that can represent integers or floating-point numbers
containing a decimal 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.
3.1 Literals (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.
3.1 Literals (3 of 5)
• String literal: value containing one or more characters; typically denoted with double
quotes
• Sample string literals:
"Mordor"
"Three rings for the elven kings..."
"[email protected]"
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.
3.1 Literals (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.
3.1 Literals (5 of 5)
• 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
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
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
3.2 Variables and Constants (1 of 8)
• Variables
• Variable: a named memory location that temporarily holds text or a numeric value
• Variables have three important characteristics:
• A name
• A corresponding memory location where data can be stored
• Changeable (mutable) 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.
3.2 Variables and Constants (2 of 8)
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.
3.2 Variables and Constants (3 of 8)
• Variables (continued)
• General variable naming conventions
• In Python, you cannot use the name of one of the language's built-in functions,
such as print
• Names should be descriptive
• Python variable names must start with a letter or an underscore
• Two most prevalent conventions for combining words for variable names:
• Camel case begins with a lowercase letter, but each subsequent word is capitalized
• Snake case uses all lowercase and separates words with an underscore
• Python typically uses camel case for variable names
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.
3.2 Variables and Constants (4 of 8)
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.
3.2 Variables and Constants (5 of 8)
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.
3.2 Variables and Constants (6 of 8)
• Constants
• Constant: a named memory location that holds data, which is used but not changed by
the statements in a program
• Important characteristics of constants:
• A name
• A corresponding memory location where data can be stored
• Data that is not meant to be changed
• General constant naming conventions:
• Should be descriptive
• Python convention is to name constants in all uppercase text with an underscore
separating words
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.
3.2 Variables and Constants (7 of 8)
• Constants (continued)
• Sample Python constants:
MINUTES_IN_HOUR = 60
MAXIMUM_USERS = 1024
OHIO_ABBREVIATION = "OH"
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.
3.2 Variables and Constants (8 of 8)
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 3.1: Knowledge Check
2. True or False: The statement healthyBreakfast = True sets a variable of the string
type.
3. For a programming language using the camel case convention, a variable to represent
"current balance" would be named _____.
4. A(n) _____ is a named memory location that holds a data value that a program will use but
not alter.
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
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 3.1: Knowledge Check Answers
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.
3.3 Assignment Statements (1 of 3)
• Declaring variables
• Using an undefined variable produces a runtime error, so it is essential to define the
variables used in programs
• To declare a variable means specifying a name that is assigned, or bound, to a memory
location
• Python requires you to assign a value to all variables with an initialization statement
while declaring them
• Initializing variables
• To initialize a variable means specifying a name and a value
• Dynamically typed: describes a programming language (such as Python) that allows a
variable to take on any type of data during program execution
• Type inference: process in which a language deduces the data type based on the
presence or absence of a decimal point and quote marks
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.
3.3 Assignment Statements (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.
Activity 3.2: Breakout Groups:
Assignment Statement Practice
1. Form small groups.
2. Choose a simple task that you might want to write a program to accomplish (e.g., calculate
the sales tax on a purchase).
3. Sketch out an algorithm for this task, identify any variables and constants that the program
would need to use, and write appropriate Python statements to initialize them.
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.
3.3 Assignment Statements (3 of 3)
• Assigning variables
• An assignment statement sets or changes the value that is stored in a variable
• Sample Python assignment statements demonstrating the general syntax:
• Setting a numeric value: dwarf_rings = 7 – 1
• Changing a numeric value: dwarf_rings = dwarf_rings – 1
• Setting a numeric value: total_rings = 3 + 7 + 9 + 1
• Setting a string value: ring_location = "Anduin River"
• Changing a string value: ring_location = "Bilbo's pocket"
• Roles of variables in programs:
• Store literals that your programs use as input, manipulations, and output
• Hold intermediate data during multistep calculations
• Act as repetition counters
• Can be used as the basis for sections of an algorithm that hinge on decisions
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 3.3: Discussion
1. How would you explain the concepts of literals, variables, and constants and the
relationships among them to a curious person who is not a software developer? Draw a
diagram (or describe a diagram you might draw) to clarify your explanation.
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.
3.4 Input and Output (1 of 2)
• Input to a variable
• To collect data from a user, you typically:
• Initialize a variable to hold the user's input
• Supply a prompt that explains what you want the user to enter
• 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
same line
• Output from a variable
• When variables are sent as output to a display or printer, the output is the value of the
variable, not the variable name
• E.g., if dwarf_rings is assigned the value 5, then when the statement print("Is",
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
not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
3.4 Input and Output (2 of 2)
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 3.4: Breakout Groups:
Extending the Trivia Game
1. Form small groups.
2. Plan your own simple trivia game similar to that featured in Section 3.4 of the module.
3. Describe what other steps you would need to add to your program’s algorithm to make it
function as you would expect.
4. Write pseudocode for your trivia game using the pattern shown in Figures 3-10 and 3-12.
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 3.5: Knowledge Check
1. True or False: Runtime errors can be caused by a program's attempt to use an undefined
variable.
2. To _____ a variable means to specify both its name and its value.
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.
4. In a Python program, you can collect input from a user in a variable called
favoriteColor with the statement favoriteColor = _____ ("What is your
favorite color? ").
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 3.5: Knowledge Check Answers
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
favoriteColor with the statement favoriteColor = _____ ("What is your
favorite color? ").
Answer: input
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. Do you expect to find making a distinction between constants and other variables useful as
you write programs? Why or why not?
2. Module 3 presents a basic algorithm for collecting input from a program's user. Briefly
describe a program of interest to you that would require input from the user, and critique
this algorithm based on how you imagine using user-supplied input in that program.
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.