Print,Input syntax in python and Examples

input():
In python it is used to assign data for variables it is like scanf in c
a=input()
 print(a)

Output:
>>>5
>>>'5'

It will take input as string by default

To take it as integer we want to convert string to integer (explict function)
For Example:
a=int(input())
print(a)

Output:

>>>5
>>>5

In python 3x input syntax will be :
                                                              input()

In python 3x input syntax will be :
                                                             1.raw_input()
                                                             2.input()
In Python 2xraw_input() returns a string, and input() tries to run the input as a Python expression.

In Python 2.7 Examples:-

  1. >>> val = input("Enter any value: ")
  2. Enter any value: 7
  3. >>> type(val)
  4. int
  5.  
  6. >>> val = input("Enter any value: ")
  7. Enter any value: 7.0
  8. >>> type(val)
  9. float
  10.  
  11. >>> val = input("Enter any value: ")
  12. Enter any value: 'abc'
  13. >>> type(val)
  14. str
  15.  
  16. >>> val = input("Enter any value: ")
  17. Enter any value: True
  18. >>> type(val)
  19. bool
  20.  
  21. >>> val = input("Enter any value: ")
  22. Enter any value: [1,2,3,4,5]
  23. >>> type(val)
  24. list

Comments

Popular posts from this blog

To print python keywords in a list