1| 2| 3| 4| 5| 6| 7| 8| 9| 10
Page-3
Input and Output
To Make the Program interactive for the users, it is necessary to make it possible that a program takes User Input and works accordingly.
For this purpose, we use the input() Function in Python. The Syntax for the input function is -
input([prompt])
Prompt is optional and is used to print a string before the user enters a value.
Example-
input("enter a number")
In this example, the string "enter a number" is a prompt that is displayed before the user enters a number.
if we don't use this prompt, the program will run flawlessly but the user will not see any message like this.
We need to save the value entered by the user in a variable. So, we will write a variable on the left-hand side of the input() function.
Example-
num1=input("enter a number")
Also, input() function considers all input from the user as a string, so, we should use be careful while using it.
In the above example, the num1 variable will contain the string entered by the user. if the user enters number 2, it will be converted into a string and will be saved in variable num1.
Further, we should explicitly convert the num1 into a number to do some calculations.
type() Function
type() function returns the type of the value passed to it.
Example-
print(type("hello"))
Output-
<class 'str'>
Example-
print(type(2.0))
Output-
<class 'float'>
No comments:
Post a Comment