Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Python Module 4 I O

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 25

04

.
Input & output
operations in
Python
Input & output Operations

● I/O Statements
● Escape Sequences
● Formatted I/O
Input & output statements
wHat are i/0
statements?

An input/output statement or I/O statement is a


portion of a program that instructs a computer how to
read and process information.

Functions like input() and print() are widely used for


standard input and output operations respectively.
Input function
wHat is input
Function?

The input() function allows a user to insert a value into


a program. input() returns a string value. You can
convert the contents of an input using any data type.
Python provides an input() function.
Example:
#Enter a input
a = input() Output
Print(a) a=3
Input function
wHat is input
Function?

Input function with the display string. The display


string can be placed either using single quotes or
double quotes.
Example:
#Enter your name
name = input(‘Enter your name’)
Print (name)
Output
name = ‘Python’
Input function
wHat is input
Function?

Python takes all the input as a string data type by


default. To convert it to any other data type we have to
convert the input using explicit type conversion. For
example, to convert the input to int or float we have to
use the int() and float() method respectively.
Example:
#Enter your age
a = int(input(‘Enter your age’))
Output
add = a+1 a = 31
print (add)
input formatting
syntax - input()
Input Output

num = input('Enter a number: ')


print(num) ‘10’

If you notice the above code, although the given input is of number the output is considered as a string data type.
You may use type conversion to convert the above number either into int or float data type.

num = int(input('Enter a number: '))


print(num) 10
#int() is a type conversion function. It helps to convert to integer

num = float(input('Enter a number: '))


print(num) 10.0
#float() is a type conversion function. It helps to convert to float
LMS Activity
Output - print function
wHat are Output
statements?

We use the print() function to get an output data to the


standard output device (screen).
Example:
a=5
print (‘The value of a is’, a)

Output
The value of a is 5
Standard print () function
Syntax

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)


Here, objects are the value(s) to be printed.

sep - The separator is used between the values. It defaults into a space
character.

After all values are printed, the specified end symbol is printed. By default
it creates a new line.

The file is the object where the values are printed and its default value is
sys.stdout (screen). The flush makes sure that any output that is buffered
goes to the destination.
Standard print () function
Object
s
Objects is the value or values to be printed.

The values are converted to string before print.


Example:
a = ‘Hello World’
print (a)
Output
Hello World
Standard print () function
separator

The separator or sep is used between the values. It


defaults into a space character.

Note: If you don’t specify separator, space is used as a


separator by default.
Example:
Output
print(1,2,3,4, sep=”,”) 1, 2, 3, 4
Standard print () function
end

End ( end ) specify what to print at the end.

Note: It defaults into a new line.


Example:
Output
print(1, 2, 3, 4, sep=’*’, end=’&’)
1*2*3*4&
Standard print () function
file

File (file) is the object where the values are printed and
its default is sys.stdout (screen).
Standard print () function
flush

The flush() method in Python file handling clears the


internal buffer of the file. The flush makes sure that
any output that is buffered goes to the destination.
LMS Activity
Output formatting
String
formatting

This can be done by using the str.format() method.


This method is visible to any string object.
Example:
age = 20
print(‘I am {} years old’.format(age))

Output
I am 20 years old
output formatting
String formatting
examples
Input Output

x = 15
y = 20 The value of x is 15 and y is 20
print('The value of x is {} and y is {}'.format(x,y))

print('I Love {0} and {1}'.format('Coding','Programming')) I Love Coding and Programming

print('I Love {1} and {0}'.format('Coding','Programming')) I Love Programming and Coding

print('Hello {name}, {greeting}'.format(greeting =


Hello John, Goodmorning
'Goodmorning', name = 'John'))
LMS Activity
Output formatting
Escape
sequence
Escape Sequence Description Example Output

print("line1 \ line1 line2 line3


Backslash means new
\ line is ignored
line2 \
line3")

print("Python \n Python
\n New line
Programming") Programming

print("Python \t
\t ASCII Horizontal Tab (TAB) Python Programming
Programming")
Output formatting
% Operator -
float
Input Output Remarks

x = 12.3456789 It display after decimal point two


The value of x is 12.35
print('The value of x is %.2f' %x) digits because of 2f.

It display after decimal point four


print('The value of x is %.4f' %x) The value of x is 12.3457
digits because of 4f.

It display after decimal point five


print('The value of x is %.5f' %x) The value of x is 12.34568
digits because of 5f.
LMS Activity
Split method
What is split
method?
The split() method splits a string into a list. You can
specify the separator, default separator is any
whitespace.
Syntax
string.split(separator, maxsplit)
Example:
txt = "I Love Python Programming"
x = txt.split(" ")
print(x)

Output
['I', 'Love', 'Python', 'Programming']
Map Function
What is Map
Function?

Python's map() is a built-in function that allows you to


process and transform all the items in an iterable
without using an explicit for loop, a technique
commonly known as mapping.

The map() is useful when you need to apply a


transformation function to each item in an iterable.
Map Function
What is Map
Function?

Example
x, y, z = map(int,input("enter numbers: ").split())
print(x+y+z)

Output
enter numbers: 1 2 3
6

You might also like