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

What's hot (20)

PDF
Python Unit 3 - Control Flow and Functions
DhivyaSubramaniyam
 
PPTX
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
vikram mahendra
 
PPTX
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
vikram mahendra
 
PPTX
Python basics
NexThoughts Technologies
 
PPTX
Introduction to Python Programming
VijaySharma802
 
PPT
Lecture 8- Data Input and Output
Md. Imran Hossain Showrov
 
PPTX
Python programming workshop session 1
Abdul Haseeb
 
PDF
Python unit 2 as per Anna university syllabus
DhivyaSubramaniyam
 
DOCX
Python unit 3 and Unit 4
Anandh Arumugakan
 
PPTX
Python programming workshop
BAINIDA
 
PPTX
C programming(part 3)
Dr. SURBHI SAROHA
 
PDF
Arrays in C++
Ilio Catallo
 
PPT
Lecture 6- Intorduction to C Programming
Md. Imran Hossain Showrov
 
PPT
Functions and pointers_unit_4
Saranya saran
 
PPTX
Python programming
Ashwin Kumar Ramasamy
 
PPT
Arrays
Saranya saran
 
PPTX
Operators and Control Statements in Python
RajeswariA8
 
PPTX
Python for Beginners(v2)
Panimalar Engineering College
 
PPTX
DATA TYPE IN PYTHON
vikram mahendra
 
DOCX
Maharishi University of Management (MSc Computer Science test questions)
Dharma Kshetri
 
Python Unit 3 - Control Flow and Functions
DhivyaSubramaniyam
 
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
vikram mahendra
 
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
vikram mahendra
 
Introduction to Python Programming
VijaySharma802
 
Lecture 8- Data Input and Output
Md. Imran Hossain Showrov
 
Python programming workshop session 1
Abdul Haseeb
 
Python unit 2 as per Anna university syllabus
DhivyaSubramaniyam
 
Python unit 3 and Unit 4
Anandh Arumugakan
 
Python programming workshop
BAINIDA
 
C programming(part 3)
Dr. SURBHI SAROHA
 
Arrays in C++
Ilio Catallo
 
Lecture 6- Intorduction to C Programming
Md. Imran Hossain Showrov
 
Functions and pointers_unit_4
Saranya saran
 
Python programming
Ashwin Kumar Ramasamy
 
Operators and Control Statements in Python
RajeswariA8
 
Python for Beginners(v2)
Panimalar Engineering College
 
DATA TYPE IN PYTHON
vikram mahendra
 
Maharishi University of Management (MSc Computer Science test questions)
Dharma Kshetri
 

Similar to Unit2 input output (20)

PPTX
Python variables in the computer science.pptx
Rajasekhar364622
 
PPTX
Python knowledge ,......................
sabith777a
 
PPTX
Chapter1 python introduction syntax general
ssuser77162c
 
PPTX
pythonforaipythonforaipythonforaipython.pptx
mlakshmi28
 
PDF
python-online&offline-training-in-kphb-hyderabad (1) (1).pdf
KosmikTech1
 
PPT
Python Programming Introduction demo.ppt
JohariNawab
 
PPTX
Learn Python The Hard Way Presentation
Amira ElSharkawy
 
PPTX
Introduction to learn and Python Interpreter
Alamelu
 
PPTX
Python 01.pptx
AliMohammadAmiri
 
PPT
Phyton Learning extracts
Pavan Babu .G
 
PPTX
Python (Data Analysis) cleaning and visualize
IruolagbePius
 
PPTX
PROGRAMMING FOR PROBLEM SOLVING FOR STRING CONCEPT
factsandknowledge94
 
PPTX
#Code2Create: Python Basics
GDGKuwaitGoogleDevel
 
PPTX
lecture 2.pptx
Anonymous9etQKwW
 
PPTX
Fundamentals of Python Programming
Kamal Acharya
 
PPTX
Python ppt
Anush verma
 
PPTX
python_class.pptx
chandankumar943868
 
PDF
Python quick guide
Hasan Bisri
 
PDF
Python Objects
MuhammadBakri13
 
PDF
python.pdf
BurugollaRavi1
 
Python variables in the computer science.pptx
Rajasekhar364622
 
Python knowledge ,......................
sabith777a
 
Chapter1 python introduction syntax general
ssuser77162c
 
pythonforaipythonforaipythonforaipython.pptx
mlakshmi28
 
python-online&offline-training-in-kphb-hyderabad (1) (1).pdf
KosmikTech1
 
Python Programming Introduction demo.ppt
JohariNawab
 
Learn Python The Hard Way Presentation
Amira ElSharkawy
 
Introduction to learn and Python Interpreter
Alamelu
 
Python 01.pptx
AliMohammadAmiri
 
Phyton Learning extracts
Pavan Babu .G
 
Python (Data Analysis) cleaning and visualize
IruolagbePius
 
PROGRAMMING FOR PROBLEM SOLVING FOR STRING CONCEPT
factsandknowledge94
 
#Code2Create: Python Basics
GDGKuwaitGoogleDevel
 
lecture 2.pptx
Anonymous9etQKwW
 
Fundamentals of Python Programming
Kamal Acharya
 
Python ppt
Anush verma
 
python_class.pptx
chandankumar943868
 
Python quick guide
Hasan Bisri
 
Python Objects
MuhammadBakri13
 
python.pdf
BurugollaRavi1
 
Ad

Recently uploaded (20)

PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PDF
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PDF
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PPTX
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
Dimensions of Societal Planning in Commonism
StefanMz
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
Ad

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']