10
Most read
11
Most read
20
Most read
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
C H A P T E R 2
Input,
Processing,
and Output
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Topics
Displaying Output with print Function
Comments
Variables
Reading Input from the Keyboard
Performing Calculations
More About Data Output
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
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 © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
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 © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
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 © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
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 © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
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
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
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 © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
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 © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
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
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Reassigning a Variable to a
Different Type
A variable in Python can refer to items of
any type
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
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
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Performing Calculations
Math expression: performs calculation
and gives a value
Math operator: tool for performing calculation
Operands: values surrounding operator
Variables can be used as operands
Resulting value typically assigned to variable
Two types of division:
/ operator performs floating point division
// operator performs integer division
Positive results truncated, negative rounded away
from zero
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Operator Precedence and
Grouping with Parentheses
Python operator precedence:
1. Operations enclosed in parentheses
Forces operations to be performed before others
2. Exponentiation (**)
3. Multiplication (*), division (/ and //), and
remainder (%)
4. Addition (+) and subtraction (-)
Higher precedence performed first
Same precedence operators execute from left
to right
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
The Exponent Operator and
the Remainder Operator
Exponent operator (**): Raises a
number to a power
x ** y = xy
Remainder operator (%): Performs
division and returns the remainder
a.k.a. modulus operator
e.g., 4%2=0, 5%2=1
Typically used to convert times and distances,
and to detect odd or even numbers
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Breaking Long Statements
into Multiple Lines
Long statements cannot be viewed on
screen without scrolling and cannot be
printed without cutting off
Multiline continuation character ():
Allows to break a statement into
multiple lines
Example:
print(‘my first name is’,
first_name)
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
More About Data Output
(cont’d.)
Special characters appearing in string
literal
Preceded by backslash ()
Examples: newline (n), horizontal tab (t)
Treated as commands embedded in string
When + operator used on two strings in
performs string concatenation
Useful for breaking up a long string literal
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Formatting Numbers
Can format display of numbers on
screen using built-in format function
Two arguments:
Numeric value to be formatted
Format specifier
Returns string containing formatted number.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Formatting Numbers (cont’d.)
The % symbol can be used in the format
string of format function to format
number as percentage
To format an integer using format
function:
Use d as the type designator
Do not specify precision
Can still use format function to set field
width or comma separator
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Summary
This chapter covered:
The program de velopment 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
Tools for performing calculations in programs

More Related Content

PDF
Network programming Using Python
PPTX
6-Python-Recursion PPT.pptx
ODP
Python unit testing
PPTX
Constructor and destructor
DOCX
Mã hóa đường cong Elliptic
PDF
c++ lab manual
PPT
Formatted input and output
PPTX
Machine learning ( Part 3 )
Network programming Using Python
6-Python-Recursion PPT.pptx
Python unit testing
Constructor and destructor
Mã hóa đường cong Elliptic
c++ lab manual
Formatted input and output
Machine learning ( Part 3 )

What's hot (20)

PDF
Phương pháp xấp xỉ điểm bất động của ánh xạ không giãn, HAY
PPT
Comments in C Programming
PDF
Lập trình Python cơ bản
PDF
Python tutorial
PPT
03 lap trinh hop ngu voi 8086
PDF
Introduction to Python IDLE | IDLE Tutorial | Edureka
PPT
Bài giảng ACCESS - VBA
PPTX
Chapter 03 python libraries
PPT
C++ classes tutorials
PPTX
Java - Generic programming
PPTX
Python in 30 minutes!
PDF
Constructors and Destructors
PDF
thuc hanh xu ly tin hieu so
PPTX
Python for loop
PPT
Thuật toán mã hóa rsa
PPSX
Modules and packages in python
PDF
Mot so ham do hoa trong c c++
PDF
1000 Bài tập của Thầy Nguyễn Tấn Trần Minh Khang.pdf
PPT
c-programming
Phương pháp xấp xỉ điểm bất động của ánh xạ không giãn, HAY
Comments in C Programming
Lập trình Python cơ bản
Python tutorial
03 lap trinh hop ngu voi 8086
Introduction to Python IDLE | IDLE Tutorial | Edureka
Bài giảng ACCESS - VBA
Chapter 03 python libraries
C++ classes tutorials
Java - Generic programming
Python in 30 minutes!
Constructors and Destructors
thuc hanh xu ly tin hieu so
Python for loop
Thuật toán mã hóa rsa
Modules and packages in python
Mot so ham do hoa trong c c++
1000 Bài tập của Thầy Nguyễn Tấn Trần Minh Khang.pdf
c-programming
Ad

Similar to Gaddis Python 3e Chapter 02 PPT (1).ppt (20)

PPTX
python notes for MASTER OF COMPUTER APPLIICATION_ppt.pptx
PDF
Python Data Types
PPT
pengenalan mengenai computer dan programming
PPT
C intro
PDF
3-Python Python oho pytho hdiwefjhdsjhds
PPT
Pengaturcaraan asas
PDF
PPTX
Python.pptx
PPTX
Variables and calculations_chpt_4
PPTX
OOP Using Java Ch2 all about oop .pptx
PPTX
C Programming Unit-1
PPTX
C programming Training in Ambala ! Batra Computer Centre
PDF
Advance Python Programming until operators.pdf
PPTX
Compiler Construction.pptx
PDF
PART - 1 Python Introduction- Variables- Data types - Numeric- String- Boole...
PDF
Unit 2 introduction to c programming
PPT
Unit 4 Foc
PPT
Introduction to C Programming
PDF
C Programming Language Introduction and C Tokens.pdf
python notes for MASTER OF COMPUTER APPLIICATION_ppt.pptx
Python Data Types
pengenalan mengenai computer dan programming
C intro
3-Python Python oho pytho hdiwefjhdsjhds
Pengaturcaraan asas
Python.pptx
Variables and calculations_chpt_4
OOP Using Java Ch2 all about oop .pptx
C Programming Unit-1
C programming Training in Ambala ! Batra Computer Centre
Advance Python Programming until operators.pdf
Compiler Construction.pptx
PART - 1 Python Introduction- Variables- Data types - Numeric- String- Boole...
Unit 2 introduction to c programming
Unit 4 Foc
Introduction to C Programming
C Programming Language Introduction and C Tokens.pdf
Ad

Recently uploaded (20)

PPTX
O2C Customer Invoices to Receipt V15A.pptx
PDF
NewMind AI Weekly Chronicles – August ’25 Week III
PDF
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
DOCX
search engine optimization ppt fir known well about this
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PPTX
observCloud-Native Containerability and monitoring.pptx
PDF
Zenith AI: Advanced Artificial Intelligence
PDF
DP Operators-handbook-extract for the Mautical Institute
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
PDF
Enhancing emotion recognition model for a student engagement use case through...
PDF
August Patch Tuesday
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
PDF
Taming the Chaos: How to Turn Unstructured Data into Decisions
PPT
What is a Computer? Input Devices /output devices
PDF
Getting Started with Data Integration: FME Form 101
O2C Customer Invoices to Receipt V15A.pptx
NewMind AI Weekly Chronicles – August ’25 Week III
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
search engine optimization ppt fir known well about this
A contest of sentiment analysis: k-nearest neighbor versus neural network
observCloud-Native Containerability and monitoring.pptx
Zenith AI: Advanced Artificial Intelligence
DP Operators-handbook-extract for the Mautical Institute
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
Enhancing emotion recognition model for a student engagement use case through...
August Patch Tuesday
Group 1 Presentation -Planning and Decision Making .pptx
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
Taming the Chaos: How to Turn Unstructured Data into Decisions
What is a Computer? Input Devices /output devices
Getting Started with Data Integration: FME Form 101

Gaddis Python 3e Chapter 02 PPT (1).ppt

  • 1. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output
  • 2. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Topics Displaying Output with print Function Comments Variables Reading Input from the Keyboard Performing Calculations More About Data Output
  • 3. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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
  • 4. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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
  • 5. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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
  • 6. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 (=)
  • 7. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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
  • 8. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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
  • 9. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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
  • 10. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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
  • 11. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Reassigning a Variable to a Different Type A variable in Python can refer to items of any type
  • 12. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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
  • 13. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Performing Calculations Math expression: performs calculation and gives a value Math operator: tool for performing calculation Operands: values surrounding operator Variables can be used as operands Resulting value typically assigned to variable Two types of division: / operator performs floating point division // operator performs integer division Positive results truncated, negative rounded away from zero
  • 14. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Operator Precedence and Grouping with Parentheses Python operator precedence: 1. Operations enclosed in parentheses Forces operations to be performed before others 2. Exponentiation (**) 3. Multiplication (*), division (/ and //), and remainder (%) 4. Addition (+) and subtraction (-) Higher precedence performed first Same precedence operators execute from left to right
  • 15. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Exponent Operator and the Remainder Operator Exponent operator (**): Raises a number to a power x ** y = xy Remainder operator (%): Performs division and returns the remainder a.k.a. modulus operator e.g., 4%2=0, 5%2=1 Typically used to convert times and distances, and to detect odd or even numbers
  • 16. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Breaking Long Statements into Multiple Lines Long statements cannot be viewed on screen without scrolling and cannot be printed without cutting off Multiline continuation character (): Allows to break a statement into multiple lines Example: print(‘my first name is’, first_name)
  • 17. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More About Data Output (cont’d.) Special characters appearing in string literal Preceded by backslash () Examples: newline (n), horizontal tab (t) Treated as commands embedded in string When + operator used on two strings in performs string concatenation Useful for breaking up a long string literal
  • 18. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Formatting Numbers Can format display of numbers on screen using built-in format function Two arguments: Numeric value to be formatted Format specifier Returns string containing formatted number.
  • 19. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Formatting Numbers (cont’d.) The % symbol can be used in the format string of format function to format number as percentage To format an integer using format function: Use d as the type designator Do not specify precision Can still use format function to set field width or comma separator
  • 20. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Summary This chapter covered: The program de velopment 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 Tools for performing calculations in programs