Python Input and Output
The function input( ) allows us to take input at run time interactively from user and store this value in a variable or object.
Input in Python (Reading String / Value)
Most of the time in our program, we want to input data / value at run time, i.e. we want to make our program user interactive.
In Python 3.x, we can use built-in function input( ). The function input( ) allows us to take input at run time interactively from user and store this value in a variable or object.
Syntax :
Variable or Object = input(“Prompt Message to be displayed”)
Example
Name = input(“Enter your name :: “)
CityName = input(“Enter your city name :: “)
When you type this above code in Python Shell, you will get a prompt message and waiting cursor for your response. Whatever value entered by you, that will be assigned to the variable written before assignment operator like Name and CityName.
Note : input( ) function always returns a value of String type.
Please look carefully on the code given in sample and its output.
#Que : Write a program in Python to add two numbers.
num1 = input (“Enter Number 1 : “)
num2 = input (“Enter Number 2 : “)
sum = num1 + num2
print(“Sum of “, num1 , “and”, num2, “is”, sum)
#Output : : ?????
Please try this program before watching video further…..
Okay! Lets see,
Output :
Sum of 20 and 25 is 2025
Why? 2025
Why not 45.
Let’s see some more interesting example –
x = input(“Enter No “)
Enter No : 25
>>> type(x)
type <str>
Ite means that input method always return a string value, either you entered number i.e. digits.
So when you input two numbers in num1 and num2, both contain string values ’20’ and ’25’ respectively.
and
sum = num1 + num2
is
sum = ’20’ + ’25’
so it concatenate not add, and it gives ‘2025’
How to resolve these problems?
Type Casting/ Data Conversion Method in Python –
Python provide us a rich set of type casting / data conversion method, which convert any string to specified numeric, and other data types.
int ( ) – convert any number convertible type of value to integer, i.e. string, which containing digit, to integer, float to integer and bool value to integer. Be careful, while converting string to integer, it must contain only digits, otherwise raise a ValueError message.
val = input(“enter no”)
x = int(val)
or
x = int(input(“enter no”))
float ( ) – convert any type of value to fractional number, i.e. string to float, integer to float and bool value to float.
v = float(‘2569.39’) # convert ‘2569.39’ into 2569.39
complex( ) – convert any type of value to complex number, i .e. string to complex, integer to complex, float to complex and bool value to complex.
bool( ) – convert any type of value to Boolean value, i.e. string to Boolean, integer to Boolean, Float to Boolean.
str( ) – convert any type of values to String value, i.e. number to string, Boolean value to string value, complex to Boolean, etc.
Using of Type Conversion Method with input( ) statement
num1 = int( input (“Enter Number 1 : “))
num2 = int (input (“Enter Number 2 : “) )
sum = num1 + num2
print(“Sum of “, num1 , “and”, num2, “is”, sum)
Output:
Enter Number 1 : 20
Enter Number 2 : 25
Sum of 20 and 25 is 45.
Output in Python
Python uses the print() function to output data to standard output device — the screen. The function print() evaluates the
expression before displaying it on the screen. The print() outputs a complete line and then moves to the next line for subsequent output.
The syntax for print() is:
print(value [, …, sep = ‘ ‘, end = ‘\n’])
- sep: The optional parameter sep is a separator between the output values. We can use a character, integer or a string as a separator. The default separator is space.
- end: This is also optional and it allows us to specify any string to be appended after the last value. The default is a new line.
>>> print(“Hello”)
Hello
>>> print(10*2.5)
25.0
>>> print(“I” + “love” + “my” + “country”)
Ilovemycountry
Note : + operator concatenates the string.
>>>print(“I’m”, 16, “years old”)
I’m 16 years old
Note : , (comma) separate the value and put space in between different values.
>>> x = 20
>>> y = 30
>>> print(“X “, x, ‘\nY ‘, y)
X 20
Y 30
Class 11 Computer Science
- Computer System: Overview – Notes
- Number Systems & Encoding Scheme – Notes
- Boolean Logics – Notes
- Emerging Trends – Notes
- Introduction to Problem Solving – Notes
- Getting Started with Python – Notes
- Python Fundamentals – Notes
- Data Handling – Notes
- Flow of Control (Selection Statement, Iterative Statement and Jump Statement) – Notes
- Strings Manipulation in Python
- List Manipulation – Notes
- Tuples Manipulation in Python Notes
- Dictionary Handling in Python – Notes
- Sorting Techniques in Python – Notes
- Introduction to Python Module – Notes
- Society, Law, and Ethics: Societal Impacts – Notes
Informatics Practices
- CBSE Board Class 12 Answer Key Informatics Practices 065 Term 2 Examination
- Class 11 Informatics Practices Chapter 8 Introduction to Structured Query Language SQL NCERT Exercise Solution
- Class 11 Informatics Practices Chapter 7 Database Concepts NCERT Exercise Solution
- Informatics Practices Answer Key Class 12 CBSE Term 1 Examination 2021 Download pdf
- Class 12 Informatics Practices 065 CBSE Sample Paper 10 years download now
- Class 11 Informatics Practices Chapter 4 Working with Lists and Dictionaries NCERT Solution
- Class 11 Informatics Practices Chapter 3 Brief Overview of Python NCERT Solution
- Input Output in Python
- Python Data Types
- Python 3 Setup and Installation Guide
- Python Important Programs and Practical’s
- CBSE Annual Exam Question Paper for Class 11 Informatics Practices Sample Papers
- Class 11 Informatics Practices Ch 3 Brief Overview of Python NOTES with WORKSHEET
- 110+ Important Networking Full Form List – Download PDF
- Class 11 Informatics Practices – Ch. 2 Emerging Trends NCERT Solutions
- Class 11 Informatics Practices – Notes
- Class 12 Informatics Practices- Ch.1 Querying & SQL Function – NCERT Solution
- Class 12 Informatics Practices NCERT Exercise Solution
- Class 11 Informatics Practices – Ch 1. Computer System NCERT Solution
- Computer System: Overview – Notes