SlideShare a Scribd company logo
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

PDF
Operators in python
PPTX
Vector class in C++
PDF
Lesson 02 python keywords and identifiers
PPTX
Conditional statement c++
PPT
Structure in C
PDF
software design principles
PPTX
Finite State Machine.ppt.pptx
PPTX
Evaluation of postfix expression
Operators in python
Vector class in C++
Lesson 02 python keywords and identifiers
Conditional statement c++
Structure in C
software design principles
Finite State Machine.ppt.pptx
Evaluation of postfix expression

What's hot (20)

PPTX
Pointer arithmetic in c
PPTX
Floating point representation
PDF
Pointers in C
PPTX
Presentation on c structures
PPTX
Multiplexer and DeMultiplexer
PPTX
Types of attributes (160210107054)
PPTX
Evaluation of postfix expression using stack
PDF
03. oop concepts
PPTX
Nested loop in C language
PPTX
PPTX
Programming in c Arrays
PPTX
List in Python
PPTX
Combinational circuits
PPTX
Presentation on pointer.
PPT
Sql operators & functions 3
PPTX
Operators in python
PDF
Arrays In C
PDF
3. ch 2-process model
PPT
structure and union
Pointer arithmetic in c
Floating point representation
Pointers in C
Presentation on c structures
Multiplexer and DeMultiplexer
Types of attributes (160210107054)
Evaluation of postfix expression using stack
03. oop concepts
Nested loop in C language
Programming in c Arrays
List in Python
Combinational circuits
Presentation on pointer.
Sql operators & functions 3
Operators in python
Arrays In C
3. ch 2-process model
structure and union
Ad

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

PDF
Python-02| Input, Output & Import
PDF
python-online&offline-training-in-kphb-hyderabad (1) (1).pdf
PPTX
IMP PPT- Python programming fundamentals.pptx
PDF
Python lecture 03
PPTX
PPTX
1-Introduction to Python, features of python, history of python(1).pptx
PPTX
python user input slides FOundation program
PPTX
Python basics
PPTX
20BCT23 – PYTHON PROGRAMMING.pptx
PPTX
BASICS OF PYTHON usefull for the student who would like to learn on their own
PPT
Python Programming Introduction demo.ppt
PPTX
Chapter 1 Python Revision (1).pptx the royal ac acemy
PPTX
Python_Modullllllle_2-_AFV._Funda-1.pptx
PPTX
Python
PPTX
#Code2Create: Python Basics
PPTX
Python basics
PPTX
Python basics
PPTX
Python basics
PPTX
Python basics
PPTX
Python basics
Python-02| Input, Output & Import
python-online&offline-training-in-kphb-hyderabad (1) (1).pdf
IMP PPT- Python programming fundamentals.pptx
Python lecture 03
1-Introduction to Python, features of python, history of python(1).pptx
python user input slides FOundation program
Python basics
20BCT23 – PYTHON PROGRAMMING.pptx
BASICS OF PYTHON usefull for the student who would like to learn on their own
Python Programming Introduction demo.ppt
Chapter 1 Python Revision (1).pptx the royal ac acemy
Python_Modullllllle_2-_AFV._Funda-1.pptx
Python
#Code2Create: Python Basics
Python basics
Python basics
Python basics
Python basics
Python basics
Ad

More from Kavitha713564 (16)

PPTX
Python Virtual Machine concept- N.Kavitha.pptx
PPTX
Operators Concept in Python-N.Kavitha.pptx
PPTX
THE PACKAGES CONCEPT IN JAVA PROGRAMMING.pptx
PPTX
The Java Server Page in Java Concept.pptx
PPTX
Programming in python in detail concept .pptx
PPTX
The Datatypes Concept in Core Python.pptx
PPTX
Packages in java
PPTX
Interface in java
PPTX
Exception handling in java
PPTX
Multithreading in java
DOCX
Methods in Java
PPTX
Applet in java new
PPTX
Basic of java
PPTX
Multithreading in java
PPTX
Input output files in java
PPTX
Arrays,string and vector
Python Virtual Machine concept- N.Kavitha.pptx
Operators Concept in Python-N.Kavitha.pptx
THE PACKAGES CONCEPT IN JAVA PROGRAMMING.pptx
The Java Server Page in Java Concept.pptx
Programming in python in detail concept .pptx
The Datatypes Concept in Core Python.pptx
Packages in java
Interface in java
Exception handling in java
Multithreading in java
Methods in Java
Applet in java new
Basic of java
Multithreading in java
Input output files in java
Arrays,string and vector

Recently uploaded (20)

PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Pharma ospi slides which help in ospi learning
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Cell Structure & Organelles in detailed.
PDF
Basic Mud Logging Guide for educational purpose
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Computing-Curriculum for Schools in Ghana
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PDF
01-Introduction-to-Information-Management.pdf
PPTX
master seminar digital applications in india
PDF
RMMM.pdf make it easy to upload and study
Module 4: Burden of Disease Tutorial Slides S2 2025
Anesthesia in Laparoscopic Surgery in India
Insiders guide to clinical Medicine.pdf
Pharma ospi slides which help in ospi learning
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Final Presentation General Medicine 03-08-2024.pptx
102 student loan defaulters named and shamed – Is someone you know on the list?
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Cell Structure & Organelles in detailed.
Basic Mud Logging Guide for educational purpose
STATICS OF THE RIGID BODIES Hibbelers.pdf
Computing-Curriculum for Schools in Ghana
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
O7-L3 Supply Chain Operations - ICLT Program
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
GDM (1) (1).pptx small presentation for students
01-Introduction-to-Information-Management.pdf
master seminar digital applications in india
RMMM.pdf make it easy to upload and study

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()