Input and Output Operations with Examples in Python

Learn: How to perform input and output operations in python using suitable methods/functions with Examples in Python?
Submitted by IncludeHelp, on December 26, 2019

A Program needs to interact with the user to accomplish the desired task; this is done using Input-Output facility. Input means the data entered by the user of the program. In python, we have input() and raw_input ( ) function available for Input.

1) input() function

Syntax:

    input (expression)

If prompt is present, it is displayed on monitor, after which the user can provide data from keyboard. Input takes whatever is typed from the keyboard and evaluates it. As the input provided is evaluated, it expects valid python expression. If the input provided is not correct then either syntax error or exception is raised by python.

Example 1:

# python input operations

# user input 
x = input("Enter any value: ")

# printing value
print("Entered value is: ", x)

Output

RUN 1:
Enter any value: 12345
Entered value is:  12345

RUN 2:
Enter any value: IncludeHelp
Entered value is:  IncludeHelp

RUN 3:
Enter any value: Python is a progamming language.
Entered value is:  Python is a progamming language.

Example 2:

# python input operations

# just provide a value and entered value prints
print(input())

# provide another value
x = input()
print("Your input is: ", x)

# prompting message for input
val1 = input("Enter a value: ")
val2 = input("Enter another value: ")
val3 = input("Enter another value: ")

# printing values
print("val1 =", val1)
print("val2 =", val2)
print("val3 =", val3)

Output

Hello
Hello
I'm Shivang!
Your input is:  I'm Shivang!
Enter a value: 100
Enter another value: 23.45
Enter another value: Helllooooooo
val1 = 100
val2 = 23.45
val3 = Helllooooooo

2) raw_input() function

This input method fairly works in older versions (like 2.x).

Syntax:

    raw_input (expression)

If prompt is present, it is displayed on the monitor after which user can provide the data from keyboard. The function takes exactly what is typed from keyboard, convert it to string and then return it to the variable on LHS of '='.

Example: In interactive mode

>>>x=raw_input ('Enter your name: ')

Enter your name: ABC

x is a variable which will get the string (ABC), typed by user during the execution of program. Typing of data for the raw_input function is terminated by enter key.

We can use raw_input() to enter numeric data also. In that case we typecast, i.e., change the data type using function, the string data accepted from user to appropriate Numeric type.

Example:

>>>y=int(raw_input("Enter your roll no."))

Enter your roll no. 5

It will convert the accepted string i.e., 5 to integer before assigning it to 'y'.

print() function/statement

print evaluates the expression before printing it on the monitor. Print statement outputs an entire (complete) line and then goes to next line for subsequent output (s). To print more than one item on a single line, comma (,) may be used.

Syntax:

    print (expression/constant/variable)

Example 1:

# print() example in Python

# using single quotes
print('Hello!')
print('How are you?')

# using double quotes
print("Hello!")
print("How are you?")

# using triple single quotes
# those can be used to print multiple line string
print('''Hello!''')
print('''How are you?''')

# printing multiline string
print('''Hello... how are you?
Hey! I am good, what about you?
I am good also, thanks.''')

Output

Hello!
How are you?
Hello!
How are you?
Hello!
How are you?
Hello... how are you?
Hey! I am good, what about you?
I am good also, thanks.

Example 2:

# print() example in Python

# printing values
print("Printing direct values...")
print(10) # printing an integer 
print(10.2345) # printing a float
print([10, 20, 30, 40, 50]) # printing a list
print({10, 20, 30, 40, 50}) # printing a set

# printing variables
a = 10 
b = 10.2345
c = [10, 20, 30, 40, 50]
d = {10, 20, 30, 40, 50}

print("Printing variables...")
print(a)
print(b)
print(c)
print(d)

# printing message with variables
print("Printing message variables...")
print("a = ", a)
print("b = ", b)
print("c = ", c)
print("d = ", d)

Output

Printing direct values...
10
10.2345
[10, 20, 30, 40, 50]
{40, 10, 50, 20, 30}
Printing variables...
10
10.2345
[10, 20, 30, 40, 50]
{40, 10, 50, 20, 30}
Printing message variables...
a =  10
b =  10.2345
c =  [10, 20, 30, 40, 50]
d =  {40, 10, 50, 20, 30}





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.