In this python tutorial, the following types of input arguments in python are covered (python input statement). They are
- How to use command line in Python
- How to use run time arguments in python (how to take runtime input in python)
- How to use fixed arguments in python.
PYTHON INPUT ARGUMENTS
- Like c#/java, Python supports three different types of input statements
TYPES
- Assignment inputs (static inputs)
- Command Line inputs
- Dynamic / Runtime inputs
1. Assignment Inputs (Static Inputs)
- Assigning the value to the variable with help of equal operator is called as assignment inputs
- We can directly assign the value to variable using equal operator (=)
- Fixed values
I. EXAMPLE OF ASSIGNMENT INPUTS
1. SOURCE CODE
# define variables
name=”Shiva”
id=1
def disp():
print(“Name \t: “,name)
print(“Id \t: “,id)
print(“—————————————-“)
print(“\tAssignment Inputs”)
print(“—————————————-“)
# calling function using its name
disp()
2. OUTPUT
2. Command Line Inputs (Command Line Arguments)
- Providing necessary input values (arguments) to the script at the time of program execution
- The python module sys stores the command line arguments into a list. We can access it using sys.argv
- By default, the command line arguments are list type in python.
II. EXAMPLE OF COMMAND LINE INPUTS
1. SOURCE CODE
# import sys module: to support command line arguments
import sys
print(“—————————————-“)
print(“\tCommand Line Inputs”)
print(“—————————————-“)
# get the total number of arguments
c=len(sys.argv)
# get 1st argument from list and convert it to integer using int() function
a=int(sys.argv[1])
# get 2nd argument from list and convert it to integer using int() function
b=int(sys.argv[2])
k=a+b
print(“Total number of arguments\t: “,c)
print(“Total Arguments\t\t\t: “,sys.argv)
print(“Add of two numbers\t\t: “,k)
2. OUTPUT
3. Runtime Inputs (Dynamic Inputs)
- Process of assigning the values to the variables at the time of program execution
- In python, the dynamic inputs are handled using the two built-in methods such as raw_input(), input()
input()
- It is a built-in function of python 3
- It is used to read a text inputs from standard input device(e.g. keyboard)
- It takes one argument. The argument is optional.
- Return type: string
raw_input()
- It is a built-in function of python 2
- It is used to read a line of text from standard input device(e.g. keyboard)
- It is similar to gets() in c language
- Return type: string
TYPE CASTING
- Process of converting one type to another type that is called as type casting
- In python, any base type can be converted to another base type using type() function. Here the type can be: int, float, char, string, etc,… based on the target type.
III. EXAMPLE OF RUNTIME INPUTS
1. SOURCE CODE
# define function
def disp():
# read a number (string format)
a=input(“Enter the 1st number: “)
# convert string number to int number
a=int(a)
# read another number (string format)
b=input(“Enter the 2nd number: “)
# convert string number to int number
b=int(b)
c=a+b
print(“Add : “,c)
print(“—————————————-“)
print(“\tRuntime Inputs”)
print(“—————————————-“)
# calling function using its name
disp()
2. OUTPUT
IV. EXAMPLE OF RUNTIME INPUTS
1. SOURCE CODE
# define function
def disp():
# read a name as string
name=input(“Enter person name \t: “)
# read id and convert it to int using type()
id=int(input(“Enter id number \t: “))
# read salary and convert it float type using type()
pack=float(input(“Enter the salary \t: “))
# read place as string
place=input(“Enter the location \t: “)
print(“—————————————-“)
# calling function
show(name,id,pack,place)
# define function
def show(name,id,pack,place):
print(“Name \t: “,name)
print(“Id \t: “,id)
print(“Salary \t: “,pack)
print(“Place \t: “,place)
print(“—————————————-“)
print(“\tRuntime Inputs”)
print(“—————————————-“)
# calling function using its name
disp()
2. OUTPUT