IKH

Input Statements

In Python, input statements are used to interact with the user by allowing them to enter data.

Dynamic Input

In Python3 the following function is available to read dynamic input from the keyboard:

  • input()

input()

This function always reads the data from the keyboard in the form of string format. We have to convert that string type to our required type by using the corresponding type casting methods.

Example

Python
x=input("Enter First Number:")
print(type(x))

Output

PowerShell
Enter First Number:12
<class 'str'>

Question:

Write a program to read 2 numbers from the keyboard and print sum.

Python
 num1=int(input("Enter First Number:"))
 num2=int(input("Enter Second Number:"))
 print("The Sum:",num1+num2)

Output

PowerShell
Enter First Number:10
Enter Second Number:20
The Sum: 30

Question:

Write a program to read employee data from the keyboard and print that data.

Python
eno=int(input("Enter Employee No:")) 
ename=input("Enter Employee Name:") 
esal=float(input("Enter Employee Salary:")) 
eaddr=input("Enter Employee Address:") 
married=bool(input("Employee Married ?[True|False]:")) 
print("Please Confirm Information") 
print("Employee No :",eno) 
print("Employee Name :",ename) 
print("Employee Salary :",esal) 
print("Employee Address :",eaddr) 
print("Employee Married ? :",married) 

Output

PowerShell
Enter Employee No:100
Enter Employee Name:Ajay
Enter Employee Salary:1000
Enter Employee Address:Noida
Employee Married ?[True|False]:True
Please Confirm Information
Employee No : 100
Employee Name : Ajay
Employee Salary : 1000.0
Employee Address : Noida
Employee Married ? : True

Read multiple values from the keyboard in a single line

Python
num1, num2= [int(x) for x in input("Enter 2 numbers :").split()]
print("Product is :", num1*num2)

Output

PowerShell
Enter 2 numbers :10 4
Product is : 40

Remark

  • split() function can take space as separator by default. But we can pass anything as separator.

Question

Write a program to read 3 float numbers from the keyboard with ‘,’ separator and print their sum.

Python
a,b,c= [float(x) for x in input("Enter 3 float numbers :").split(',')]
print("The Sum is :", a+b+c)

Output

PowerShell
Enter 3 float numbers :10.1,2.8,9.1
The Sum is : 22.0

eval()

eval function take a string and evaluate the result.

Example

Python
x = eval("10+20+30")
print(x)

Output

PowerShell
60

Example

Python
x = eval(input(“Enter Expression”)) 

Output

PowerShell
Enter Expression: 10+23/4
11.5
  • eval() can evaluate the Input to list, tuple, set … etc. based on the provided Input.

Question

Write a Program to accept list from the keyboard and print.

Python
l = eval(input("Enter List: "))
print(type(l))

Output

PowerShell
Enter List: [1,2,3,4]
<class 'list'>

Command Line Arguments

  • The Argument which are passing at the time of execution are called command line arguments.
  • sys.argv is used for command line arguments, where sys is one of the python module.
  • argv[0] represents name of the program, but not first command line argument.
  • argv[1] represent first command line argument.

Question

Check type of argv from sys.

Python
import sys
print(type(sys.argv))

Output

PowerShell
<class 'list'>

Question

Write a program to display command line arguments.

Python
from sys import argv
print("The Number of Command Line Arguments:", len(argv))
print("The List of Command Line Arguments:", argv)
print("Command Line Arguments one by one:")
for x in argv:
    print(x)
  • Save the file with name test.py and run it with command “python test.py 6 3 9”

Output

PowerShell
The Number of Command Line Arguments: 4
The List of Command Line Arguments: ['test.py', '6','3','9']
Command Line Arguments one by one:
test.py
6
3
9

Question

Write a program to get sum of all command line arguments value.

Python
from sys import argv
sum=0
args=argv[1:]
for x in args :
    n=int(x)
    sum=sum+n
print("The Sum:",sum)
  • Save the file with name test.py and run it with command “python test.py 6 3 9”

Output

PowerShell
18

Remark

  • Usually space is separator between command line arguments. If our command line argument itself contains space then we should enclose within double quotes (but not single quotes).

Example

Python
from sys import argv
print(argv[1])
  • Save the file with name test.py and run it with command ‘python test.py Ajay Shukla’

Output

PowerShell
Ajay

Remark

  • Within the python program command line arguments are available in the string form. Based on our requirement, we can convert into corresponding type by using type casting methods.

Ungraded Questions

Get ready for an exhilarating evaluation of your understanding! Brace yourself as we dive into the upcoming assessment. Your active participation is key, so make sure to attend and demonstrate your knowledge. Let’s embark on this exciting learning journey together!


Name
Email
Phone

Report an error