Lecture-2-Input Process and Output
Lecture-2-Input Process and Output
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
11
Strings and String Literals
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
17
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 18
Different Type
A variable in Python can refer to items of
any type
20
Reading Input from the Keyboard
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
22
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
Operator Precedence and Grouping 23
with Parentheses
Python operator precedence:
Operations enclosed in parentheses
Forces operations to be performed before others
Exponentiation (**)
Multiplication (*), division (/ and //), and
remainder (%)
Addition (+) and subtraction (-)
Higher precedence performed first
Same precedence operators execute from left to right
The Exponent Operator and the 24
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
Converting Math Formulas to 25
Programming Statements
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
Breaking Long Statements into 27
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)
28
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
29
More About Data Output (cont’d.)