0% found this document useful (0 votes)
9 views32 pages

CT1111 Slides 442 4

Chapter 2 covers the essential concepts of input, processing, and output in programming, specifically using Python. It explains how to read user input from the keyboard, perform calculations with various data types, and format output effectively. Key topics include the use of built-in functions for type conversion, operator precedence, and customizing output with the print function.

Uploaded by

Amjed Mohammed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views32 pages

CT1111 Slides 442 4

Chapter 2 covers the essential concepts of input, processing, and output in programming, specifically using Python. It explains how to read user input from the keyboard, perform calculations with various data types, and format output effectively. Key topics include the use of built-in functions for type conversion, operator precedence, and customizing output with the print function.

Uploaded by

Amjed Mohammed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

CHAPTER 2

Input,
Processing
, and
Output

Copyright © 2018 Pearson Education,


Ltd.
Topics
• Reading Input from the Keyboard
• Performing Calculations
• More About Data Output

Copyright © 2018 Pearson Education, 2


Ltd.
2.6 Reading Input from the
Keyboard

Concept:
Programs commonly need to read
input typed by the user on the
keyboard. We will use the Python
functions to do this.

2-3
Copyright © 2018 Pearson Education,
Ltd.
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 © 2018 Pearson Education, 4


Ltd.
Reading Input from the
Code in editor
Keyboard

Output in the console

Copyright © 2018 Pearson Education, 5


Ltd.
Reading Numbers with the
input Function
• input function always returns a string
Code in editor

Output in the console

Copyright © 2018 Pearson Education, 6


Ltd.
Reading Numbers with the
input Function
• Built-in functions convert between data types
• int(item) converts item to an int
• float(item) converts item to a float
Code in editor

Output in the console

Copyright © 2018 Pearson Education, 7


Ltd.
Reading Numbers with the
input Function
• We Can do it in one step
• Nested function call: general format:
function1(function2(argument))
• value returned by function2 is passed to function1

Copyright © 2018 Pearson Education, 8


Ltd.
Reading Numbers with the
input Function
• Type conversion only works if item is valid numeric
value, otherwise, throws exception
Output in the console

Output in the console

Copyright © 2018 Pearson Education, 9


Ltd.
Copyright © 2018 Pearson Education, 10
Ltd.
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

operator
Resulting value

z= x + y
Operands

Copyright © 2018 Pearson Education, 11


Ltd.
2.7 Performing Calculations
Table 2-2 Python math operators

2-12
Copyright © 2018 Pearson Education,
Ltd.
2.7 Performing Calculations

Code in editor Output in the console

2-13
Copyright © 2018 Pearson Education,
Ltd.
2.7 Performing Calculations

Exercise:
Input : ?
Output: ?
Processing : ?

2-14
Copyright © 2018 Pearson Education,
Ltd.
Copyright © 2018 Pearson Education, 15
Ltd.
The Exponent Operator and
the Remainder Operator
• Exponent operator (**): Raises a
number to a power
• x ** y = xy

Copyright © 2018 Pearson Education, 16


Ltd.
The Exponent Operator and
the Remainder Operator
• Remainder operator (%): Performs
division and returns the remainder
• a.k.a. modulus operator

• Typically used to convert times and distances,


and to detect odd or even numbers

Copyright © 2018 Pearson Education, 17


Ltd.
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 © 2018 Pearson Education, 18


Ltd.
2.7 Performing Calculations
Table 2-3 Some expressions

2-19
Copyright © 2018 Pearson Education,
Ltd.
Converting Math Formulas to
Programming Statements
• Operator required for any mathematical
operation
• When converting mathematical
expression to programming statement:
• May need to add multiplication operators
• May need to insert parentheses

Copyright © 2018 Pearson Education, 20


Ltd.
Mixed-Type Expressions and
Data Type Conversion
• Data type resulting from math operation depends on data types
of operands
• Two int values: result is an int
• Two float values: result is a float
• int and float: int temporarily converted to float, result of
the operation is a float
• Mixed-type expression
• Type conversion of float to int causes truncation of fractional
part

Copyright © 2018 Pearson Education, 21


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

result = var1 * 2 + var2 * 3 + \


var3 * 4 + var4 * 5

Copyright © 2018 Pearson Education, 22


Ltd.
Breaking Long Statements
into Multiple Lines
• Any part of a statement that is enclosed in
parentheses can be broken without the line
continuation character.

print("Monday's sales are", monday,


"and Tuesday's sales are", tuesday,
"and Wednesday's sales are", Wednesday)

total = (value1 + value2 +


value3 + value4 +
value5 + value6)

Copyright © 2018 Pearson Education, 23


Ltd.
More About Data Output
• print function displays line of output
• Newline character at end of printed data
• Special argument end='delimiter' causes print
to place delimiter at end of data instead of newline
character
• print function uses space as item separator
• Special argument sep='delimiter' causes print
to use delimiter as item separator

Copyright © 2018 Pearson Education, 24


Ltd.
More About Data Output

•If you do not want the print function to start a new line of output when it
finishes displaying its output, you can pass the special argument end=' '
to the function

print('One', end=' ') print('One', end='')


print('Two', end=' ') print('Two', end='')
print('Three') print('Three')

Output: :Output

One Two Three OneTwoThree

Copyright © 2018 Pearson Education,


Ltd. 2-25
More About Data Output
Specifying an Item Separator
•When multiple arguments are passed to the print function, they are
automatically separated by a space when they are displayed on the
screen.
•If you do not want a space printed between the items, you can pass the
argument sep='‘
•You can also use this special argument to specify a character other than
the space to separate multiple items sep='*‘ , sep=‘~~'
Example:
print('One', 'Two', 'Three') >>> print('One', 'Two', 'Three', sep='')

Output: :Output

One Two Three OneTwoThree

Copyright © 2018 Pearson Education,


Ltd. 2-26
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 © 2018 Pearson Education, 27


Ltd.
More About Data Output
Formatting Numbers

2-28
Copyright © 2018 Pearson Education,
Ltd.
More About Data Output

2-29
Copyright © 2018 Pearson Education,
Ltd.
2.8 More About Data Output
Formatting Numbers

2-30
Copyright © 2018 Pearson Education,
Ltd.
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 © 2018 Pearson Education, 31


Ltd.
More About Data Output

2-32
Copyright © 2018 Pearson Education,
Ltd.

You might also like