Python-3.4.3/C3/Input-output/English-timed
From Script | Spoken-Tutorial
Time | Narration |
00:01 | Welcome to the spoken tutorial on Input and Output. |
00:06 | In this tutorial, we will learn to
Print some values Print using format specifiers |
00:14 | Take input from user and
Display a prompt to the user before taking the input |
00:22 | To record this tutorial, I am using
Ubuntu Linux 16.04 operating system Python 3.4.3 and IPython 5.1.0 |
00:38 | To practise this tutorial, You should know how to run basic Python commands on the ipython console. |
00:47 | If not, see the relevant Python tutorials on this website. |
00:52 | Let us start ipython. |
00:55 | Open the terminal.
Type ipython3 and press Enter. |
01:03 | From here onwards, remember to press the Enter key after typing every command on the terminal. |
01:10 | Let us start this tutorial with a string. |
01:14 | Type, a is equal to inside double quotes This is a string |
01:21 | To see the value of a, type, a |
01:26 | Type, print inside parentheses a |
01:31 | This also prints the value of a. But there is a difference in the outputs. Typing just a displays the content of a. |
01:43 | The statement print inside parentheses a prints the string itself. We can see the difference clearly when we use strings with new lines in them. |
01:55 | Type b is equal to inside double quotes A line backslash n New line |
02:04 | Type, b As you can see, it just shows that b contains a newline character. |
02:13 | Type, print inside parentheses b |
02:18 | It prints the string, A line and then New line in the next line. print statement in Python supports string formatting. |
02:28 | Next we shall look at different ways of outputting the data. |
02:33 | Percentage string operator is called as format operator. |
02:38 | For example:
Percentage d - specifies the integer format Percentage s - specifies the string format and Percentage f - denotes the float format |
02:52 | Switch back to the terminal. Next let us assign x as shown. |
03:00 | To print the value of x, type as shown. |
03:06 | Here percentage 3.2f specifies the output in float and .2f rounds off the value to two decimal point. |
03:18 | We will see one more example. Type as shown. Here, it rounds off to four decimal places. |
03:30 | Next, let us assign two more values. Type, y is equal to 2 |
03:38 | z is equal to inside double quotes red |
03:43 | To print the value of x, y and z, type as shown. |
03:50 | Various arguments can be passed to print using modifiers. |
03:55 | The values of x, y and z are substituted in place of the format specifiers as
percentage 2.1f, percentage d and percentage s respectively. |
04:09 | Pause the video. Try this exercise and then resume the video. |
04:15 | What happens when you execute the following? |
04:18 | Switch to the terminal for the solution. |
04:22 | Type as shown. Here the int value of x and float value of y are printed as per the format specifiers. |
04:34 | It prints six decimal places by default for float. |
04:39 | Usually print statement prints the output in a new line. Now we will see how to suppress the newline character. |
04:49 | Open a text editor and type the following code. |
04:53 | The newline character can be suppressed by passing end is equal to inside single quotes a space. |
05:01 | Save the script as print underscore example.py. |
05:07 | Switch back to the terminal. |
05:10 | Let us run the code by typing percentage run space print underscore example.py |
05:19 | We can see that the print statement prints a space instead of a new line. |
05:25 | Next we shall look at taking input from the user. We will use the input function for this. |
05:33 | Type ip is equal to input open and close parentheses |
05:39 | The cursor is blinking indicating that it is waiting for an input. Type, an input and press Enter. |
05:49 | Now let us see what is the value of ip.Type ip We can see that it contains the string - an input |
06:00 | Pause the video. Try this exercise and then resume the video. Enter the number 5.6 as input and store it in a variable c. |
06:12 | Switch back to the terminal for the solution. |
06:16 | We have to use the input command with variable c. Type, c is equal to input open and close parentheses |
06:26 | Enter 5.6 as input. Press Enter. |
06:32 | To see the input value, type c |
06:36 | Now let us see the data type of c. Type, type inside parentheses c We see that c is a string. |
06:48 | Because input command always takes the input as string no matter whatever is the input. |
06:56 | Pause the video. Try this exercise and then resume the video. |
07:02 | Execute the below statement. What happens when you do not enter anything and hit Enter? |
07:10 | Switch back to the terminal for the solution. |
07:14 | Type d is equal to input open and close parentheses Press Enter without giving any input. |
07:24 | Again press Enter to get the prompt. |
07:28 | To see the input value, type, d When nothing is entered, an empty string is considered as input. |
07:38 | We can also use input to display a prompt to assist the user. |
07:44 | Now we will give a prompt to get the input.
Type ip is equal to input inside parentheses inside double quotes Please enter a number backslash n |
07:58 | I will give 12 as input. This is how we can display a prompt to get input from user. |
08:08 | This brings us to the end of this tutorial. |
08:12 | Let us summarise. In this tutorial, we have learnt to, |
08:17 | Use print statement |
08:20 | Use the format specifiers percentage d, percentage f and percentage s in the print statement |
08:29 | Take input from user by using input function and Display a prompt to the user before taking the input |
08:39 | Here are some self assessment questions for you to solve a is equal to input open and close parentheses and user enters 2.5. What is the type of a? |
08:55 | 2. If a is equal to 2 and b is equal to 4.5, what is the result of the following action? |
09:05 | And the answers,
1. No matter what you enter, it will be taken as a string. Hence 2.5 is a string. |
09:16 | 2. Since b is called first, it will display integer value of b. Because the modifier used is percentage d. |
09:28 | Similarly, float value of a will be displayed due to its modifier percentage 2.1f. Hence the output is a is 4 and b is 2.0 |
09:42 | Please post your timed queries in this forum. |
09:46 | Please post your general queries on Python in this forum. |
09:51 | FOSSEE team coordinates the TBC project. |
09:55 | Spoken-tutorial is funded by NMEICT, MHRD, Govt. of India. For more details, visit this website. |
10:04 | This is Priya from IIT Bombay signing off. Thanks for watching. |