SlideShare a Scribd company logo
Python provides numerous built-in functions that are
readily available to us at the Python prompt.
Some of the functions like input() and print() are
widely used for standard input and output operations
respectively
Python Input and Output
Python Input
• In Python, we have the input() function to allow to take the input
from the user. The syntax for input() is
input([prompt])
where prompt is the string we wish to display on the screen. It is
optional.
>>> num = input('Enter a number: ')
Enter a number: 10
>>> num
'10'
Here, we can see that the entered value 10 is a string, not a number. To
convert this into a number we can use int() or float() functions.
>>> int('10')
10
>>> float('10')
10.0
• This same operation can be performed using the eval() function. But
it takes it further. It can evaluate even expressions, provided the input
is a string
>>> int('2+3')
Traceback (most recent call last):
File "<string>", line 301, in runcode
File "<interactive input>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '2+3'
>>> eval('2+3')
5
Python Output Using print() function
The print() function to output data to the standard output device
(screen).
• We can also output data to a file, but this will be discussed later. An
example use is given below.
print('This sentence is output to the screen')
# Output: This sentence is output to the screen
a = 5
print('The value of a is', a)
# Output: The value of a is 5
• In the second print() statement, we can notice that a space was
added between the string and the value of variable a. This is by
default, but we can change it.
• The actual syntax of the print() function is
print(*objects, sep=' ', end='n', file=sys.stdout, flush=False)
• Here,
• The sep separator is used between the values. It defaults into a space
character.
• After all values are printed, end is printed. It defaults into a new line.
objects is the value(s) to be printed.
• The file is the object where the values are printed and its default
value is sys.stdout (screen).
Here are an example to illustrate this
print(1,2,3,4)
# Output: 1 2 3 4
print(1,2,3,4,sep='*')
# Output: 1*2*3*4
print(1,2,3,4,sep='#',end='&')
# Output: 1#2#3#4&
Output formatting
• Sometimes we would like to format our output to make it look
attractive. This can be done by using the str.format() method. This
method is visible to any string object.
>>> x = 5; y = 10
>>> print('The value of x is {} and y is {}'.format(x,y))
The value of x is 5 and y is 10
• Here the curly braces {} are used as placeholders. We can specify the
order in which it is printed by using numbers (tuple index).
print('I love {0} and {1}'.format('bread','butter'))
# Output: I love bread and butter
print('I love {1} and {0}'.format('bread','butter'))
# Output: I love butter and bread
• We can even use keyword arguments to format the string.
>>> print('Hello {name}, {greeting}'.format(greeting = 'Goodmorning', name =
'John'))
Hello John, Goodmorning
We can even format strings like the old sprintf() style used in C
programming language. We use the % operator to accomplish this.
>>> x = 12.3456789
>>> print('The value of x is %3.2f' %x)
The value of x is 12.35
>>> print('The value of x is %3.4f' %x)
The value of x is 12.3457
Python Import
• When our program grows bigger, it is a good idea to break it into
different modules.
• A module is a file containing Python definitions and statements.
Python modules have a filename and end with the extension .py.
• Definitions inside a module can be imported to another module or
the interactive interpreter in Python. We use the import keyword to
do this.
• For example, we can import the math module by typing in import
math.
import math
print(math.pi)
• Now all the definitions inside math module are available in our scope. We can also
import some specific attributes and functions only, using the from keyword. For example:
>>> from math import pi
>>> pi
3.141592653589793
While importing a module, Python looks at several places defined in sys.path. It is a list of
directory locations.
>>> import sys
>>> sys.path
['', 'C:UsersDeepakAppDataLocalProgramsPythonPython37-32Libidlelib',
'C:UsersDeepakAppDataLocalProgramsPythonPython37-32python37.zip',
'C:UsersDeepakAppDataLocalProgramsPythonPython37-32DLLs',
'C:UsersDeepakAppDataLocalProgramsPythonPython37-32lib',
'C:UsersDeepakAppDataLocalProgramsPythonPython37-32',
'C:UsersDeepakAppDataLocalProgramsPythonPython37-32libsite-packages']

More Related Content

PDF
Python-02| Input, Output & Import
PPTX
USER DEFINE FUNCTIONS IN PYTHON
PPTX
GE8151 Problem Solving and Python Programming
PPTX
OPERATOR IN PYTHON-PART2
PPTX
INTRODUCTION TO FUNCTIONS IN PYTHON
PPTX
Python Programming Essentials - M16 - Control Flow Statements and Loops
PPTX
USE OF PRINT IN PYTHON PART 2
Python-02| Input, Output & Import
USER DEFINE FUNCTIONS IN PYTHON
GE8151 Problem Solving and Python Programming
OPERATOR IN PYTHON-PART2
INTRODUCTION TO FUNCTIONS IN PYTHON
Python Programming Essentials - M16 - Control Flow Statements and Loops
USE OF PRINT IN PYTHON PART 2

What's hot (20)

PDF
Python Unit 3 - Control Flow and Functions
PPTX
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
PPTX
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
PPTX
PPTX
Introduction to Python Programming
PPT
Lecture 8- Data Input and Output
PPTX
Python programming workshop session 1
PDF
Python unit 2 as per Anna university syllabus
DOCX
Python unit 3 and Unit 4
PPTX
Python programming workshop
PPTX
C programming(part 3)
PDF
Arrays in C++
PPT
Lecture 6- Intorduction to C Programming
PPT
Functions and pointers_unit_4
PPTX
Python programming
PPT
PPTX
Operators and Control Statements in Python
PPTX
Python for Beginners(v2)
PPTX
DATA TYPE IN PYTHON
DOCX
Maharishi University of Management (MSc Computer Science test questions)
Python Unit 3 - Control Flow and Functions
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
Introduction to Python Programming
Lecture 8- Data Input and Output
Python programming workshop session 1
Python unit 2 as per Anna university syllabus
Python unit 3 and Unit 4
Python programming workshop
C programming(part 3)
Arrays in C++
Lecture 6- Intorduction to C Programming
Functions and pointers_unit_4
Python programming
Operators and Control Statements in Python
Python for Beginners(v2)
DATA TYPE IN PYTHON
Maharishi University of Management (MSc Computer Science test questions)
Ad

Similar to Unit2 input output (20)

PPTX
Python variables in the computer science.pptx
PPTX
lecture 2.pptx
PPT
Python Programming Introduction demo.ppt
PDF
Advanced Web Technology ass.pdf
PPTX
Python basics
PPTX
CLASS-11 & 12 ICT PPT PYTHON PROGRAMMING.pptx
PDF
Pres_python_talakhoury_26_09_2023.pdf
PPTX
Functions
PPTX
Keep it Stupidly Simple Introduce Python
PPTX
Python knowledge ,......................
PPTX
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
PPTX
Chapter1 python introduction syntax general
ODP
Hands on Session on Python
PDF
python-online&offline-training-in-kphb-hyderabad (1) (1).pdf
PPTX
Fundamentals of functions in C program.pptx
PPTX
PDF
headerfilesinc-181121134545 (1).pdf
PDF
introduction to python programming course 2
PPTX
Python Details Functions Description.pptx
PPTX
Function in c program
Python variables in the computer science.pptx
lecture 2.pptx
Python Programming Introduction demo.ppt
Advanced Web Technology ass.pdf
Python basics
CLASS-11 & 12 ICT PPT PYTHON PROGRAMMING.pptx
Pres_python_talakhoury_26_09_2023.pdf
Functions
Keep it Stupidly Simple Introduce Python
Python knowledge ,......................
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Chapter1 python introduction syntax general
Hands on Session on Python
python-online&offline-training-in-kphb-hyderabad (1) (1).pdf
Fundamentals of functions in C program.pptx
headerfilesinc-181121134545 (1).pdf
introduction to python programming course 2
Python Details Functions Description.pptx
Function in c program
Ad

Recently uploaded (20)

PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
Lesson notes of climatology university.
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
master seminar digital applications in india
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
Final Presentation General Medicine 03-08-2024.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Final Presentation General Medicine 03-08-2024.pptx
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Lesson notes of climatology university.
STATICS OF THE RIGID BODIES Hibbelers.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
Pharmacology of Heart Failure /Pharmacotherapy of CHF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Anesthesia in Laparoscopic Surgery in India
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Microbial disease of the cardiovascular and lymphatic systems
01-Introduction-to-Information-Management.pdf
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
master seminar digital applications in india
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
2.FourierTransform-ShortQuestionswithAnswers.pdf
Supply Chain Operations Speaking Notes -ICLT Program

Unit2 input output

  • 1. Python provides numerous built-in functions that are readily available to us at the Python prompt. Some of the functions like input() and print() are widely used for standard input and output operations respectively Python Input and Output
  • 2. Python Input • In Python, we have the input() function to allow to take the input from the user. The syntax for input() is input([prompt]) where prompt is the string we wish to display on the screen. It is optional. >>> num = input('Enter a number: ') Enter a number: 10 >>> num '10' Here, we can see that the entered value 10 is a string, not a number. To convert this into a number we can use int() or float() functions. >>> int('10') 10 >>> float('10') 10.0
  • 3. • This same operation can be performed using the eval() function. But it takes it further. It can evaluate even expressions, provided the input is a string >>> int('2+3') Traceback (most recent call last): File "<string>", line 301, in runcode File "<interactive input>", line 1, in <module> ValueError: invalid literal for int() with base 10: '2+3' >>> eval('2+3') 5
  • 4. Python Output Using print() function The print() function to output data to the standard output device (screen). • We can also output data to a file, but this will be discussed later. An example use is given below. print('This sentence is output to the screen') # Output: This sentence is output to the screen a = 5 print('The value of a is', a) # Output: The value of a is 5
  • 5. • In the second print() statement, we can notice that a space was added between the string and the value of variable a. This is by default, but we can change it. • The actual syntax of the print() function is print(*objects, sep=' ', end='n', file=sys.stdout, flush=False) • Here, • The sep separator is used between the values. It defaults into a space character. • After all values are printed, end is printed. It defaults into a new line. objects is the value(s) to be printed. • The file is the object where the values are printed and its default value is sys.stdout (screen).
  • 6. Here are an example to illustrate this print(1,2,3,4) # Output: 1 2 3 4 print(1,2,3,4,sep='*') # Output: 1*2*3*4 print(1,2,3,4,sep='#',end='&') # Output: 1#2#3#4&
  • 7. Output formatting • Sometimes we would like to format our output to make it look attractive. This can be done by using the str.format() method. This method is visible to any string object. >>> x = 5; y = 10 >>> print('The value of x is {} and y is {}'.format(x,y)) The value of x is 5 and y is 10 • Here the curly braces {} are used as placeholders. We can specify the order in which it is printed by using numbers (tuple index). print('I love {0} and {1}'.format('bread','butter')) # Output: I love bread and butter print('I love {1} and {0}'.format('bread','butter')) # Output: I love butter and bread
  • 8. • We can even use keyword arguments to format the string. >>> print('Hello {name}, {greeting}'.format(greeting = 'Goodmorning', name = 'John')) Hello John, Goodmorning We can even format strings like the old sprintf() style used in C programming language. We use the % operator to accomplish this. >>> x = 12.3456789 >>> print('The value of x is %3.2f' %x) The value of x is 12.35 >>> print('The value of x is %3.4f' %x) The value of x is 12.3457
  • 9. Python Import • When our program grows bigger, it is a good idea to break it into different modules. • A module is a file containing Python definitions and statements. Python modules have a filename and end with the extension .py. • Definitions inside a module can be imported to another module or the interactive interpreter in Python. We use the import keyword to do this. • For example, we can import the math module by typing in import math. import math print(math.pi)
  • 10. • Now all the definitions inside math module are available in our scope. We can also import some specific attributes and functions only, using the from keyword. For example: >>> from math import pi >>> pi 3.141592653589793 While importing a module, Python looks at several places defined in sys.path. It is a list of directory locations. >>> import sys >>> sys.path ['', 'C:UsersDeepakAppDataLocalProgramsPythonPython37-32Libidlelib', 'C:UsersDeepakAppDataLocalProgramsPythonPython37-32python37.zip', 'C:UsersDeepakAppDataLocalProgramsPythonPython37-32DLLs', 'C:UsersDeepakAppDataLocalProgramsPythonPython37-32lib', 'C:UsersDeepakAppDataLocalProgramsPythonPython37-32', 'C:UsersDeepakAppDataLocalProgramsPythonPython37-32libsite-packages']