Difference between revisions of "Python-3.4.3/C3/Input-output/English"
(Created page with " {| style="border-spacing:0;" | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| <ce...") |
|||
(3 intermediate revisions by 2 users not shown) | |||
Line 18: | Line 18: | ||
* Take '''input''' from user and | * Take '''input''' from user and | ||
* Display a '''prompt''' to the user before taking the '''input''' | * Display a '''prompt''' to the user before taking the '''input''' | ||
− | |||
− | |||
|- | |- | ||
Line 30: | Line 28: | ||
* '''Python 3.4.3 '''and | * '''Python 3.4.3 '''and | ||
* '''IPython 5.1.0''' | * '''IPython 5.1.0''' | ||
− | |||
− | |||
|- | |- | ||
Line 95: | Line 91: | ||
− | The | + | The '''statement print''' ''inside parentheses'' '''a''' prints the '''string''' itself. |
− | We can see the difference clearly when we use '''strings''' with | + | We can see the difference clearly when we use '''strings''' with new lines in them. |
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type, '''b = "A line \n New line" ''' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type, '''b = "A line \n New line" ''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Now type '''b '''''is equal to inside double quotes''''' A line | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Now type '''b '''''is equal to inside double quotes''''' A line backslash n New line''' |
|- | |- | ||
Line 112: | Line 108: | ||
− | As you can see, it just shows that '''b''' contains a '''newline | + | As you can see, it just shows that '''b''' contains a '''newline character'''. |
|- | |- | ||
Line 118: | Line 114: | ||
− | Highlight the output | + | Highlight the output. |
Line 125: | Line 121: | ||
− | It prints the '''string, A line''' and then '''New line''' in the | + | It prints the '''string, A line''' and then '''New line''' in the next line. |
− | '''print''' | + | '''print statement''' in '''Python''' supports '''string formatting'''. |
Line 140: | Line 136: | ||
− | '''Percentage''' | + | '''Percentage string operator''' is called as '''format operator'''. |
For example: | For example: | ||
− | + | * '''Percentage d''' - specifies the '''integer format''' | |
− | * Percentage d - specifies the integer format | + | * '''Percentage s''' - specifies the '''string format''' and |
− | * Percentage s - specifies the string format and | + | * '''Percentage f''' - denotes the '''float format''' |
− | * Percentage f - denotes the float format | + | |
− | + | ||
− | + | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch back to the terminal. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch back to the '''terminal'''. |
|- | |- | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type x = 1. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type x = 1.56789 |
print('The value of x is %3.2f' %x) | print('The value of x is %3.2f' %x) | ||
Highlight %3.2f | Highlight %3.2f | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Next let us assign x as shown. |
Line 168: | Line 161: | ||
− | Here '' | + | Here '''''percentage'' 3.2f''' specifies the output in '''float''' and '''.2f''' rounds off the value to two decimal point. |
|- | |- | ||
Line 175: | Line 168: | ||
Highlight .4f | Highlight .4f | ||
− | |||
− | |||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| We will see one more example. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| We will see one more example. | ||
Line 195: | Line 186: | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type, '''z = "red"''' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type, '''z = "red"''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| '''z '''''is equal to | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| '''z '''''is equal to inside double quotes '''''red''' |
|- | |- | ||
Line 215: | Line 206: | ||
The values of x, y and z are substituted in place of the '''format specifiers''' as | 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. | respectively. | ||
Line 239: | Line 230: | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch the terminal | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch the terminal | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to the terminal for the solution. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to the '''terminal''' for the solution. |
|- | |- | ||
Line 267: | Line 258: | ||
print ("World") | print ("World") | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Usually '''print''' | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Usually '''print statement''' prints the output in a new line. |
− | Now we will see how to suppress the newline character. | + | Now we will see how to suppress the '''newline''' character. |
Line 285: | Line 276: | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to the terminal | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to the terminal | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch back to the terminal. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch back to the '''terminal'''. |
|- | |- | ||
Line 292: | Line 283: | ||
Highlight space in between '''Hello''' and '''world''' | Highlight space in between '''Hello''' and '''world''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Let us run the code by typing | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Let us run the code by typing '''percentage run '''space''' print '''''underscore''''' example.py ''' |
− | We can see that the '''print''' | + | We can see that the '''print statement''' prints a '''space''' instead of a new line. |
Line 305: | Line 296: | ||
− | We will use the '''input ''' | + | We will use the '''input function''' for this. |
Line 331: | Line 322: | ||
− | We can see that it contains the | + | We can see that it contains the '''string - an input''' |
|- | |- | ||
Line 344: | Line 335: | ||
− | Enter the number 5.6 as input and store it in a variable '''c'''. | + | Enter the number 5.6 as '''input''' and store it in a variable '''c'''. |
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to terminal | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to terminal | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch back to the | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch back to the '''terminal''' for the solution. |
|- | |- | ||
Line 359: | Line 350: | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Enter '''5.6''' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Enter '''5.6''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Enter 5.6 as input'''. ''' | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Enter 5.6 as '''input'''. Press '''Enter'''. |
|- | |- | ||
Line 373: | Line 364: | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Now let us see the | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Now let us see the '''data type''' of '''c'''. |
Line 379: | Line 370: | ||
− | We see that '''c''' is a string. | + | We see that '''c''' is a '''string'''. |
− | Because '''input''' command always takes the input as string no matter whatever is the input. | + | Because '''input''' command always takes the '''input''' as '''string''' no matter whatever is the '''input'''. |
|- | |- | ||
Line 395: | Line 386: | ||
− | Execute the below statement. | + | Execute the below '''statement'''. |
− | What happens when you do not enter anything and hit Enter? | + | What happens when you do not enter anything and hit '''Enter'''? |
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to terminal | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to terminal | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch back to the terminal for the solution. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch back to the '''terminal''' for the solution. |
|- | |- | ||
Line 412: | Line 403: | ||
− | Press '''Enter''' without giving any input | + | Press '''Enter''' without giving any '''input'''. |
+ | |||
+ | Again press '''Enter''' to get the '''prompt'''. | ||
|- | |- | ||
Line 419: | Line 412: | ||
Highlight the output | Highlight the output | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| To see the input value, type, '''d''' | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| To see the '''input''' value, type, '''d''' |
Line 432: | Line 425: | ||
− | Now we will give a prompt to get the input. | + | 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 ''''' | + | Type '''ip '''''is equal to''''' input '''''inside parentheses inside double quotes '''''Please enter a number '''''backslash''''' n''' |
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Enter 12 | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Enter 12 | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| I will give 12 as input. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| I will give 12 as '''input'''. |
− | This is how we can display a prompt to get input from user. | + | This is how we can display a '''prompt''' to get '''input''' from user. |
|- | |- | ||
Line 454: | Line 447: | ||
− | In this tutorial, we have learnt to, | + | In this tutorial, we have learnt to, |
− | + | * Use '''print statement''' | |
− | * Use '''print''' | + | * Use the '''format specifiers ''percentage ''d, ''percentage ''f '''and''' ''percentage ''s''' in the '''print statement ''' |
− | * Use the '''format specifiers | + | * Take '''input''' from user by using''' input function''' and |
− | * Take input from user by using''' input ''' | + | |
* Display a '''prompt''' to the user before taking the '''input''' | * Display a '''prompt''' to the user before taking the '''input''' | ||
− | |||
− | |||
|- | |- | ||
Line 474: | Line 464: | ||
− | # '''a | + | # '''a ''is equal to'' input ''open and close parentheses''''' and user enters '''2.5'''. What is the '''type''' of '''a'''? |
− | # If '''a | + | # If '''a ''is equal to'' 2 '''and '''b ''is equal to'' 4.5''', what is the result of the following action? |
− | + | ||
− | + | ||
|- | |- | ||
Line 487: | Line 475: | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| And the answers, | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| And the answers, |
− | + | ||
− | + | ||
− | + | ||
+ | # No matter what you enter, it will be taken as a '''string'''. Hence '''2.5''' is a '''string'''. | ||
+ | # 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''' | ||
|- | |- |
Latest revision as of 11:10, 28 May 2018
|
|
Show Slide Title | Welcome to the spoken tutorial on Input and Output. |
Show Slide
Learning Objectives |
In this tutorial, we will learn to
|
Show Slide
System Specifications |
To record this tutorial, I am using
|
Show Slide
Pre-requisite slide |
To practise this tutorial,
If not, see the relevant Python tutorials on this website. |
[Terminal]
ipython3 |
Let us start ipython.
|
Type, a = "This is a string" | Let us start this tutorial with a string.
|
Type, a | To see the value of a, type, a |
Type, print (a)
|
Type, print inside parentheses a
|
Type, b = "A line \n New line" | Now type b is equal to inside double quotes A line backslash n New line |
Type, b
|
Type, b
|
Type, print (b)
|
Type, print inside parentheses b
|
Slide:
Formatting operations |
Next we shall look at different ways of outputting the data.
|
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.
|
print('The value of x is %3.4f' %x)
|
We will see one more example.
|
Type, y = 2 | Next, let us assign two more values.
|
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))
|
To print the value of x, y and z, type as shown.
respectively.
|
Show Slide
|
Pause the video.
|
Switch the terminal | Switch to the terminal for the solution. |
Type, print (''x is %d, y is %f'' %(x, y))
|
Type as shown.
|
Open text editor and type
print ("Hello") print ("World") print ("Hello",end=' ') print ("World") |
Usually print statement prints the output in a new line.
|
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
|
Let us run the code by typing percentage run space print underscore example.py
|
Type, ip = input() | Next we shall look at taking input from the user.
|
Point the input cursor
|
The cursor is blinking indicating that it is waiting for an input.
|
Type, ip
|
Now let us see what is the value of ip.
|
Show Slide
|
Pause the video.
|
Switch to terminal | Switch back to the terminal for the solution. |
Type, c = input() | We have to use the input command with variable c.
|
Enter 5.6 | Enter 5.6 as input. Press Enter. |
Type, c | To see the input value, type c |
Type, type(c)
|
Now let us see the data type of c.
|
Show Slide
|
Pause the video.
|
Switch to terminal | Switch back to the terminal for the solution. |
Type, d = input()
|
Type d is equal to input open and close parentheses
Again press Enter to get the prompt. |
Type, d
|
To see the input value, type, d
|
Type, ip = input("Please enter a number\n ") | We can also use input to display a prompt to assist the user.
|
Enter 12 | I will give 12 as input.
|
Show Slide
Summary slide
|
This brings us to the end of this tutorial. Let us summarise.
|
Show Slide
Evaluation
|
Here are some self assessment questions for you to solve
|
Show Slide
|
And the answers,
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.
|
Show Slide
Thank You |
This is Priya from IIT Bombay signing off.
|