Python/C3/I-O/English-timed

From Script | Spoken-Tutorial
Revision as of 17:21, 18 March 2013 by Sneha (Talk | contribs)

Jump to: navigation, search
Visual Cue Narration
0:00 Hello friends and welcome to this tutorial on 'Input/Output'.
0:05 At the end of this tutorial,you will be able to,
  1. Print some value.
  2. Print using modifiers.
  3. Take input from user.
  4. Display a prompt to the user before taking the input.
0:20 So type ipython on our terminal.
0:26 Type a = within double quotes This is a string
 Type a
 Type print a


0:45 obviously, print a , prints the value of a .
0:52 As you can see, even when you type just a, the value of a is shown.
0:59 But there is a difference.
1:01 Typing just a displays the content of a whereas the statement print a prints the string itself.
1:08 This difference becomes more evident when we use strings with newlines in them.
1:14 Type b = within double quotes A line backslash n New line and hit enter
Type b
Type print b
1:35 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.
1:46 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.
2:00 We shall look at different ways of outputting the data.
2:04 print statement in python supports string formatting.
2:08 Various arguments can be passed to print using modifiers.
2:12 type x = 1.5
      y = 2
      z = within double quotes red
      print   within 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


2:51 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.
3:03 Pause the video here, try out the following exercise and resume the video.
3:08 What happens when you do print within double quotes x is modula d comma y is modula f modula within brackets x comma y
3:19 Switch to the terminal for solution.
3:24 Type print within double quotes x is modula d comma y is modula f modula within brackets x comma y
3:50 We see that the int value of x and float value of y are printed corresponding to the modifiers used in the print statement.
3:58 We have seen that print statement prints a new line character every time it is called.
4:04 This can be suppressed by using a " comma " at the end of the print statement.
4:13 Let us see this by typing out following code on an editor as print underscore example.py
4:24 So Type..


4:44 print "Hello"
print "World"
print "Hello" comma 
print "World"
5:22 Save the script as 'print underscore example.py' and run it using modula run slash home slash fossee slash print underscore example.py
5:34 As we can see, the print statement when used with comma in the end, prints a space instead of a new line.
5:46 Now we shall look at taking input from the user.
6:06 We will use the ~~raw underscore input~~ for this.
6:11 So type ip = raw underscore input()
6:23 The cursor is blinking indicating that it is waiting for input, so type something and hit enter.
6:32 So you can type an input
6:35 Now let us see what is the value of ip by typing it.
6:41 So type ip and hit enter
6:45 We can see that it contains the string "an input"
6:51 Pause the video here, try out the following exercise and resume the video.
6:58 You have an question
7:02 Enter the number 5.6 as input and store it in a variable called c.
7:11 Switch to the terminal for solution.
7:15 We have to use the raw underscore input command with variable c.
7:19 So type c = raw underscore input() and hit enter
Put 5.6
And again enter.
Type c
7:36 Now let us see the type of c.
7:40 Type type within brackets c
7:46 We see that c is a string.
7:49 This implies that anything you enter as input, it will be taken as a string no matter what you enter.
7:55 Pause the video here, try out the following exercise and resume the video.
7:59 What happens when you do not enter anything and hit enter.
8:04 Switch to the terminal for solution.
8:08 Type d = raw underscore input()

<RET> d

8:28 We see that when nothing is entered, an empty string is considered as input.
8:32 raw underscore input also can display a prompt to assist the user.
8:37 So type name = raw underscore input within brackets within double quotes Please enter your name:
8:48 It prints the string given as argument and then waits for the user input.
8:54 Let us do one more exercise.
8:56 Pause the video here, try out the following exercise and resume the video.
9:00 How do you display a prompt and let the user enter input in next line.
9:09 Switch to the terminal for solution.
9:12 The trick is to include a newline character at the end of the prompt string.
9:17 Type ip = raw underscore input within brackets within double quotes Please enter a number in the next line backslash n>
9:28 It prints the newline character and hence the user enters input in the next line
9:35 This brings us to the end of the tutorial.
9:39 In this tutorial, we have learnt to,1. Use the print statement.
9:42 2. Use the modifiers modula d, modula f, modula s in the print statement.
9:47 3. Take input from user by using raw underscore input().
9:55 4. Display a prompt to the user before taking the input by passing a string as an argument to raw underscore input.
10:04 Here are some self assessment questions for you to solve
10:08 1. a = raw underscore input() and user enters 2.5 .
10:13 What is the type of a?
    • str
    • int
    • float
    • char
10:20 2. a = 2 and b = 4.5.
10:27 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
10:50 And the answers,
10:53 1.No matter what you enter, it will be taken as a string.
10:58 Hence 2.5 is a string.
11:01 2. Since 'b' is called first, It will display integer value of 'a' because the modifier used is modula d.
11:10 Similarly, 'b' will get the float value of 'a' due to it's modifier modula 2.1f.
11:18 Hence 'a' will be 4 and 'b' 2.0 .
11:24 Hope you have enjoyed this tutorial and found it useful.

Contributors and Content Editors

Gaurav, Minal, PoojaMoolya, Sneha