0% found this document useful (0 votes)
16 views6 pages

12 Lecture Note Unit 1 Output Function Pcps 2023

Uploaded by

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

12 Lecture Note Unit 1 Output Function Pcps 2023

Uploaded by

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

Department of Computer Science and Engineering

PES University, Bangalore, India

Lecture Notes
Python for Computational Problem Solving
UE23CS151A

Lecture #12
Output function

By,
Prof. Sindhu R Pai,
Anchor, PCPS - 2023
Assistant Professor
Dept. of CSE, PESU

Verified by,
PCPS Team - 2023

Many Thanks to
Dr. Shylaja S S (Director, CCBD and CDSAML Research Centers, Former
Chairperson, CSE, PES University)
Prof. Chitra G M (Asst. Prof, Dept. of CSE, PCPS Anchor – 2022)
Aug – Dec, 2023

Output Function – print:


Consider the below executions.
Interpreter mode

Observation: Runs one statement at a time. Here, we need not tell the interpreter what to
be displayed because of the statement execution. When you press the enter key, the result
is displayed without any extra instruction like print. It does the same job when you use the
print function in interpreter mode.

Batch mode

Observation: No output from the first four lines of code. Output of next three lines are there
in the output window. Here, we need to explicitly tell the interpreter what should be
displayed as a result of the program. Python has a specialized function that does just that,
called the print function.

Note: In Batch mode, we must use the function print for displaying information to the
output screen

Department of CSE, PESU 2


Aug – Dec, 2023

Function: A group of commands that performs a specified task. It may take some input, do
some processing, and returns a result(output). In programming, input given to a function
are called arguments. For more details, refer to Lecture #51

Print in detail :
Print is a function that takes input and as part of the processing, displays the value of
the argument to the output screen. We will be calling this print function using a call operator
(). Let us get the help page of print to understand it better.

Characteristics of print function:


• Can display/print anything in the world.
• Can take any type of arguments.
Values could be Numbers, Boolean, strings, collections, expressions, functions,
etc.
• Can take any number of arguments.
• Has the capability to evaluate the expression.
• Formatting is possible to some extent using the keyword separators – sep & end

Department of CSE, PESU 3


Aug – Dec, 2023

Few Examples:
Example 1: Can take any type of arguments

Example 2: Can take any type of arguments of any type

Example 3: Can be used to evaluate the expression

Department of CSE, PESU 4


Aug – Dec, 2023

Example 4: Batch Mode executions

In all above examples, we see that if there is more than one argument to print, they are
separated by a space and between two print calls, there is a new line in the output. This is
because of the keyword parameters sep and end respectively. The default value for sep is
space (‘ ‘) and the default value for end is ‘\n’ (newline).

Example 5:

Few points to note in Example 5:


The sep and end field behavior is only applicable for the print function in which
it is specified. The subsequent print function calls will use its default value as per the
help page of print.

Now think about printing only 2 digits after the decimal point in each expression like
5/7. Possible using the format function in python. The built-in format function can
be used to produce a numeric value containing a specific number of decimal places.
More about this will be discussed in Lecture #17.

Department of CSE, PESU 5


Aug – Dec, 2023

Example 6:

- END -

Department of CSE, PESU 6

You might also like