0% found this document useful (0 votes)
21 views

McMullen ProgwPython 1e Mod03 PowerPoint

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

McMullen ProgwPython 1e Mod03 PowerPoint

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Programming with Python

Module 3: Literals, Variables,


and Constants

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)

• 3.2 Variables and Constants


• 3.2.1 List the characteristics of a program variable.
• 3.2.2 Create descriptive variable names using appropriate style conventions.
• 3.2.3 Describe the purpose of a constant.
• 3.2.4 Use standard naming conventions for constants.
• 3.2.5 Explain the difference between a variable, a constant, and a literal.
• 3.2.6 Explain the relationship between variables and memory.

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)

• 3.3 Assignment Statements


• 3.3.1 Differentiate between undefined and defined variables.
• 3.3.2 Identify statements that declare variables.
• 3.3.3 Identify statements that initialize variables.
• 3.3.4 Explain how Python uses dynamic typing and determines data types.
• 3.3.5 Explain the meaning of type inference.
• 3.3.6 Explain the concept of a null variable.
• 3.3.7 Identify assignment statements that change the value of a variable.
• 3.3.8 Differentiate declaring, initializing, and assigning variables.

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)

• 3.4 Input and Output


• 3.4.1 State the algorithm for collecting input from a user and placing it in a variable.
• 3.4.2 Transform the user input algorithm into pseudocode.
• 3.4.3 Trace the pseudocode that outputs the value of a variable that is input by a user.

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

• Sample numeric literals:


dwarf_rings = 7
1
ring_thickness = 2.7

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)

Figure 3-2 Common numeric 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.
3.1 Literals (3 of 5)

• Character and string literals


• Character literal: a single letter or symbol
• Sample character literals:
'a'
'@'

• 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)

Figure 3-3 Character literals and string


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.
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)

Figure 3-4 A variable is a named


memory location that temporarily holds
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 (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)

Snake case Camel case


elven_rings elvenRings
made_in_mordor madeInMordor
hobbit hobbit
multiples_of_2 multiplesOf2

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)

Figure 3-6 A constant is a named


memory location that holds data that
does not change

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"

• 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
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)

Figure 3-7 Variable names correspond


to memory locations

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

1. One example of a(n) _____ is "Thomas".

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

1. One example of a(n) _____ is "Thomas".


Answer: string literal
2. True or False: The statement healthyBreakfast = True sets a variable of the string
type.
Answer: False
3. For a programming language using the camel case convention, a variable to represent
"current balance" would be named _____.
Answer: currentBalance
4. A(n) _____ is a named memory location that holds a data value that a program will use but
not alter.
Answer: constant
5. Three key characteristics of _____ are that they have names, have corresponding memory
locations, and contain changeable data.
Answer: variables

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)

• Initializing variables (continued)


• In a dynamically typed language, a variable can repeatedly be reassigned to a value of a
different data type (e.g., changed from an integer to a string)
• Statically typed: describes a programming language that requires you to store only one
type of data in a variable
• Roles of initialization in programming
• Specifying starting values when known
• Specifying the initial value of a repetition counter (typically 0 or 1)
• Creating a null variable (one that has no value), which is assigned to the keyword
None 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.
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.

2. Write Python assignment statements for the following variables/constants, using


appropriate naming conventions: deposit amount of $330, fixed interest rate of 5%,
acceleration due to gravity of 9.8 meters per second per second, and mass of an object of
12 kilograms.

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)

Figure 3-11 Loading a variable with


data entered by a user

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

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