Python: Input and Output Statements
This blog lets us learn about Python's input and output statements.
Output Statement:
Output statements are nothing but statements which are used to display the output or results of the program.
In this Python, we use the print() function to display the output to the user.
Syntax:
print("String",Variable1,Variable2,...)
comma(,) is used as an item separator in the print function.
Example:
print("Hello World!")
color = "Blue"
print("Color:",color)
Output:
Hello World!
Color: Blue
Input Statements:
Now, let us learn to get input from the user. Python uses the input() function to get input from the user.
Syntax:
variable = input("prompt")
Here input() function gets an argument known as a prompt, which is nothing but the message displayed to the user.
Example:
color = input("Enter your favorite color:")
print("Color: ",color)
Output:
Enter your favorite color:Blue
Color: Blue
Integer Input:
To get an integer input from the user we int() function,
why do we need to use the int() function?
because the input function returns parameters in string data type, so we need to convert them into integer datatype for doing arithmetic operations in it.
Example:
a = int(input("Enter a number:"))
b = int(input("Enter a number:"))
print(a+b)
Output:
Enter a number:12
Enter a number:14
26
You can also use the float() function instead of the int() function to get float-type inputs.
Tags:
Python
Hi coders, Learn about input and output statements from the above post.
ReplyDelete