CHAPTER 2
Input,
Processing
, and
Output
Copyright © 2018 Pearson Education,
Ltd.
Topics
• Input, Processing, and Output
• Displaying Output with print Function
• Comments
• Variables
Copyright © 2018 Pearson Education, 2
Ltd.
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
Copyright © 2018 Pearson Education, 3
Ltd.
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
Copyright © 2018 Pearson Education, 4
Ltd.
Print function
Code in editor
Output in the console
Copyright © 2018 Pearson Education, 5
Ltd.
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
Copyright © 2018 Pearson Education, 6
Ltd.
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
Copyright © 2018 Pearson Education, 7
Ltd.
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 (=)
Copyright © 2018 Pearson Education, 8
Ltd.
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
Copyright © 2018 Pearson Education, 9
Ltd.
Variable Naming Rules
Copyright © 2018 Pearson Education, 10
Ltd.
variables
2-11
Copyright © 2018 Pearson Education,
Ltd.
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
Copyright © 2018 Pearson Education, 12
Ltd.
Displaying Multiple Items with
the print Function
Code in editor
Output in the console
2-13
Copyright © 2018 Pearson Education,
Ltd.
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
Copyright © 2018 Pearson Education, 14
Ltd.
Variable Reassignment
Code in editor
Output in the console
2-15
Copyright © 2018 Pearson Education,
Ltd.
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
Copyright © 2018 Pearson Education, 16
Ltd.
Numeric Data Types
• Int, or integer, is a whole number, positive or
negative, without decimals, of unlimited length.
• Float, or "floating point number" is a number,
positive or negative, containing one or more
decimals.
• Type Conversion
• You can convert from one type to another with
the int() and float() methods
Copyright © 2018 Pearson Education, 17
Ltd.
Numeric Data Types, Literals,
and the str Data Type
For example:
1.5, 3.1415, and 5.0
2-18
Copyright © 2018 Pearson Education,
Ltd.
Numeric Data Types, Literals,
and the str Data Type
Code in editor
Output in the console
2-19
Copyright © 2018 Pearson Education,
Ltd.
Reassigning a Variable to a
Different Type
• A variable in Python can refer to items of
any type
Copyright © 2018 Pearson Education, 20
Ltd.