Hello friends and welcome to this tutorial on 'Input/Output'.
At the end of this tutorial,you will be able to, Print some value. Print using modifiers. Take input from user. Display a prompt to the user before taking the input.
So type ipython in the terminal.
Type a = within double quotes This is a string Type a Type print a
obviously, print a , prints the value of a .
As you can see, even when you type just a, the value of a is shown.
But there is a difference.
Typing just a displays the content of a whereas the statement print a prints the string itself.
This difference becomes more evident when we use strings with newlines in them.
Type b = within double quotes A line backslash n New line and hit enter Type b Type print b
As you can see, just typing b shows that b contains a newline character but While typing print b,it prints the string and hence the newline.
Moreover when we type just a , the value a is shown only in interactive mode and does not have any effect on the program while running it as a script.
We shall look at different ways of outputting the data.
print statement in python supports string formatting.
Various arguments can be passed to print using modifiers.
type x = 1.5 y = 2 z = within double quotes red print then in double quotes x is modula 2 dot 1f comma y is modula d comma z is modula s then again a modula within brackets x comma y comma z
As you can see, the values of x, y and z are substituted in place of the modifiers modula 2.1f, modula d and modula s respectively.
Pause the video here, try out the following exercise and resume the video.
What happens when you do print within double quotes x is modula d comma y is modula f modula x comma y
Switch to the terminal for solution.
Type print within double quotes x is modula d comma y is modula f modula within brackets x comma y
We see that the int value of x and float value of y are printed corresponding to the modifiers used in the print statement.
We have seen that print statement prints a new line character every time it is called.
This can be suppressed by using a " comma " at the end of the print statement.
Let us see this by typing out following code on an editor as print underscore example.py
So Type..
print "Hello" print "World" print "Hello" comma print "World"
Save the script as 'print underscore example.py' and run it using modula run slash home slash fossee slash print underscore example.py
As we can see, the print statement when used with comma in the end, prints a space instead of a new line.
Now we shall look at taking input from the user.
We will use the ~~raw underscore input~~ for this.
So type ip = raw underscore input()
The cursor is blinking indicating that it is waiting for input, so type something and hit enter.
So you can type an input
Now let us see what is the value of ip by typing it.
So type ip on the terminal and hit enter
We can see that it contains the string "an input"
Pause the video here, try out the following exercise and resume the video.
You have an question
Enter the number 5.6 as input and store it in a variable called c.
Switch to the terminal for solution.
We have to use the raw underscore input command with variable c.
So type c = raw underscore input() and hit enter Put 5.6 And again enter. Type c
Now let us see the type of c.
Type type within brackets c
We see that c is a string.
This implies that anything you enter as input, it will be taken as a string no matter what you enter.
Pause the video here, try out the following exercise and resume the video.
What happens when you do not enter anything and hit enter.
Switch to the terminal for solution.
Type d = raw underscore input() d
We see that when nothing is entered, an empty string is considered as input.
raw underscore input also can display a prompt to assist the user.
So type name = raw underscore input within brackets within double quotes Please enter your name:
It prints the string given as argument and then waits for the user input.
Let us do one more exercise.
Pause the video here, try out the following exercise and resume the video.
How do you display a prompt and let the user enter input in next line.
Switch to the terminal now.
The trick is to include a newline character at the end of the prompt string.
Type ip = raw underscore input within brackets within double quotes Please enter a number in the next line backslash n
It prints the newline character and hence the user enters input in the next line
This brings us to the end of the tutorial.
In this tutorial, we have learnt to,1. Use the print statement.
2. Use the modifiers modula d, modula f, modula s in the print statement.
3. Take input from user by using raw underscore input().
4. Display a prompt to the user before taking the input by passing a string as an argument to raw underscore input.
Here are some self assessment questions for you to solve
1. a = raw underscore input() and user enters 2.5 .
What is the type of a? str int float char
2. a = 2 and b = 4.5.
What does ``print "a is modula d and b is In line literal start-string without end-string. modula 2.1f" modula within brackets b comma a`` print? a is 2 and b is 4.5 a is 4 and b is 2 a is 4 and b is 2.0 a is 4.5 and b is 2
And the answers,
1.No matter what you enter, it will be taken as a string.
Hence 2.5 is a string.
2. Since 'b' is called first, It will display integer value of 'a' because the modifier used is modula d.
Similarly, 'b' will get the float value of 'a' due to it's modifier modula 2.1f.
Hence 'a' will be 4 and 'b' 2.0 .
Hope you have enjoyed this tutorial and found it useful.