Input Output Statements in Python

Input Statements in Python: We can read the variables by using python input() function as:
1) We can write any string/text to be displayed at the python prompt as an argument to the input function.
2) If we don’t specify any string/text to be displayed at the python prompt , then nothing will be displayed. But we can still type the value to be read.
3) We can assign the result of the input function to be stored in any variable. We can then use the variable anywhere.
4) If we don’t assign the result of the input function to any variable, then input() returns the inputted string as a result of the input()

>>># input() returns the inputted string as a result
>>># "enter the number" is displayed at prompt
>>> input("enter the number")
enter the number5
'5'

>>># result of input() stored in a variable named a
>>># "enter the number" is displayed at prompt
>>> a=input("enter the number")
enter the number5
>>> a
'5'

>>># result of input() stored in a variable named a
>>># Nothing is displayed at prompt , but we can still type
>>> a=input()
20
>>> a
'20'

>>># input() returns the inputted string as a result
>>># Nothing is displayed at prompt , but we can still type 
>>> input()
10
'10'

Note that the result of the input function is a string. We can use Explicit type casting to convert this string into another possible types.
We can convert strings into integers, only if the string contains only numeric characters.

>>> a=int(input("enter the string"))
enter the string23

>>> a=int(input("enter the string"))
enter the string23.6
Traceback (most recent call last):
  File "<pyshell#17>", line 1, in <module>
    a=int(input("enter the string"))
ValueError: invalid literal for int() with base 10: '23.6'

>>> a=int(input("enter the string"))
enter the string3+5j
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    a=int(input("enter the string"))
ValueError: invalid literal for int() with base 10: '3+5j'

>>> a=int(input("enter the string"))
enter the stringgargs academy
Traceback (most recent call last):
  File "<pyshell#18>", line 1, in <module>
    a=int(input("enter the string"))
ValueError: invalid literal for int() with base 10: 'gargs academy'

We can convert string into float, if the string is an int or a float type number.

>>> a=float(input("enter value"))
enter value10
>>> a
10.0

>>> a=float(input("enter value"))
enter value10.5
>>> a
10.5

>>> a=float(input("enter value"))
enter value2+4j
Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    a=float(input("enter value"))
ValueError: could not convert string to float: '2+4j'

>>> a=float(input("enter value"))
enter valuegargs academy
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    a=float(input("enter value"))
ValueError: could not convert string to float: 'gargs academy'

Output Statements in Python: We can print the strings/literals/variables by using python print() function as:
1) We can write any string/variable/literal to be printed at the python prompt as an argument to the output function.
2) If we don’t specify any string/text to be printed at the python prompt , then nothing will be printed.
3) print() function prints the string/variable/literal and returns None. We can assign the result of the output function i.e. None to be stored in any variable. We can then use the variable anywhere.
4) We can combine two or more string/variable/literals using a comma in print() function.
5) We can also use string concatenation operator + to add two or more strings.

>>>#nothing is printed
>>> print()

>>>#string "hello" is printed and result of print() i.e. None is stored in a variable a
>>> a=print("hello")
hello
>>> a
>>> print(a)
None

>>>#nothing is printed and result of print() i.e. None is stored in a variable a
>>> a=print()
>>> a
>>> print(a)
None

>>># literal 9467863365 is printed
>>> print(9467863365)
9467863365

>>># string "gargs academy" is printed.
>>> print("gargs academy")
gargs academy

>>># variable name is printed
>>> name="gargs academy"
>>> print(name)
gargs academy

>>> # string "gargs academy" is combined with literal 9467863365 using ,
>>> print("gargs academy" , 94678633665)
gargs academy 94678633665

>>> # string "gargs academy" is combined with string "9467863365" using string concatenation operator +
>>> print("gargs academy" + "94678633665")
gargs academy94678633665

>>> # string "gargs academy" is combined with string "9467863365" using ,
>>> print("gargs academy" , "94678633665")
gargs academy 94678633665

>>> #string "gargs academy" can't be combined with literal 9467863365 
>>># using string concatenation operator +
>>> print("gargs academy" + 94678633665)
Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    print("gargs academy" + 94678633665)
TypeError: can only concatenate str (not "int") to str
error: You can only copy the programs code and output from this website. You are not allowed to copy anything else.