0% found this document useful (0 votes)
385 views11 pages

Python Input/Output Guide

The document provides information on different ways to take input and provide output in Python. It discusses the input() and raw_input() functions for taking user input, and the print() function for displaying output. It also covers topics like command line arguments, formatted string output and multiple values as input.

Uploaded by

Rajendra Buchade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
385 views11 pages

Python Input/Output Guide

The document provides information on different ways to take input and provide output in Python. It discusses the input() and raw_input() functions for taking user input, and the print() function for displaying output. It also covers topics like command line arguments, formatted string output and multiple values as input.

Uploaded by

Rajendra Buchade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Learn

Complete
Python
In
Simple Way

1 https://www.youtube.com/durgasoftware
INPUT
AND
OUTPUT
STATEMENTS
STUDY MATERIAL

2 https://www.youtube.com/durgasoftware
Reading Dynamic Input from the Keyboard:
In Python 2 the following 2 functions are available to read dynamic input from the
keyboard.

1) raw_input()
2) input()

1) raw_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.

Eg: x = raw_input("Enter First Number:")


print(type(x))  It will always print str type only for any input type

2) input():
input() function can be used to read data directly in our required format.We are not
required to perform type casting.

x = input("Enter Value)
type(x)

10  int
"durga" str
10.5  float
True  bool

***Note:
 But in Python 3 we have only input() method and raw_input() method is not available.
 Python3 input() function behaviour exactly same as raw_input() method of Python2.
i.e every input value is treated as str type only.
 raw_input() function of Python 2 is renamed as input() function in Python 3.

1) >>> type(input("Enter value:"))


2) Enter value:10
3) <class 'str'>
4)
5) Enter value:10.5
6) <class 'str'>
7)
8) Enter value:True
9) <class 'str'>

3 https://www.youtube.com/durgasoftware
Q) Write a program to read 2 numbers from the keyboard and print sum

1) x=input("Enter First Number:")


2) y=input("Enter Second Number:")
3) i = int(x)
4) j = int(y)
5) print("The Sum:",i+j)

Enter First Number: 100


Enter Second Number: 200
The Sum: 300
-----------------------------------------------------

1) x=int(input("Enter First Number:"))


2) y=int(input("Enter Second Number:"))
3) print("The Sum:",x+y)

-----------------------------------------------------------

print("The Sum:",int(input("Enter First Number:"))+int(input("Enter Second Number:")))

Q) Write a Program to read Employee Data from the Keyboard and


print that Data
1) eno=int(input("Enter Employee No:"))
2) ename=input("Enter Employee Name:")
3) esal=float(input("Enter Employee Salary:"))
4) eaddr=input("Enter Employee Address:")
5) married=bool(input("Employee Married ?[True|False]:"))
6) print("Please Confirm Information")
7) print("Employee No :",eno)
8) print("Employee Name :",ename)
9) print("Employee Salary :",esal)
10) print("Employee Address :",eaddr)
11) print("Employee Married ? :",married)

D:\Python_classes>py test.py
Enter Employee No:100
Enter Employee Name:Sunny
Enter Employee Salary:1000
Enter Employee Address:Mumbai
Employee Married ?[True|False]:True
Please Confirm Information

4 https://www.youtube.com/durgasoftware
Employee No : 100
Employee Name : Sunny
Employee Salary : 1000.0
Employee Address : Mumbai
Employee Married ? : True

How to read multiple values from the keyboard in a


single line:
1) a,b= [int(x) for x in input("Enter 2 numbers :").split()]
2) print("Product is :", a*b)

D:\Python_classes>py test.py
Enter 2 numbers :10 20
Product is : 200

Note: split() function can take space as seperator by default .But we can pass
anything as seperator.

Q) Write a program to read 3 float numbers from the keyboard


with, seperator and print their sum
1) a,b,c= [float(x) for x in input("Enter 3 float numbers :").split(',')]
2) print("The Sum is :", a+b+c)

D:\Python_classes>py test.py
Enter 3 float numbers :10.5,20.6,20.1
The Sum is : 51.2

eval():
eval Function take a String and evaluate the Result.

Eg: x = eval(“10+20+30”)
print(x)
Output: 60

Eg: x = eval(input(“Enter Expression”))


Enter Expression: 10+2*3/4
Output: 11.5
eval() can evaluate the Input to list, tuple, set, etc based the provided Input.

5 https://www.youtube.com/durgasoftware
Eg: Write a Program to accept list from the keynboard on the display

1) l = eval(input(“Enter List”))
2) print (type(l))
3) print(l)

COMMAND LINE ARGUMENTS


 argv is not Array it is a List. It is available sys Module.
 The Argument which are passing at the time of execution are called Command Line
Arguments.

Eg: D:\Python_classes py test.py 10 20 30

Command Line Arguments

Within the Python Program this Command Line Arguments are available in argv. Which is
present in SYS Module.

test.py 10 20 30

Note: argv[0] represents Name of Program. But not first Command Line Argument.
argv[1] represent First Command Line Argument.

Program: To check type of argv from sys

import argv
print(type(argv))

D:\Python_classes\py test.py

Write a Program to display Command Line Arguments


1) from sys import argv
2) print(“The Number of Command Line Arguments:”, len(argv))
3) print(“The List of Command Line Arguments:”, argv)
4) print(“Command Line Arguments one by one:”)
5) for x in argv:
6) print(x)

6 https://www.youtube.com/durgasoftware
D:\Python_classes>py test.py 10 20 30
The Number of Command Line Arguments: 4
The List of Command Line Arguments: [‘test.py’, ‘10’,’20’,’30’]
Command Line Arguments one by one:
test.py
10
20
30
---------------------------
1) from sys import argv
2) sum=0
3) args=argv[1:]
4) for x in args :
5) n=int(x)
6) sum=sum+n
7) print("The Sum:",sum)

D:\Python_classes>py test.py 10 20 30 40
The Sum: 100

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

1) from sys import argv


2) print(argv[1])

D:\Python_classes>py test.py Sunny Leone


Sunny

D:\Python_classes>py test.py 'Sunny Leone'


'Sunny

D:\Python_classes>py test.py "Sunny Leone"


Sunny Leone

Note 2: 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.

1) from sys import argv


2) print(argv[1]+argv[2])
3) print(int(argv[1])+int(argv[2]))

7 https://www.youtube.com/durgasoftware
D:\Python_classes>py test.py 10 20
1020
30

Note 3: If we are trying to access command line arguments with out of range index then
we will get Error.

1) from sys import argv


2) print(argv[100])

D:\Python_classes>py test.py 10 20
IndexError: list index out of range

Note: In Python there is argparse module to parse command line arguments and display
some help messages whenever end user enters wrong input.

input()
raw_input()

Command Line Arguments

Output Statements:
We can use print() function to display output.

Form-1: print() without any argument


Just it prints new line character

Form-2:

1) print(String):
2) print("Hello World")
3) We can use escape characters also
4) print("Hello \n World")
5) print("Hello\tWorld")
6) We can use repetetion operator (*) in the string
7) print(10*"Hello")
8) print("Hello"*10)
9) We can use + operator also
10) print("Hello"+"World")

8 https://www.youtube.com/durgasoftware
Note:
֍ If both arguments are String type then + operator acts as concatenation operator.
֍ If one argument is string type and second is any other type like int then we will get
Error.
֍ If both arguments are number type then + operator acts as arithmetic addition
operator.

Note:

1) print("Hello"+"World")
2) print("Hello","World")

HelloWorld
Hello World

Form-3: print() with variable number of arguments

1) a,b,c=10,20,30
2) print("The Values are :",a,b,c)

Output: The Values are : 10 20 30

By default output values are seperated by space.If we want we can specify seperator by
using "sep" attribute

1) a,b,c=10,20,30
2) print(a,b,c,sep=',')
3) print(a,b,c,sep=':')

D:\Python_classes>py test.py
10,20,30
10:20:30

Form-4:print() with end attribute

1) print("Hello")
2) print("Durga")
3) print("Soft")

Output:
Hello
Durga
Soft

9 https://www.youtube.com/durgasoftware
If we want output in the same line with space

1) print("Hello",end=' ')
2) print("Durga",end=' ')
3) print("Soft")

Output: Hello Durga Soft

Note: The default value for end attribute is \n, which is nothing but new line character.

Form-5: print(object) statement

We can pass any object (like list, tuple, set etc) as argument to the print() statement.

1) l=[10,20,30,40]
2) t=(10,20,30,40)
3) print(l)
4) print(t)

Form-6: print(String, variable list)

We can use print() statement with String and any number of arguments.

1) s = "Durga"
2) a = 48
3) s1 ="Java"
4) s2 ="Python"
5) print("Hello",s,"Your Age is",a)
6) print("You are teaching",s1,"and",s2)

Output:
Hello Durga Your Age is 48
You are teaching java and Python

Form-7: print (formatted string)

1) %i  int
2) %d  int
3) %f  float
4) %s  String type

Syntax: print("formatted string" %(variable list))

10 https://www.youtube.com/durgasoftware
Eg 1:

1) a=10
2) b=20
3) c=30
4) print("a value is %i" %a)
5) print("b value is %d and c value is %d" %(b,c))

Output
a value is 10
b value is 20 and c value is 30

Eg 2:

1) s="Durga"
2) list=[10,20,30,40]
3) print("Hello %s ...The List of Items are %s" %(s,list))

Output: Hello Durga ...The List of Items are [10, 20, 30, 40]

Form-8: print() with replacement operator {}

Eg:

1) name = "Durga"
2) salary = 10000
3) gf = "Sunny"
4) print("Hello {0} your salary is {1} and Your Friend {2} is waiting".
format(name,salary,gf))
5) print("Hello {x} your salary is {y} and Your Friend {z} is waiting".
format(x=name,y=salary,z=gf))

Output
Hello Durga your salary is 10000 and Your Friend Sunny is waiting
Hello Durga your salary is 10000 and Your Friend Sunny is waiting

11 https://www.youtube.com/durgasoftware

You might also like