24 {{{ Show the slide containing title }}} |
24 {{{ Show the slide containing title }}} |
25 |
25 |
26 {{{ Show the slide containing the outline slide }}} |
26 {{{ Show the slide containing the outline slide }}} |
27 |
27 |
28 Input and Output are used in almost every program we use. |
28 Input and Output are used in almost every program we use. |
29 In this tutorial, we shall learn |
29 In this tutorial, we shall learn how to |
30 |
30 |
31 * Outputting data |
31 * Output data |
32 * Taking input from the user |
32 * Take input from the user |
33 |
33 |
34 type |
34 type |
35 :: |
35 :: |
36 |
36 |
37 a = "This is a string" |
37 a = "This is a string" |
38 a |
38 a |
39 print a |
39 print a |
40 |
40 |
|
41 <<<<<<< local |
41 print a, prints the value of a. |
42 print a, prints the value of a. |
|
43 ======= |
|
44 ``print a``, obviously, is printing the value of ``a``. |
|
45 >>>>>>> other |
42 As you can see, even when you type just a, the value of a is shown. |
46 As you can see, even when you type just a, the value of a is shown. |
43 But there is a difference. |
47 But there is a difference. |
44 |
48 |
45 .. #[Amit: The next sentence does seem to be clear enough] |
49 .. #[Amit: The next sentence does seem to be clear enough] |
46 |
50 |
57 While typing print b prints the string and hence the newline. |
61 While typing print b prints the string and hence the newline. |
58 |
62 |
59 Moreover when we type just a, the value a is shown only in interactive mode and |
63 Moreover when we type just a, the value a is shown only in interactive mode and |
60 does not have any effect on the program while running it as a script. |
64 does not have any effect on the program while running it as a script. |
61 |
65 |
|
66 .. #[punch: I think we could show that?] |
|
67 |
62 We shall look at different ways of outputting the data. |
68 We shall look at different ways of outputting the data. |
63 |
69 |
|
70 <<<<<<< local |
64 .. #[Amit: C's printf syntax ?? i think its better to elaborate the |
71 .. #[Amit: C's printf syntax ?? i think its better to elaborate the |
65 idea] |
72 idea] |
66 |
73 |
67 print statement in python supports string formatting. |
74 print statement in python supports string formatting. |
68 Various arguments can be passed to print using modifiers. |
75 Various arguments can be passed to print using modifiers. |
|
76 ======= |
|
77 ``print`` statement also accepts the syntax of C's ``printf`` statement. |
|
78 Various arguments can be passed to ``print`` using modifiers. |
|
79 >>>>>>> other |
69 type |
80 type |
70 :: |
81 :: |
71 |
82 |
72 x = 1.5 |
83 x = 1.5 |
73 y = 2 |
84 y = 2 |
74 z = "zed" |
85 z = "zed" |
75 print "x is %2.1f y is %d z is %s"%(x,y) |
86 print "x is %2.1f y is %d z is %s"%(x,y) |
76 |
87 |
77 As you can see, the values of x and y are substituted in place of %2.1f and %d |
88 As you can see, the values of x and y are substituted in place of |
|
89 ``%2.1f`` and ``%d`` |
78 |
90 |
79 {{{ Pause here and try out the following exercises }}} |
91 {{{ Pause here and try out the following exercises }}} |
80 |
92 |
81 %% 1 %% What happens when you do ``print "x is %d y is %f" %(x, y)`` |
93 %% 1 %% What happens when you do ``print "x is %d y is %f" %(x, y)`` |
82 |
94 |
83 {{{ continue from paused state }}} |
95 {{{ continue from paused state }}} |
84 |
96 |
85 We see that the int value of x and float value of y are printed corresponding |
97 We see that the ``int`` value of x and ``float`` value of y are |
86 to the modifiers used in the print statement. |
98 printed corresponding to the modifiers used in the print statement. |
87 |
99 |
88 We can also see that print statement prints a new line character at the end of |
100 We can also see that ``print`` statement prints a new line character |
89 line, everytime it is called. This can be suppressed by using a "," at the end |
101 at the end of the line, everytime it is called. This can be suppressed |
90 print statement. |
102 by using a "," at the end ``print`` statement. |
91 |
103 |
92 Let us see this by typing out following code on an editor as print_example.py |
104 Let us see this by typing out following code on an editor as print_example.py |
93 |
105 |
94 {{{ open an editor }}} |
106 {{{ open an editor }}} |
95 type |
107 type |