1 00:00:00 --> 00:00:04 Hello friends and welcome to this tutorial on 'Input/Output'. 2 00:00:05 --> 00:00:19 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. 3 00:00:20 --> 00:00:25 So type ipython in the terminal. 4 00:00:26 --> 00:00:44 Type a = within double quotes This is a string Type a Type print a 5 00:00:45 --> 00:00:51 obviously, print a , prints the value of a . 6 00:00:52 --> 00:00:58 As you can see, even when you type just a, the value of a is shown. 7 00:00:59 --> 00:01:00 But there is a difference. 8 00:01:01 --> 00:01:07 Typing just a displays the content of a whereas the statement print a prints the string itself. 9 00:01:08 --> 00:01:13 This difference becomes more evident when we use strings with newlines in them. 10 00:01:14 --> 00:01:34 Type b = within double quotes A line backslash n New line and hit enter Type b Type print b 11 00:01:35 --> 00:01:45 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. 12 00:01:46 --> 00:01:59 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. 13 00:02:00 --> 00:02:03 We shall look at different ways of outputting the data. 14 00:02:04 --> 00:02:07 print statement in python supports string formatting. 15 00:02:08 --> 00:02:11 Various arguments can be passed to print using modifiers. 16 00:02:12 --> 00:02:50 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 17 00:02:51 --> 00:03:02 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. 18 00:03:03 --> 00:03:07 Pause the video here, try out the following exercise and resume the video. 19 00:03:08 --> 00:03:18 What happens when you do print within double quotes x is modula d comma y is modula f modula x comma y 20 00:03:19 --> 00:03:23 Switch to the terminal for solution. 21 00:03:24 --> 00:03:49 Type print within double quotes x is modula d comma y is modula f modula within brackets x comma y 22 00:03:50 --> 00:03:57 We see that the int value of x and float value of y are printed corresponding to the modifiers used in the print statement. 23 00:03:58 --> 00:04:03 We have seen that print statement prints a new line character every time it is called. 24 00:04:04 --> 00:04:12 This can be suppressed by using a " comma " at the end of the print statement. 25 00:04:13 --> 00:04:23 Let us see this by typing out following code on an editor as print underscore example.py 26 00:04:24 --> 00:04:43 So Type.. 27 00:04:44 --> 00:05:21 print "Hello" print "World" print "Hello" comma print "World" 28 00:05:22 --> 00:05:33 Save the script as 'print underscore example.py' and run it using modula run slash home slash fossee slash print underscore example.py 29 00:05:34 --> 00:05:45 As we can see, the print statement when used with comma in the end, prints a space instead of a new line. 30 00:05:46 --> 00:06:05 Now we shall look at taking input from the user. 31 00:06:06 --> 00:06:10 We will use the ~~raw underscore input~~ for this. 32 00:06:11 --> 00:06:22 So type ip = raw underscore input() 33 00:06:23 --> 00:06:31 The cursor is blinking indicating that it is waiting for input, so type something and hit enter. 34 00:06:32 --> 00:06:34 So you can type an input 35 00:06:35 --> 00:06:40 Now let us see what is the value of ip by typing it. 36 00:06:41 --> 00:06:44 So type ip on the terminal and hit enter 37 00:06:45 --> 00:06:50 We can see that it contains the string "an input" 38 00:06:51 --> 00:06:57 Pause the video here, try out the following exercise and resume the video. 39 00:06:58 --> 00:07:01 You have an question 40 00:07:02 --> 00:07:10 Enter the number 5.6 as input and store it in a variable called c. 41 00:07:11 --> 00:07:14 Switch to the terminal for solution. 42 00:07:15 --> 00:07:18 We have to use the raw underscore input command with variable c. 43 00:07:19 --> 00:07:35 So type c = raw underscore input() and hit enter Put 5.6 And again enter. Type c 44 00:07:36 --> 00:07:39 Now let us see the type of c. 45 00:07:40 --> 00:07:45 Type type within brackets c 46 00:07:46 --> 00:07:48 We see that c is a string. 47 00:07:49 --> 00:07:54 This implies that anything you enter as input, it will be taken as a string no matter what you enter. 48 00:07:55 --> 00:07:58 Pause the video here, try out the following exercise and resume the video. 49 00:07:59 --> 00:08:03 What happens when you do not enter anything and hit enter. 50 00:08:04 --> 00:08:07 Switch to the terminal for solution. 51 00:08:08 --> 00:08:27 Type d = raw underscore input() d 52 00:08:28 --> 00:08:31 We see that when nothing is entered, an empty string is considered as input. 53 00:08:32 --> 00:08:36 raw underscore input also can display a prompt to assist the user. 54 00:08:37 --> 00:08:47 So type name = raw underscore input within brackets within double quotes Please enter your name: 55 00:08:48 --> 00:08:53 It prints the string given as argument and then waits for the user input. 56 00:08:54 --> 00:08:55 Let us do one more exercise. 57 00:08:56 --> 00:08:59 Pause the video here, try out the following exercise and resume the video. 58 00:09:00 --> 00:09:08 How do you display a prompt and let the user enter input in next line. 59 00:09:09 --> 00:09:11 Switch to the terminal now. 60 00:09:12 --> 00:09:16 The trick is to include a newline character at the end of the prompt string. 61 00:09:17 --> 00:09:27 Type ip = raw underscore input within brackets within double quotes Please enter a number in the next line backslash n 62 00:09:28 --> 00:09:34 It prints the newline character and hence the user enters input in the next line 63 00:09:35 --> 00:09:38 This brings us to the end of the tutorial. 64 00:09:39 --> 00:09:41 In this tutorial, we have learnt to,1. Use the print statement. 65 00:09:42 --> 00:09:46 2. Use the modifiers modula d, modula f, modula s in the print statement. 66 00:09:47 --> 00:09:54 3. Take input from user by using raw underscore input(). 67 00:09:55 --> 00:10:03 4. Display a prompt to the user before taking the input by passing a string as an argument to raw underscore input. 68 00:10:04 --> 00:10:07 Here are some self assessment questions for you to solve 69 00:10:08 --> 00:10:12 1. a = raw underscore input() and user enters 2.5 . 70 00:10:13 --> 00:10:19 What is the type of a? str int float char 71 00:10:20 --> 00:10:26 2. a = 2 and b = 4.5. 72 00:10:27 --> 00:10:49 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 73 00:10:50 --> 00:10:52 And the answers, 74 00:10:53 --> 00:10:57 1.No matter what you enter, it will be taken as a string. 75 00:10:58 --> 00:11:00 Hence 2.5 is a string. 76 00:11:01 --> 00:11:09 2. Since 'b' is called first, It will display integer value of 'a' because the modifier used is modula d. 77 00:11:10 --> 00:11:17 Similarly, 'b' will get the float value of 'a' due to it's modifier modula 2.1f. 78 00:11:18 --> 00:11:23 Hence 'a' will be 4 and 'b' 2.0 . 79 00:11:24 --> 00:11:29 Hope you have enjoyed this tutorial and found it useful.