11 IO Operations
11 IO Operations
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
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
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
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
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
• 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
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()
16
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Positional Arguments
17
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Keyword Arguments
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))
19
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
IO Operations - Example
# inputs two integers and displays their sum
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