Home | | Computer Science 12th Std | Input and Output Functions

Python - Input and Output Functions | 12th Computer Science : Chapter 5 : Core Python : Python Variables and Operators

Chapter: 12th Computer Science : Chapter 5 : Core Python : Python Variables and Operators

Input and Output Functions

A program needs to interact with the user to accomplish the desired task; this can be achieved using Input-Output functions.

Input and Output Functions

A program needs to interact with the user to accomplish the desired task; this can be achieved using Input-Output functions. The input() function helps to enter data at run time by the user and the output function print() is used to display the result of the program on the screen after execution.

 

1. The print() function

In Python, the print() function is used to display result on the screen. The syntax for print() is as follows:

Example

print (“string to be displayed as output ” )

print (variable )

print (“String to be displayed as output ”, variable)

print (“String1 ”, variable, “String 2”, variable, “String 3” ……)

Example

>>>print (“Welcome to Python Programming”) Welcome to Python Programming

>>>x = 5

>>>y = 6

>>>z = x + y

>>>print (z)

11

>>> print (“The sum = ”, z)

The sum = 11

>>> print (“The sum of ”, x, “ and ”, y, “ is ”, z)

The sum of 5 and 6 is 11

The print ( ) evaluates the expression before printing it on the monitor. The print () displays an entire statement which is specified within print ( ). Comma ( , ) is used as a separator in print ( ) to print more than one item.

 

2. input() function

In Python, input( ) function is used to accept data as input at run time. The syntax for input() function is,

Variable = input (“prompt string”)

Where, prompt string in the syntax is a statement or message to the user, to know what input can be given.

If a prompt string is used, it is displayed on the monitor; the user can provide expected data from the input device. The input( ) takes whatever is typed from the keyboard and stores the entered data in the given variable. If prompt string is not given in input( ) no message is displayed on the screen, thus, the user will not know what is to be typed as input.

Example 1:input( ) with prompt string

>>>city=input (“Enter Your City: ”)

Enter Your City:Madurai

>>>print (“I am from “, city)

I am from Madurai

Example 2:input( ) without prompt string

>>> city=input()

Rajarajan

>>> print (I am from", city)

I am from Rajarajan

Note that in example-2, the input( ) is not having any prompt string, thus the user will not know what is to be typed as input. If the user inputs irrelevant data as given in the above example, then the output will be unexpected. So, to make your program more interactive, provide prompt string with input( ).

The input ( ) accepts all data as string or characters but not as numbers. If a numerical value is entered, the input values should be explicitly converted into numeric data type. The int( ) function is used to convert string data as integer data explicitly. We will learn about more such functions in later chapters.

Example 3:

x = int (input(“Enter Number 1: ”))

y = int (input(“Enter Number 2: ”))

print (“The sum = ”, x+y)

Output:

Enter Number 1: 34

Enter Number 2: 56

The sum = 90

Example 4: Alternate method for the above program

x,y=int (input("Enter Number 1 :")),int(input("Enter Number 2:"))

print ("X = ",x," Y = ",y)

Output:

Enter Number 1 :30

Enter Number 2:50

X = 30 Y = 50

 

Tags : Python , 12th Computer Science : Chapter 5 : Core Python : Python Variables and Operators
Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
12th Computer Science : Chapter 5 : Core Python : Python Variables and Operators : Input and Output Functions | Python


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.