Python-3.4.3/C3/Input-output/English

From Script | Spoken-Tutorial
Revision as of 11:10, 28 May 2018 by Priyacst (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Visual Cue
Narration
Show Slide Title Welcome to the spoken tutorial on Input and Output.
Show Slide

Learning Objectives

In this tutorial, we will learn to
  • Print some values
  • Print using format specifiers
  • Take input from user and
  • Display a prompt to the user before taking the input
Show Slide

System Specifications

To record this tutorial, I am using
  • Ubuntu Linux 16.04 operating system
  • Python 3.4.3 and
  • IPython 5.1.0
Show Slide

Pre-requisite slide

To practise this tutorial,
  • You should know how to run basic Python commands on the ipython console.

If not, see the relevant Python tutorials on this website.

[Terminal]

ipython3

Let us start ipython.


Open the terminal.


Type ipython3 and press Enter.


From here onwards, remember to press the Enter key after typing every command on the terminal.

Type, a = "This is a string" Let us start this tutorial with a string.


Type, a is equal to inside double quotes This is a string

Type, a To see the value of a, type, a
Type, print (a)


Highlight the output


Highlight the output of a


Highlight the output of print(a)


Type, print inside parentheses a


This also prints the value of a.


But there is a difference in the outputs.


Typing just a displays the content of a.


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.

Type, b = "A line \n New line" Now type b is equal to inside double quotes A line backslash n New line
Type, b


Highlight the output

Type, b


As you can see, it just shows that b contains a newline character.

Type, print (b)


Highlight the output.


Type, print inside parentheses b


It prints the string, A line and then New line in the next line.


print statement in Python supports string formatting.


<<PAUSE>>

Slide:

Formatting operations

Next we shall look at different ways of outputting the data.


Percentage string operator is called as format operator.


For example:

  • Percentage d - specifies the integer format
  • Percentage s - specifies the string format and
  • Percentage f - denotes the float format
Switch back to the terminal.
Type x = 1.56789

print('The value of x is %3.2f' %x)

Highlight %3.2f

Next let us assign x as shown.


To print the value of x, type as shown.


Here percentage 3.2f specifies the output in float and .2f rounds off the value to two decimal point.

print('The value of x is %3.4f' %x)


Highlight .4f

We will see one more example.


Type as shown.


Here, it rounds off to four decimal places.

Type, y = 2 Next, let us assign two more values.


Type, y is equal to 2

Type, z = "red" z is equal to inside double quotes red
Highlight

Type, print (''x is %2.1f, y is %d, z is %s''%(x,y,z))


Highlight%2.1f, %d, %s


To print the value of x, y and z, type as shown.


Various arguments can be passed to print using modifiers.


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.


<<PAUSE>>

Show Slide


Exercise 1

Pause the video.


Try this exercise and then resume the video.


What happens when you execute the following?

Switch the terminal Switch to the terminal for the solution.
Type, print (''x is %d, y is %f'' %(x, y))


Highlight %d, %f


Highlight 2.000000

Type as shown.


Here the int value of x and float value of y are printed as per the format specifiers.


It prints six decimal places by default for float.

Open text editor and type

print ("Hello")

print ("World")

print ("Hello",end=' ')

print ("World")

Usually print statement prints the output in a new line.


Now we will see how to suppress the newline character.


Open a text editor and type the following code.

Highlight end=' ' The newline character can be suppressed by passing end is equal to inside single quotes a space.
Save it as print_example.py Save the script as print underscore example.py.
Switch to the terminal Switch back to the terminal.
Type %run print_example.py


Highlight space in between Hello and world

Let us run the code by typing percentage run space print underscore example.py


We can see that the print statement prints a space instead of a new line.


<<PAUSE>>

Type, ip = input() Next we shall look at taking input from the user.


We will use the input function for this.


Type ip is equal to input open and close parentheses

Point the input cursor


Type an input

The cursor is blinking indicating that it is waiting for an input.


Type, an input and press Enter.

Type, ip


Highlight the outpt

Now let us see what is the value of ip.


Type ip


We can see that it contains the string - an input

Show Slide


Exercise 2

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.

Switch to terminal Switch back to the terminal for the solution.
Type, c = input() We have to use the input command with variable c.


Type, c is equal to input open and close parentheses

Enter 5.6 Enter 5.6 as input. Press Enter.
Type, c To see the input value, type c
Type, type(c)


Highlight the output


Now let us see the data type of c.


Type, type inside parentheses c


We see that c is a string.


Because input command always takes the input as string no matter whatever is the input.

Show Slide


Exercise 3

Pause the video.


Try this exercise and then resume the video.


Execute the below statement.


What happens when you do not enter anything and hit Enter?

Switch to terminal Switch back to the terminal for the solution.
Type, d = input()


<press enter>

Type d is equal to input open and close parentheses


Press Enter without giving any input.

Again press Enter to get the prompt.

Type, d


Highlight the output

To see the input value, type, d


When nothing is entered, an empty string is considered as input.


<<PAUSE>>

Type, ip = input("Please enter a number\n ") We can also use input to display a prompt to assist the user.


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

Enter 12 I will give 12 as input.


This is how we can display a prompt to get input from user.

Show Slide

Summary slide


This brings us to the end of this tutorial. Let us summarise.


In this tutorial, we have learnt to,

  • Use print statement
  • Use the format specifiers percentage d, percentage f and percentage s in the print statement
  • Take input from user by using input function and
  • Display a prompt to the user before taking the input
Show Slide

Evaluation


Here are some self assessment questions for you to solve


  1. a is equal to input open and close parentheses and user enters 2.5. What is the type of a?
  2. If a is equal to 2 and b is equal to 4.5, what is the result of the following action?
Show Slide


Solutions


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 b. Because the modifier used is percentage d.

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

Slide Forum Please post your timed queries in this forum.
Slide Fossee Forum Please post your general queries on Python in this forum.
Slide Textbook Companion FOSSEE team coordinates the TBC project.
Show Slide

Acknowledgement

Spoken-tutorial is funded by NMEICT, MHRD, Govt. of India.


For more details, visit this website.

Show Slide

Thank You

This is Priya from IIT Bombay signing off.


Thanks for watching.

Contributors and Content Editors

Nancyvarkey, Priyacst