Python/C3/I-O/English

From Script | Spoken-Tutorial
Revision as of 11:44, 29 November 2012 by Chandrika (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Visual Cue Narration
Show Slide 1

Containing title, name of the production team along with the logo of MHRD

Hello friends and welcome to this tutorial on 'Input/Output'.
Show Slide 2

Learning objectives

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.


ipython Let us first start ipython on our terminal
a = "This is a string"
a
print a
Let us start this tutorial by typing a string
b = "A line \n New line"
b
print b
print a, obviously, 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.

x = 1.5
y = 2
z = "red"
print "x is %2.1f, y is %d, z is %s"%(x,y,z)
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

As you can see, the values of x, y and z are substituted in place of the modifiers %2.1f, %d and %s respectively.

Pause the video here, try out the following exercise and resume the video.

Show Slide 3

Assignment 1

What happens when you do print "x is %d, y is %f" %(x, y)
Continue from paused state Switch to the terminal
print "x is %d, y is %f" %(x, y)
Switch to the terminal for solution.
open an editor
print "Hello"
print "World"

print "Hello",
print "World"
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 everytime it is called. This can be suppressed by using a "," at the end of the print statement.

Let us see this by typing out following code on an editor as print_example.py

ip = raw_input() Save the script as 'print_example.py' and run it using %run /home/fossee/print_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_input~~ for this. type

an input The cursor is blinking indicating that it is waiting for input type something and hit enter.
ip Now let us see what is the value of ip by typing it.
We can see that it contains the string "an input"

Pause the video here, try out the following exercise and resume the video.

Show Slide 4

Assignment 2

Enter the number 5.6 as input and store it in a variable called c.
Continue from paused state Switch to the terminal Switch to the terminal for solution.
c = raw_input()
5.6
c
We have to use the raw_input command with variable c. type
type(c) Now let us see the type of 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.

Show Slide 5

Assignment 3

What happens when you do not enter anything and hit enter.
Continue from paused state Switch to the terminal
d = raw_input()
<RET>
d
Switch to the terminal for solution.
name = raw_input("Please enter your name: ") We see that when nothing is entered, an empty string is considered as input.

raw_input also can display a prompt to assist the user.

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.

Show Slide 6

Assignment 4

How do you display a prompt and let the user enter input in next line.
Continue from paused state Switch to the terminal
ip = raw_input("Please enter a number in the next line\n> ")
Switch to the terminal for solution. The trick is to include a newline character at the end of the prompt string.
It prints the newline character and hence the user enters input in the next line
Show Slide 7

Summary slide

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 %d, %f, %s in the print statement.
  3. Take input from user by using raw_input().
  4. Display a prompt to the user before taking the input by passing a string as an argument to raw_input.


Show Slide 8

Self assessment questions slide

Here are some self assessment questions for you to solve
  1. a = raw_input() and user enters 2.5. What is the type of a?
    • str
    • int
    • float
    • char
  1. a = 2 and b = 4.5. What does ``print "a is %d and b is
    Inline literal start-string without end-string.
     %2.1f" %(b, 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


Show Slide 9

Solution of self assessment questions on slide

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 %d. Similarly, 'b' will get the float value of 'a' due to it's modifier %2.1f. Hence 'a' will be 4 and 'b' 2.0 .


Show Slide 10

Acknowledgment slide

Hope you have enjoyed this tutorial and found it useful. Thank You!

Contributors and Content Editors

Chandrika