English
Tamil Nadu Board of Secondary EducationHSC Science Class 12

Explain input() and print() functions with examples.

Advertisements
Advertisements

Question

Explain input() and print() functions with examples.

Answer in Brief
Advertisements

Solution

input () function:

In Python input() function is used to accept data as input at run time.
Syntax:
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.
  • The input() takes whatever is typed from the keyboard and stores the entered data in the given variable.
  • If the 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:
> > > city=input(“Enter Your City: “)
Enter Your City: Madurai
> > > print(“I am from “, city)
I am from Madurai

  • The input () accepts all data as strings of characters but not as numbers.
  • If a numerical value is entered, the input values should be explicitly converted into numeric data types.
  • The int( ) function is used to convert string data as integer data explicitly.

Example:

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

print () function :

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

Syntax:
print (“string to be displayed as output” )
print (variable)
print (“String to be displayed as output variable)
print (“String 1 “, 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 expressions before printing them on the monitor
  • The print( ) displays an entire statement specified within print ( )
  • The comma (,) is used as a separator in print more than one time.
shaalaa.com
  Is there an error in this question or solution?
Chapter 5: Python - Variables and Operators - Evaluation [Page 66]

APPEARS IN

Samacheer Kalvi Computer Science [English] Class 12 TN Board
Chapter 5 Python - Variables and Operators
Evaluation | Q 2. | Page 66
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×