Lesson 2
Variables - User Input
Topics
• Designing a Program
• Input, Processing, and Output
• Displaying Output with print Function
• Comments
• Variables
• Reading Input from the Keyboard
Designing a Program (1 of 3)
• Programs must be designed before they are written
• Program development cycle:
– Design the program
– Write the code
– Correct syntax errors
– Test the program
– Correct logic errors
Designing a Program (2 of 3)
• Design is the most important part of the program
development cycle
• Understand the task that the program is to perform
– Work with customer to get a sense what the program is
supposed to do
– Ask questions about program details
– Create one or more software requirements
Designing a Program (3 of 3)
• Determine the steps that must be taken to perform the
task
– Break down required task into a series of steps
– Create an algorithm, listing logical steps that must be
taken
• Algorithm: set of well-defined logical steps that must
be taken to perform a task
Pseudocode
• Pseudocode: fake code
– Informal language that has no syntax rule
– Not meant to be compiled or executed
– Used to create model program
§ No need to worry about syntax errors, can focus on
program’s design
§ Can be translated directly into actual code in any
programming language
Flowcharts (1 of 2)
• Flowchart: diagram that graphically depicts the steps
in a program
– Ovals are terminal symbols
– Parallelograms are input and output symbols
– Rectangles are processing symbols
– Symbols are connected by arrows that represent the
flow of the program
Flowcharts (2 of 2)
Figure 2-2 The program development cycle
Input, Processing, and Output
• Typically, computer performs three-step process
– Receive input
§ Input: any data that the program receives while it is
running
– Perform some process on the input
§ Example: mathematical calculation
– Produce output
Displaying Output with the print
Function
• Function: piece of prewritten code that performs an
operation
• print function: displays output on the screen
• Argument: data given to a function
– Example: data that is printed to screen
• Statements in a program execute in the order that
they appear
– From top to bottom
Strings and String Literals
• String: sequence of characters that is used as data
• String literal: string that appears in actual code of a
program
– Must be enclosed in single (') or double (") quote marks
– String literal can be enclosed in triple quotes (''' or """)
§ Enclosed string can contain both single and double quotes and
can have multiple lines
Comments
• Comments: notes of explanation within a program
– Ignored by Python interpreter
§ Intended for a person reading the program’s code
– Begin with a # character
• End-line comment: appears at the end of a line of
code
– Typically explains the purpose of that line
Variables
• Variable: name that represents a value stored in the
computer memory
– Used to access and manipulate data stored in memory
– A variable references the value it represents
• Assignment statement: used to create a variable and
make it reference data
– General format is variable = expression
§ Example: age = 29
§ Assignment operator: the equal sign (=)
Variables (cont’d.)
• In assignment statement, variable receiving value
must be on left side
• A variable can be passed as an argument to a
function
– Variable name should not be enclosed in quote marks
• You can only use a variable if a value is assigned to it
Variable Naming Rules
• Rules for naming variables in Python:
– Variable name cannot be a Python key word
– Variable name cannot contain spaces
– First character must be a letter or an underscore
– After first character may use letters, digits, or
underscores
– Variable names are case sensitive
• Variable name should reflect its use
Displaying Multiple Items with the
print Function
• Python allows one to display multiple items with a
single call to print
– Items are separated by commas when passed as
arguments
– Arguments displayed in the order they are passed to
the function
– Items are automatically separated by a space when
displayed on screen
Variable Reassignment
• Variables can reference different values while program
is running
• Garbage collection: removal of values that are no
longer referenced by variables
– Carried out by Python interpreter
• A variable can refer to item of any type
– Variable that has been assigned to one type can be
reassigned to another type
Numeric Data Types, Literals, and the
str Data Type
• Data types: categorize value in memory
– e.g., int for integer, float for real number, str used for
storing strings in memory
• Numeric literal: number written in a program
– No decimal point considered int, otherwise, considered
float
• Some operations behave differently depending on
data type
Reassigning a Variable to a Different
Type
• A variable in Python can refer to items of any type
Figure 2-7 The variable x references an integer
Figure 2-8 The variable x references a string
Reading Input from the Keyboard
• Most programs need to read input from the user
• Built-in input function reads input from keyboard
– Returns the data as a string
– Format: variable = input(prompt)
§ prompt is typically a string instructing user to enter a value
– Does not automatically display a space after the
prompt
Reading Numbers with the input
Function
• input function always returns a string
• Built-in functions convert between data types
– int(item) converts item to an int
– float(item) converts item to a float
– Nested function call: general format:
function1(function2(argument))
§ value returned by function2 is passed to function1
– Type conversion only works if item is valid numeric
value, otherwise, throws exception
Summary
• This lesson covered:
– The program development cycle, tools for program
design, and the design process
– Ways in which programs can receive input, particularly
from the keyboard
– Ways in which programs can present and format output
– Use of comments in programs
– Uses of variables and named constants