Python- Input function

Good Morning Boys.

Welcome to the fourth session of the online classes for Python Programing.
------------------------------------------------------------------------------------------------------------------------------

By the end of this session, you will be able to understand 

  • the purpose and syntax of the input( ) function
  • working of the input( ) function                 
  • Understand and apply the concept of TYPECASTING.
---------------------------------------------------------------------------------------------------------------------------------

Lets see whether the following terms are clear to you or not ?
1. Identifier
2. Comment
  • inline comment
  • multiline comment
Doubts if any, must be shared now. I am waiting.🙏 

_______________________________________________________________  

Having revised yesterday's content. Lets move ahead.
The topic of the discussion today is the INPUT function in PYTHON- input( )

Taking input in Python

Developers often have a need to interact with users, either to get data or to provide some sort of result. Most programs today use a dialog box as a way of asking the user to provide some type of input. While Python provides us with two inbuilt functions to read the input from the keyboard.

  • raw_input ( prompt )
  • input ( prompt )

We will limit our discussion to INPUT( ) 

Syntax

input(prompt)

Parameter Values

ParameterDescription
promptA String, representing a default message before the input.

More Examples

Example

Use the prompt parameter to write a message before the input:
x = input('Enter your name:')
print('Hello, ' + x)
If the user enters "Geeta" as the name, the OUTPUT will be: 
Enter your name:

Hello, Geeta





input ( ) : This function first takes the input from the user and then evaluates the expression, which means Python automatically identifies whether user entered a string or a number or list. 
If the input provided is not correct then either syntax error or exception is raised by python. For example –

# Python program showing 
# a use of input()
  
val = input("Enter your value: ")
print(val)
Output:


How the input function works in Python :
  • When input() function executes program flow will be stopped until the user has given an input.
  • The text or message display on the output screen to ask a user to enter input value is optional i.e. the prompt, will be printed on the screen is optional.
  • Whatever you enter as input, input function convert it into a string. if you enter an integer value still input() function convert it into a string. You need to explicitly convert it into an integer in your code using typecasting.
 TYPECASTINGThe process of converting the value of one data type (integer, string, float, etc.) to another data type is called TYPECASTING

Example 
n=int(input("Enter your age"))

Here 
--> the user is shown the message -"Enter your age" and is prompted to enter value for age. 
--> Whatever he enters is typecasted  to integer.

EXERCISE
Write statements in PYTHON to do the following:
1. Accept a number from the user . Display an appropriate message .
2. Accept a name from the user . Display an appropriate message .
3. Accept the name and age of the user. Display appropriate messages.
4. Accept two numbers from the user. Display appropriate messages before taking the input.
5. Accept a number from the user and typecast it to float.

Comments

  1. Good Morning boys. Hope all is well. I want to take doubts of previous sessions. Kindly share.

    ReplyDelete
  2. Are there any doubts with the previous sessions?

    ReplyDelete
  3. by the end of today's session, I want all of you to give the value for x in the following input statement:
    a=input("Enter a number ") #suppose user enters 5.0
    b=input("Enter a number ") #suppose user enters 2.0
    x=int(a/b)
    print(x)

    ReplyDelete
  4. If anyone having problem, can we connect on Zoom call?

    ReplyDelete
  5. pls mark your attendance as you enter the class

    ReplyDelete
  6. Ma'am, do we have to start writing from the topic SYNTAX??
    -Geet

    ReplyDelete
  7. Remember, all notes to be taken down in the CA registers

    ReplyDelete
  8. My blog is available beyond class hours also. You can take a review whenever you want and ensure you are able to understand the topic.Queries can be posted later also

    ReplyDelete
  9. I have posted a question in the comment section. I expect an answer for that after you have concluded the topic. I am waiting

    ReplyDelete
  10. Replies
    1. thanks. I will share the answer when others also have shared their answer

      Delete
  11. Ma'am i've finished my work
    -Geet

    ReplyDelete
  12. Pls note that a=5.0 and b=2.0 . on division the result will be 2.5 but \because of typecasting (int(a/b)), 2.5 which is float will be typecasted to integer, that is 2

    So the answer is 2

    ReplyDelete
  13. I hope you have understood the explanation.

    ReplyDelete

Post a Comment

Popular posts from this blog

HOLIDAY HW