0% found this document useful (0 votes)
2 views2 pages

Python Day7

The document provides an overview of Python's output and input functions, specifically the print() and input() functions. It explains how to use print() for displaying data, including formatting options and escape sequences, as well as how to gather user input with the input() function. Examples are provided to illustrate the syntax and functionality of these functions.

Uploaded by

Sivasree S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Python Day7

The document provides an overview of Python's output and input functions, specifically the print() and input() functions. It explains how to use print() for displaying data, including formatting options and escape sequences, as well as how to gather user input with the input() function. Examples are provided to illustrate the syntax and functionality of these functions.

Uploaded by

Sivasree S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

20CST283 - Python for Machine Learning

1 Output and Input functions- print() and input()


1.1 Python Output Using print() function
We use the print() function to output data to the standard output device (screen)
An example of its use is given below.

>>>print(’This is a sample output’)


This is a sample output

Another example is given below:


>>>x = 5
>>>print(’The value of x is’, x)
The value of a is 5

The actual syntax of the print() function is:


print(∗objects, sep =0 0 , end =0 ∗0 , f ile = sys.stdout, f lush = F alse)

Here, objects is the value(s) to be printed.


The sep separator is used between the values. It defaults into a space character.
After all values are printed, end is printed. It defaults into a new line.
The file is the object where the values are printed and its default value is sys.stdout (screen).

>>>print(1, 2, 3, 4, sep=’*’)
1*2*3*4
>>>print(1, 2, 3, 4, sep=’#’, end=’&’)
1#2#3#4&

1.1.1 Output formatting


Sometimes we would like to format our output to make it look attractive. This can be done by using the str.format() method.
>>> x = 5; y = 10
>>> print(’The value of x is and y is ’.format(x,y))
T he value of x is 5 and y is 10
Here, the curly braces {} are used as placeholders. We can specify the order in which they are printed by using numbers (tuple
index).

>>> print(0 I love {0} and {1}0 .f ormat(0 bread0 ,0 butter0 ))


I love bread and butter
>>> print(0 I love 1 and 00 .f ormat(0 bread0 ,0 butter0 ))
I love butter and bread

We can even use keyword arguments to format the string.


>>> print(’Hello {name}, {greeting}’.format(greeting = ’Goodmorning’, name = ’John’)) Hello John, Goodmorning

1.1.2 Escape sequences


Escape sequences are the way Python express special characters in strings such as newline, tab and backspace etc.The back
slash(
) is used for escape sequences, it must be escaped to appear as literal character in string. Thus print(¨ ¨) will print a single
character. The following are some of the commonly used escape sequence in Python 3.

1
Escape Sequence Meaning

1.2 Python Input using input()


Up until now, our programs were static. To allow flexibility, we might want to take the input from the user. In Python, we have
the input() function to allow this.
The syntax for input() is:
input([prompt])
where prompt is the string we wish to display on the screen. It is optional.
>>> num = input(’Enter a number: ’)
Enter a number: 10
>>> num ’10’
>>>type(num)
< class 0 str0 >

You might also like