INPUT-OUTPUT
STATEMENTS
IN
PYTHON
thon provides numerous built-in functions
at are readily available to us at the Python prompt.
me of the functions like input() and print() are
dely used for standard input and output operations respectively.
t us see the output section first.
hon Output Using print() function
use the print() function to output data to the standard output device (scre
print('This sentence is output to the screen')
Output
This sentence is output to the screen
The actual syntax of the print() function is:
print(*objects, sep=' ', end='n', file=sys.stdout, flush=False)
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
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.
This method is visible to any string object.
print('I love {0} and {1}'.format('bread','butter'))
print('I love {1} and {0}'.format('bread','butter'))
Here, the curly braces {} are used as placeholders.
We can specify the order in which they are printed by using numbers (tuple index).
We can even use keyword arguments to format
the string.
print('Hello {name}, {greeting}'.format(greeting = 'Goodmorning', name = 'John’))
Hello John, Goodmorning
We can also format strings like the old sprintf() style
used in C programming language.
We use the % operator to accomplish this.
>>> x = 12.3456789
>>> print('The value of x is %3.2f' %x)
The value of x is 12.35
>>> print('The value of x is %3.4f' %x)
The value of x is 12.3457
Python Input
until now, our programs were static. The value of variables was defined or hard coded into the source co
llow flexibility, we might want to take the input from the user.
ython, we have the input() function to allow this. The syntax for input() is: input([prompt])

Python variables in the computer science.pptx

  • 1.
  • 2.
    thon provides numerousbuilt-in functions at are readily available to us at the Python prompt. me of the functions like input() and print() are dely used for standard input and output operations respectively. t us see the output section first.
  • 3.
    hon Output Usingprint() function use the print() function to output data to the standard output device (scre print('This sentence is output to the screen') Output This sentence is output to the screen The actual syntax of the print() function is: print(*objects, sep=' ', end='n', file=sys.stdout, flush=False) 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
  • 4.
    Output formatting Sometimes wewould like to format our output to make it look attractive. This can be done by using the str.format() method. This method is visible to any string object. print('I love {0} and {1}'.format('bread','butter')) print('I love {1} and {0}'.format('bread','butter')) Here, the curly braces {} are used as placeholders. We can specify the order in which they are printed by using numbers (tuple index).
  • 5.
    We can evenuse keyword arguments to format the string. print('Hello {name}, {greeting}'.format(greeting = 'Goodmorning', name = 'John’)) Hello John, Goodmorning
  • 6.
    We can alsoformat strings like the old sprintf() style used in C programming language. We use the % operator to accomplish this. >>> x = 12.3456789 >>> print('The value of x is %3.2f' %x) The value of x is 12.35 >>> print('The value of x is %3.4f' %x) The value of x is 12.3457
  • 7.
    Python Input until now,our programs were static. The value of variables was defined or hard coded into the source co llow flexibility, we might want to take the input from the user. ython, we have the input() function to allow this. The syntax for input() is: input([prompt])