0% found this document useful (0 votes)
1 views

11 IO Operations

Uploaded by

Darshanjr Dachhu
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

11 IO Operations

Uploaded by

Darshanjr Dachhu
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Lecture 11: IO Operations

18ESC108A Elements of Computer Science and Engineering


B. Tech. 2018

Course Leader(s):
Prabhakar A.
Roopa G.
Jishmi Jos Choondal
Pujitha V.
Department of Computer Science and Engineering
Faculty of Engineering and Technology
Ramaiah University of Applied Sciences

1
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Objectives

• At the end of this lecture, student will be able to


– Explain library functions and format used to perform input/output operations
– Use strings for the terminal input and output of text
– Construct a simple Python program that performs inputs, calculations, and outputs

2
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Topics

• Input operations
• Output operations
• Output formatting

3
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Data to The Computer
• Most of the programs conform to the input-process-output model: data comes in,
gets manipulated, and then is stored, displayed, printed, or transferred

4
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Input From The User
• Python has the input() built in function to display a prompt on screen, and then
accept keyboard input, returning what was entered as a string to the code

>>> res = input('What is your favorite programming language: ')


What is your favorite programming language: Python
>>> res
Python

res = input('What is your favorite programming language: ')


print(res)
What is your favorite programming language: Python
Python
5
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Reading a Numeric Value
• When reading a numeric value from the user
1. programmer must use the input() function to get the string of characters
2. use the int or float syntax to construct the numeric value that character string
represents

>>>year = int(input(“What is your age?” ))

6
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Splitting The Input
• If the user enters multiple pieces of information on the same line, it is common to
call the split method on the result

reply = input(" Enter x and y, separated by spaces:" )


pieces = reply.split( ) # returns a list of strings, as separated by spaces
x = int(pieces[0])
y = int(pieces[1])
print(x,y)

The output is
Enter x and y, separated by spaces:10 20
10 20
7
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Splitting The Input contd.
result = input(" Enter two numbers, separated by spaces:" )
x,y = result.split()
print(y)
Enter x and y, separated by spaces:10 20
20

>>> a,b='h i'.split(' ')


>>> a
'h'
>>> b
'i'

8
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Display on the Screen
• The built-in function, print, is used to generate standard output to the console

• In its simplest form, it prints an arbitrary sequence of arguments, separated by


spaces, and followed by a trailing newline character
print(“hello”) #outputs the string “hello”

• Arguments need not be string instances


print(1.5 * 3)
4.5

• Without any arguments, the command print( ) outputs a single newline character

9
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Printing Multiple Items
• To print multiple items, pass all the items in sequence separated by commas
• They are automatically separated by commas

print(“hello”,”world”)
hello world

print(“2+3=”,2+3)
2+3= 5 # there is a space after the = operator

10
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Printing Multiple Items contd.
a=20
b=10
c=25
print(c,"is greater than",a,"and",b)
25 is greater than 20 and 10

11
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Separating Items
• To print multiple items but do not want space to be the separator, pass the
separator string as an argument to print()

print("hello","world",sep=',')
hello,world
print("hello","world",sep='<->')
hello<->world

print("welcome","to","python",sep='-')
welcome-to-python

print("2+3=",2+3,sep="")
2+3=5 # there is no space after the = operator
12
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Terminating Items
• By default, print() function prints a newline character ( \n ) after printing all the
items
• To control the termination of items, use the keyword end
print("Is Python a dynamic language?",end="\nPython")
Is Python a dynamic language?
Python

print("Is Python a dynamic language?\n",end="Python")


Is Python a dynamic language?
Python

print("Is Python a dynamic language?\t",end="Python")


Is Python a dynamic language? Python 13
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Quotation Marks

• Strings in Python are enclosed in quotation marks


• Python does not differentiate between single quotes and double quotes

Single quotes
• Any single quote character inside the string needs to be escaped by prefixing it with
backslash
• Example
print('Hello, World!')
print(‘ ”Hi” from \’India\’ ')
• The output is
Hello, World!
“Hi” from ‘India’
14
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Quotation Marks contd.

Double quotes
• Any double quote character inside the string needs to be escaped by prefixing it
with backslash

• Example
print(“Hello, World!”)
print(“ ‘Hi’ from \”India\” “)

• The output is
Hello, World!
‘Hi’ from “India”

15
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Formatting Strings Using format()

• The format() function searches the string for placeholders


• These placeholders are indicated by braces ( { } ) and indicate that some value needs
to be substituted there

• The format() takes any number of parameters


• It is divided into two types of parameters:
1. Positional parameters - list of parameters that can be accessed with index of parameter
inside curly braces {index}
2. Keyword parameters - list of parameters of type key=value, that can be accessed with
key of parameter inside curly braces {key}

16
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Positional Arguments

• Argument 0 is a string "Adam" and Argument 1 is a floating number 230.2346

17
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Keyword Arguments

• Instead of just the parameters, a key-value for the parameters is


used; name="Adam" and blc=230.2346

18
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Formatting Strings - Examples
# default arguments
print("Hello {}, your balance is {}.".format("Adam", 230.2346))
# positional arguments
print("Hello {0}, your balance is {1}.".format("Adam", 230.2346))
#keyword arguments
print("Hello {name}, your balance is {blc}.".format(name="Adam", blc=230.2346))
# mixed arguments
print("Hello {0}, your balance is {blc}.".format("Adam", blc=230.2346))

• All the above statements displays the same output


Hello Adam, your balance is 230.2346.

19
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
IO Operations - Example
# inputs two integers and displays their sum

>>> first = int(input( "Enter the first number: “ ))


Enter the first number: 23

>>> second = int(input( "Enter the second number: “ ))


Enter the second number: 44

>>> print( "The sum is“ , first + second)


The sum is 67

20
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Summary
• The print() function prints a list of items
• The input() function prints an optional prompt message and reads in a single line of
input
• Output formatting can be done using format() method of String class

21
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences

You might also like