0% found this document useful (0 votes)
2 views

9. Input Output in Python

Chapter 9 covers the input() and print() functions in Python. The input() function prompts users for data and treats all input as strings, while the print() function outputs values and messages with customizable formatting options. The chapter also explains how to convert input to integers and floats, and demonstrates how to print multiple values with different separators and formatting.

Uploaded by

somakushwahsoma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

9. Input Output in Python

Chapter 9 covers the input() and print() functions in Python. The input() function prompts users for data and treats all input as strings, while the print() function outputs values and messages with customizable formatting options. The chapter also explains how to convert input to integers and floats, and demonstrates how to print multiple values with different separators and formatting.

Uploaded by

somakushwahsoma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.

com

Chapter- 9: Input/output in Python

"Some People Around You Are Just Like the


Semicolon in Python, You Have to Debug the Code
and Omit them From Your Life……….. "
You will learn the following topics in this tutorial

S.No. Topics

1 The input ( ) function

2 The print ( ) function

Page 1 of 8
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com

Chapter- 9: Input/output in Python

The input ( ) function


 The input ( ) function prompts the user to enter data.
 It accepts all user input as string.
 The user may enter a number or a string but the input ( ) function
treats them as strings only.
The syntax for input ( ) is:

input ([Prompt])
 Prompt is the string we may like to display on the screen prior to taking
the input, and it is optional.
For example,

The value that you type in front of


displayed prompt will be assigned to given
variables name and marks respectively.

Page 2 of 8
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com

Chapter- 9: Input/output in Python

Inputting Numeric Values


Suppose we want to find sum of two numbers and the program is:

When we execute the above program, the program doesn’t add the two
numbers together; it just puts one right after the other as 1020

The solution to the above problem is:

Python offers two functions int ( ) and float ( ) to be used with input ( ) to
convert the values received through input ( ) into int and float types.

Accept an Integer input from User


We need to convert an input string value into an integer using a int( )
function.
num1 = int (input("Enter first number: ")) Both input values are
num2 = int(input("Enter second number: ")) converted into integer type
sum = num1 + num2
print ('The sum of ', num1, ' and ', num2, ' is ',sum)

OUTPUT: Enter first number: 10


Enter second number: 30
The sum of 10 and 30 is 40
Page 3 of 8
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com

Chapter- 9: Input/output in Python

Accept an float input from User


You need to convert user input to the float number using the float()
function as we did for the integer value.
num1 = float (input("Enter first number: ")) Both input values are
num2 = float(input("Enter second number: ")) converted into float type
sum = num1 + num2
print ('The sum of ', num1, ' and ', num2, ' is ',sum)

OUTPUT: Enter first number: 10.5


Enter second number: 30.5
The sum of 10.5 and 30.5 is 41.0
Get multiple input values from a user in one line

In Python, we can accept two or three values from the user in one input()
call.

Page 4 of 8
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com

Chapter- 9: Input/output in Python

The print ( ) function

 The print ( ) function allows you to print out the value of a variable and
strings in parenthesis.
The general form of print ( ) function is:

print ( )
print (value/expr)
print (value, ...., sep=' ', end='\n', file=sys.stdout, flush=False)

 The print ( ) function prints a blank line. For example:


>>> print ( )
 The print ( ) function prints the value of variable or expression. For
example:
>>> N1 = 20

>>> N2 = 30

>>> Sum = N1 + N2

>>> print (Sum) # prints a value

50

>>> print (N1+N2) # prints a value of an expression i.e., N1+N2

50

>>> Ver = 3.6

>>> Prog = "Python"


Page 5 of 8
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com

Chapter- 9: Input/output in Python

>>> print (Prog, Ver) # prints two values separated with comma (,)

Python 3.6
 The print() function contains number of options. You can use either
one, two or all at once.
For example:

− Print a message.

>>> print ("The sum is") # prints a message

The sum is

>>> print ("My Python")

My Python

− Print a value, message or both message and values and vice versa.

>>> print ("The sum is", Sum) # prints a message with a value

The sum is 50

>>> print (Sum, "is the sum of", N1, "and", N2) # print is alternate way

50 is the sum of 20 and 30

 The sep separator is used between the values. It defaults into a space
character. For example:
>>> print (10,20,30,40,sep='*') # prints a separator *

10*20*30*40

Page 6 of 8
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com

Chapter- 9: Input/output in Python

 After all values are printed, end is printed. It defaults into a new line.
>>> print (10, 20, 30, 40, sep = '*', end = '@') # A new end is assigned @

10*20*30*40@

Let us take other examples, where a print() function can output multiple
things at once, each item separated by a comma.

# The following code prints: The answer to 10+10 is 20

>>>print (" The answer to 10+10 is" , 10+10)

# The following code prints: The answer to 10+10 is 10+10

>>>print (" The answer to 10+10 is", "10+10 ")

# The following code does not work and product Invalid Syntax because
the comma (,) is inside the quotation marks and not outside.

>>>print (" The answer to 10+10 is ," 10+10)

 print () with Concatenate Operator (+)


The print() function can print two strings either using comma (,) operator,
or a concatenate operator (+).
For example,

>>> print ("My name is Ahil", "Class-XI A.") # Print with space separator.

My name is Ahil Class-XI A.

Similarly, by using concatenate operator (+), the print() function is:

>>> print ("My name is Ashaz" + "Class-XI B.") # Print without separator.

My name is AshazClass-XI A.
Page 7 of 8
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com

Chapter- 9: Input/output in Python

Formatting Data with print()

The general Syntax:

% width.precision type-char

For example:

a=2.45567

print("%.2F" %a)

Page 8 of 8

You might also like