SlideShare a Scribd company logo
1 of 29
INPUT STATEMENT
Mrs.N.Kavitha
Head,Department of Computer Science,
E.M.G.Yadava Women’s College
Madurai-14.
DEFINITION
In Python, we use the input() function to take input from the user. Whatever you
enter as input, the input function converts it into a string. If you enter an integer value
still input() function converts it into a string.
There are two functions that can be used to read data or input from the user in
python: raw_input() and input(). The results can be stored into a variable. raw_input() -
It reads the input or command and returns a string. input() - Reads the input and returns
a python type like list, tuple, int, etc
raw_input() Function:
The raw_input() Function which is available in python2 is used to take the input
entered by the user.The raw_input() function explicitly converts the entered data into a
string and returns the value.The raw_input() can be used only in python2.
example:
name=raw_input(“Enter your name:”)
print(“Hi %s,Let us be friends!”%name);
output:
Enter your name: Jeyabharathi
Hi Jeyabharathi,Let us be friends!
input() Function
In Python,we use the input() Function to take input from the user.Whatever you
enter as input,the input function converts it into a string.If you enter an integer value
still input() function converts it into a string.
example:
value=input{“Enter the string:”}
print{f’you entered{value}’}
output:
Enter the string: Apple
you entered Apple
Difference between input() and raw_input() Function
raw_input function input() Function
• The return type of raw_input is always string The return type of input need not be string only.
• raw_input() function takes the point from the
user.
input() function take the user input.
• its Syntax:
raw_input(input)
its syntax:
input(prompt)
• It takes only one parameter that is the input. It takes only one parameter that us prompt.
• It is only introduced in python 2.0 version hang till
user inputs
It converts the input into a string by removing the
trailing newline blocks untikl input received
• “hello world” but string “hello world”
• foo in snake_case
To accept input from keyboard,Python provides the input() function. This function takes
a value from the keyboard and returns it as a string.For example..,
str=input()
priya
print(str)
OUTPUT: priya
it is a better idea to display a message to the user so that the user understands what to
enter.This can be done by writing a message inside the input() function as:
str=input(“Enter your name:”)
Enter your name: Priya
print(str)
OUTPUT: Priya
once the value comes into the variable ‘str’,it can be converted into ‘int’ or ‘float’ etc.
This is useful to accept numbers as:
str=input(‘Enter a number:’)
Enter a number: 123
x=int(str)
print(x)
OUTPUT: 125
We can use the int() function before the input() function to accept an integer from the
keyboard as:
x=int(input(“Enter a number:”)
Enter a number: 123
print(x)
OUTPUT: 123
Similarly, to accept a float value from the keyboard ,we can use the float() functionn
along with the innput() function as:
x=float(input(“Enter a number:”))
Enter a number: 12.143
print(x)
OUTPUT: 12.143
we will understand these concepts with the help of a python program. Let’s write a
program to accept a string and dispalyed it..,
example :
str=input(“Enter a string:”)
print(‘U entered:’,str)
OUTPUT :
we can understand that the input() function is accepting the character as a string
only. if we need only a character, then we should use the index, as: ch[0]. Here 0th
character is taken from the string.
example :
ch=input(“Enter a char”)
print(“U entered:”,+ch[0])
OUTPUT :
A python program to accept a float number from keyboard.
example :
x=input(“Enter a number:”)
print(‘U entered:’,x)
output:
From the above output,we can understand that the float values are displayed to an
accurancy of 15 digits after decimal point. In the next program,we will accept two
integer numbers and display them.
A program to accept two integer numbers from keyboard.
example :
x=int(input(“Enter first number:”))
y=int(input(“Enter second number:”))
print(‘u entered:’,x,y)
OUTPUT
The two numbers in the output are displayed using a space. Suppose we want to display
these numbers using a comma as seperator, then we can use sep=’ ,’ in the print()
function as follows.,
In this case,the output will be..,
Observe the commas after “U entered:’ and after 12.A better way to display these
numbers seperating them using commas in this,
print(‘u entered:’, x , y, sep=’ , ’);
u entered: 12 , 2
print(‘U entered: %d, %d ‘ % (x,y))
A Python program to convert numbers from other systems into decimal number system.
example:
str=input(‘Enter hexadecimal number:’)
n=int(str, 16)
print(‘Hexadecimal to Decimal=’ , n);
str=input(‘Enter Octal number:’)
n=int(str, 8)
print(‘Octal to Decimal=’ , n);
str=input(‘Enter binary number:’)
n=int(str, 2)
print(‘Binary to Decimal=’ , n);
OUTPUT
To accept more tha n one input in the same liine,we can use a for llop along with the
input() function in the following format,
In the previous statement, the input() function will display the message ‘Enter two
numbers:’ to the user. When the user enters two values, they are accepted as strings.
These strings are divided wherever a space is found by split() method. so, we get two
strings as elements of a list.These strings are read by for loop and converted into
integers by the int() function.These integers are finally stored into a and b.
The split() method by default splits the values where a space is found.Hence while
entering the numbers, the user should perate them using a space.The square brackets[ ]
around the total expression indicates that the input is accepted as elements of a list
a , b = [ int(x) for x in input (“Enter two numbers :”) , split () ]
COMMAND LINE ARGUMENTS :
We can design our programs in such a way that we can pass inputs t the program
when we run command. For example, we write a program by the name ‘sum.py’ that
takes numbers and sums them. We can supply the two numbers as input to the program
at the time of running the program at command prompt as,
Here,sum.py is our program name.While running this program,we are passing two
arguments 12 and 20 t the program,which are called command line arguments.so
command line arguments are the values to a python program at command prompt or
system prompt.Command line arguments are passed to the program from outside the
program.All the arguments should be entered from the keyboard seperating them by a
space.
c:> python sum.py 12 20
These arguments are stored by default in the form of strings in a list in the name ‘argv’
which is available in sys module. Since argv is a list that contains all values passed to
tha program,argv[0] represents the name of the program,argv[1] represents the first
value,argv[2] represents the second value and so on..,,
c:/> python sum.py 12 20
argv
argv[0] argv[1] argv[2]
command line args are stored as strings in argv list
‘sum.py’ 12 20
In command line arguments, they are various ways of dealing with these types of
arguments.The three most common are:
using sys.argv
using getopt module
using argparse module
Using sys.argv
The sys module provides funnctions and variables used to manipulate different
parts of the python runtime environment .This module provides access to some
variables used or maintained by the interpreter and to functions that innteract strongly
with the interpreter.
we want to find the number of command line arguments,we can use the len()
function as: len(argv).The following program reads the command line arguments
entered at command prompt and displayss them.
example:
import sys
n=len(sys.argv)
args=sys.argv
print(‘no. of comman line args=’,n)
print(‘The args are: ,args)
print(‘The args one by one:’)
for a in args:
print(a)
OUTPUT
using getopt module
Python getopt module is similar to the getopt() function of C.Unlike sys module getopt
module extends the seperation of the input string by parameter validation.it allows both short,
and long options including a value assignment.However,this module requires the use of the
sys module to process input data properly.To use getopt module,it is required to remove the
first element from the list of command-line arguments.
example:
import sys
import getopt
def full_name():
first_name=None
last_name=None
argv=sys.argv[1:]
try:
opts,args=getopt.getopt(argv,”f:l:”)
except:
print(“Error”)
for opt,arg in opts:
if opt in [ ‘ -f ‘]:
first_name = arg
elif opt in [‘ -l ‘]:
lats_name = arg
print(first_name +” “ + last_name)
full_name()
OUTPUT:
Using argparse module
Using argparse module is a better option than the above two options as it provides a
lot of options such as positional arguments,default value for arguments,help
message ,specifying data type of argument etc..,
example
import argparse
parser=argparse.ArgumentParser()
parser.add_argument("a")
args=parser.parse_args()
OUTPUT
The Input Statement in Core Python .pptx

More Related Content

Similar to The Input Statement in Core Python .pptx

Functions of stdio conio
Functions of stdio   conio Functions of stdio   conio
Functions of stdio conio Bhavik Vashi
 
Python lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce functionPython lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce functionARVIND PANDE
 
Function in c program
Function in c programFunction in c program
Function in c programumesh patil
 
Python Training in Bangalore | Python Introduction Session | Learnbay
Python Training in Bangalore |  Python Introduction Session | LearnbayPython Training in Bangalore |  Python Introduction Session | Learnbay
Python Training in Bangalore | Python Introduction Session | LearnbayLearnbayin
 
Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programmingIcaii Infotech
 
C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2Mohamed Ahmed
 
Unit-IV Strings.pptx
Unit-IV Strings.pptxUnit-IV Strings.pptx
Unit-IV Strings.pptxsamdamfa
 
python lab programs.pdf
python lab programs.pdfpython lab programs.pdf
python lab programs.pdfCBJWorld
 

Similar to The Input Statement in Core Python .pptx (20)

Functions of stdio conio
Functions of stdio   conio Functions of stdio   conio
Functions of stdio conio
 
Python lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce functionPython lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce function
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Python Training in Bangalore | Python Introduction Session | Learnbay
Python Training in Bangalore |  Python Introduction Session | LearnbayPython Training in Bangalore |  Python Introduction Session | Learnbay
Python Training in Bangalore | Python Introduction Session | Learnbay
 
Lecture 2 java.pdf
Lecture 2 java.pdfLecture 2 java.pdf
Lecture 2 java.pdf
 
Functions
FunctionsFunctions
Functions
 
Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programming
 
UNIT-II CP DOC.docx
UNIT-II CP DOC.docxUNIT-II CP DOC.docx
UNIT-II CP DOC.docx
 
C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2
 
Lecture 8- Data Input and Output
Lecture 8- Data Input and OutputLecture 8- Data Input and Output
Lecture 8- Data Input and Output
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Python 3.pptx
Python 3.pptxPython 3.pptx
Python 3.pptx
 
Array Cont
Array ContArray Cont
Array Cont
 
Data Handling
Data Handling Data Handling
Data Handling
 
Computer programming 2 chapter 1-lesson 2
Computer programming 2  chapter 1-lesson 2Computer programming 2  chapter 1-lesson 2
Computer programming 2 chapter 1-lesson 2
 
MODULE. .pptx
MODULE.                              .pptxMODULE.                              .pptx
MODULE. .pptx
 
Function
FunctionFunction
Function
 
Python.pptx
Python.pptxPython.pptx
Python.pptx
 
Unit-IV Strings.pptx
Unit-IV Strings.pptxUnit-IV Strings.pptx
Unit-IV Strings.pptx
 
python lab programs.pdf
python lab programs.pdfpython lab programs.pdf
python lab programs.pdf
 

More from Kavitha713564

THE PACKAGES CONCEPT IN JAVA PROGRAMMING.pptx
THE PACKAGES CONCEPT  IN JAVA PROGRAMMING.pptxTHE PACKAGES CONCEPT  IN JAVA PROGRAMMING.pptx
THE PACKAGES CONCEPT IN JAVA PROGRAMMING.pptxKavitha713564
 
The Java Server Page in Java Concept.pptx
The Java Server Page in Java Concept.pptxThe Java Server Page in Java Concept.pptx
The Java Server Page in Java Concept.pptxKavitha713564
 
Programming in python in detail concept .pptx
Programming in python in detail concept .pptxProgramming in python in detail concept .pptx
Programming in python in detail concept .pptxKavitha713564
 
The Datatypes Concept in Core Python.pptx
The Datatypes Concept in Core Python.pptxThe Datatypes Concept in Core Python.pptx
The Datatypes Concept in Core Python.pptxKavitha713564
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javaKavitha713564
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaKavitha713564
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaKavitha713564
 
Input output files in java
Input output files in javaInput output files in java
Input output files in javaKavitha713564
 
Arrays,string and vector
Arrays,string and vectorArrays,string and vector
Arrays,string and vectorKavitha713564
 

More from Kavitha713564 (14)

THE PACKAGES CONCEPT IN JAVA PROGRAMMING.pptx
THE PACKAGES CONCEPT  IN JAVA PROGRAMMING.pptxTHE PACKAGES CONCEPT  IN JAVA PROGRAMMING.pptx
THE PACKAGES CONCEPT IN JAVA PROGRAMMING.pptx
 
The Java Server Page in Java Concept.pptx
The Java Server Page in Java Concept.pptxThe Java Server Page in Java Concept.pptx
The Java Server Page in Java Concept.pptx
 
Programming in python in detail concept .pptx
Programming in python in detail concept .pptxProgramming in python in detail concept .pptx
Programming in python in detail concept .pptx
 
The Datatypes Concept in Core Python.pptx
The Datatypes Concept in Core Python.pptxThe Datatypes Concept in Core Python.pptx
The Datatypes Concept in Core Python.pptx
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
Applet in java new
Applet in java newApplet in java new
Applet in java new
 
Basic of java
Basic of javaBasic of java
Basic of java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
 
Arrays,string and vector
Arrays,string and vectorArrays,string and vector
Arrays,string and vector
 

Recently uploaded

Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 

Recently uploaded (20)

Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 

The Input Statement in Core Python .pptx

  • 1. INPUT STATEMENT Mrs.N.Kavitha Head,Department of Computer Science, E.M.G.Yadava Women’s College Madurai-14.
  • 2. DEFINITION In Python, we use the input() function to take input from the user. Whatever you enter as input, the input function converts it into a string. If you enter an integer value still input() function converts it into a string. There are two functions that can be used to read data or input from the user in python: raw_input() and input(). The results can be stored into a variable. raw_input() - It reads the input or command and returns a string. input() - Reads the input and returns a python type like list, tuple, int, etc
  • 3. raw_input() Function: The raw_input() Function which is available in python2 is used to take the input entered by the user.The raw_input() function explicitly converts the entered data into a string and returns the value.The raw_input() can be used only in python2. example: name=raw_input(“Enter your name:”) print(“Hi %s,Let us be friends!”%name); output: Enter your name: Jeyabharathi Hi Jeyabharathi,Let us be friends!
  • 4. input() Function In Python,we use the input() Function to take input from the user.Whatever you enter as input,the input function converts it into a string.If you enter an integer value still input() function converts it into a string. example: value=input{“Enter the string:”} print{f’you entered{value}’} output: Enter the string: Apple you entered Apple
  • 5. Difference between input() and raw_input() Function raw_input function input() Function • The return type of raw_input is always string The return type of input need not be string only. • raw_input() function takes the point from the user. input() function take the user input. • its Syntax: raw_input(input) its syntax: input(prompt) • It takes only one parameter that is the input. It takes only one parameter that us prompt. • It is only introduced in python 2.0 version hang till user inputs It converts the input into a string by removing the trailing newline blocks untikl input received • “hello world” but string “hello world” • foo in snake_case
  • 6. To accept input from keyboard,Python provides the input() function. This function takes a value from the keyboard and returns it as a string.For example.., str=input() priya print(str) OUTPUT: priya it is a better idea to display a message to the user so that the user understands what to enter.This can be done by writing a message inside the input() function as: str=input(“Enter your name:”) Enter your name: Priya print(str) OUTPUT: Priya
  • 7. once the value comes into the variable ‘str’,it can be converted into ‘int’ or ‘float’ etc. This is useful to accept numbers as: str=input(‘Enter a number:’) Enter a number: 123 x=int(str) print(x) OUTPUT: 125 We can use the int() function before the input() function to accept an integer from the keyboard as: x=int(input(“Enter a number:”) Enter a number: 123 print(x) OUTPUT: 123
  • 8. Similarly, to accept a float value from the keyboard ,we can use the float() functionn along with the innput() function as: x=float(input(“Enter a number:”)) Enter a number: 12.143 print(x) OUTPUT: 12.143 we will understand these concepts with the help of a python program. Let’s write a program to accept a string and dispalyed it.., example : str=input(“Enter a string:”) print(‘U entered:’,str)
  • 10. we can understand that the input() function is accepting the character as a string only. if we need only a character, then we should use the index, as: ch[0]. Here 0th character is taken from the string. example : ch=input(“Enter a char”) print(“U entered:”,+ch[0])
  • 12. A python program to accept a float number from keyboard. example : x=input(“Enter a number:”) print(‘U entered:’,x) output:
  • 13. From the above output,we can understand that the float values are displayed to an accurancy of 15 digits after decimal point. In the next program,we will accept two integer numbers and display them. A program to accept two integer numbers from keyboard. example : x=int(input(“Enter first number:”)) y=int(input(“Enter second number:”)) print(‘u entered:’,x,y)
  • 15. The two numbers in the output are displayed using a space. Suppose we want to display these numbers using a comma as seperator, then we can use sep=’ ,’ in the print() function as follows., In this case,the output will be.., Observe the commas after “U entered:’ and after 12.A better way to display these numbers seperating them using commas in this, print(‘u entered:’, x , y, sep=’ , ’); u entered: 12 , 2 print(‘U entered: %d, %d ‘ % (x,y))
  • 16. A Python program to convert numbers from other systems into decimal number system. example: str=input(‘Enter hexadecimal number:’) n=int(str, 16) print(‘Hexadecimal to Decimal=’ , n); str=input(‘Enter Octal number:’) n=int(str, 8) print(‘Octal to Decimal=’ , n); str=input(‘Enter binary number:’) n=int(str, 2) print(‘Binary to Decimal=’ , n);
  • 18. To accept more tha n one input in the same liine,we can use a for llop along with the input() function in the following format, In the previous statement, the input() function will display the message ‘Enter two numbers:’ to the user. When the user enters two values, they are accepted as strings. These strings are divided wherever a space is found by split() method. so, we get two strings as elements of a list.These strings are read by for loop and converted into integers by the int() function.These integers are finally stored into a and b. The split() method by default splits the values where a space is found.Hence while entering the numbers, the user should perate them using a space.The square brackets[ ] around the total expression indicates that the input is accepted as elements of a list a , b = [ int(x) for x in input (“Enter two numbers :”) , split () ]
  • 19. COMMAND LINE ARGUMENTS : We can design our programs in such a way that we can pass inputs t the program when we run command. For example, we write a program by the name ‘sum.py’ that takes numbers and sums them. We can supply the two numbers as input to the program at the time of running the program at command prompt as, Here,sum.py is our program name.While running this program,we are passing two arguments 12 and 20 t the program,which are called command line arguments.so command line arguments are the values to a python program at command prompt or system prompt.Command line arguments are passed to the program from outside the program.All the arguments should be entered from the keyboard seperating them by a space. c:> python sum.py 12 20
  • 20. These arguments are stored by default in the form of strings in a list in the name ‘argv’ which is available in sys module. Since argv is a list that contains all values passed to tha program,argv[0] represents the name of the program,argv[1] represents the first value,argv[2] represents the second value and so on..,, c:/> python sum.py 12 20 argv argv[0] argv[1] argv[2] command line args are stored as strings in argv list ‘sum.py’ 12 20
  • 21. In command line arguments, they are various ways of dealing with these types of arguments.The three most common are: using sys.argv using getopt module using argparse module Using sys.argv The sys module provides funnctions and variables used to manipulate different parts of the python runtime environment .This module provides access to some variables used or maintained by the interpreter and to functions that innteract strongly with the interpreter.
  • 22. we want to find the number of command line arguments,we can use the len() function as: len(argv).The following program reads the command line arguments entered at command prompt and displayss them. example: import sys n=len(sys.argv) args=sys.argv print(‘no. of comman line args=’,n) print(‘The args are: ,args) print(‘The args one by one:’) for a in args: print(a)
  • 24. using getopt module Python getopt module is similar to the getopt() function of C.Unlike sys module getopt module extends the seperation of the input string by parameter validation.it allows both short, and long options including a value assignment.However,this module requires the use of the sys module to process input data properly.To use getopt module,it is required to remove the first element from the list of command-line arguments. example: import sys import getopt def full_name(): first_name=None last_name=None argv=sys.argv[1:]
  • 25. try: opts,args=getopt.getopt(argv,”f:l:”) except: print(“Error”) for opt,arg in opts: if opt in [ ‘ -f ‘]: first_name = arg elif opt in [‘ -l ‘]: lats_name = arg print(first_name +” “ + last_name) full_name()
  • 27. Using argparse module Using argparse module is a better option than the above two options as it provides a lot of options such as positional arguments,default value for arguments,help message ,specifying data type of argument etc.., example import argparse parser=argparse.ArgumentParser() parser.add_argument("a") args=parser.parse_args()