Keywords

  • The print function enables a Python program to display textual information to the user.

  • Programs may use the input function to obtain information from the user.

  • The simplest use of the input function assigns a string to a variable:

    • x = input()



  • The parentheses are empty because, the input function does not require any information to do its job. Below program demonstrates that the input function produces a string value.


Shell Exercies


    >>>x = input()
    #enter text
    'hi welcome to Ravula's Tutorials'
    #prints value of variable x
    >>>print(x)
    hi welcome to Ravula's Tutorials


eval function

  • The eval function dynamically translates the text provided by the user into an executable form that the program can process

  • This allows users to provide input in a variety of flexible ways; for example, users can enter multiple entries separated by commas, and the eval function evaluates it as a Python tuple


>>>num1, num2 = eval(input('Please enter number 1, number 2: '))
>>>print(num1, '+', num2, '=', num1 + num2)

    The following sample run shows how the user now must enter the two numbers at the same time separated by a comma:


    Please enter number 1, number 2: 23, 10
    23 + 10 = 33


Type Conversion in Python


  • Casting is when you convert a variable value from one type to another
  • In python this is done with functions such as int() or float() or str(). A very common pattern is that you convert a number, currently as a string into a proper number.

    • >>>x=input()
      3
      >>>print(type(x))
      class 'str'

      >>>x=int(input())
      3
      >>>print(type(x))
      class 'int'


  1. int(a,base) : This function converts any data type to integer. ‘Base’ specifies the base in which string is if data type is string.


  2. # initializing string 
    >>>s = "10010"
    # printing string converting to int base 2
    >>>c = int(s,2)
    >>>print ("After converting to integer base 2 : ", end="")
    >>>print (c)

      After converting to integer base 2 : 18

  3. float() : This function is used to convert any data type to a floating point number.


  4. # initializing string 
    >>>s = "10010"
    # printing string converting to float
    >>>e = float(s)
    >>>print ("After converting to float : ", end="")
    >>>print (e)

      After converting to float : 10010.0

  5. ord() : This function is used to convert a character to integer.


  6. # initializing string 
    >>>s = '4'
    # printing character converting to integer
    >>c = ord(s)
    >>>print ("After converting character to integer : ",end="")
    >>>print (c)

      After converting character to integer : 52

  7. hex() : This function is to convert integer to hexadecimal string.


  8. # initializing string 
    >>>s = "4"
    # printing integer converting to hexadecimal string
    >>>c = hex(56)
    >>>print ("After converting 56 to hexadecimal string : ",end="")
    >>>print (c)

      After converting 56 to hexadecimal string : 0x38

  9. oct() : This function is to convert integer to octal string.


  10. # initializing string 
    >>>s = "10010"
    # printing integer converting to octal string
    >>>c = oct(56)
    >>>print ("After converting 56 to octal string : ",end="")
    >>>print (c)

      After converting 56 to octal string : 0o70

  11. tuple() : This function is used to convert to a tuple.


  12. # initializing string 
    >>>s = 'geeks'
    # printing string converting to tuple
    >>>c = tuple(s) )
    >>>print ("After converting string to tuple : ",end="")
    >>>print (c)

      After converting string to tuple : ('g', 'e', 'e', 'k', 's')

  13. set() : This function returns the type after converting to set.


  14. # initializing string 
    >>>s = 'geeks'
    # printing string converting to set
    >>>c = set(s)
    >>>print ("After converting string to set : ",end="")
    >>>print (c)

      After converting string to set : {'k', 'e', 's', 'g'}

  15. list() : This function is used to convert any data type to a list type.


  16. # initializing string 
    >>>s = 'geeks'
    # printing string converting to list
    >>>c = list(s)
    >>>print ("After converting string to list : ",end="")
    >>>print (c)

      After converting string to list : ['g', 'e', 'e', 'k', 's']

  17. dict() : This function is used to convert a tuple of order (key,value) into a dictionary.


  18. # initializing integers  
    >>>a,b=1,2
    # initializing tuple
    >>>tup = (('a', 1) ,('f', 2), ('g', 3))
    # printing integer converting to complex number
    >>>c = complex(1,2)
    >>>print ("After converting integer to complex number : ",end="")
    >>>print (c)

      After converting integer to complex number : (1+2j)

  19. str() : Used to convert integer into a string.


  20. # initializing integers  
    >>>a,b=1,2
    # initializing tuple
    >>>tup = (('a', 1) ,('f', 2), ('g', 3))
    # printing integer converting to string
    >>>c = str(a)
    >>>print ("After converting integer to string : ",end="")
    >>>print (c)

      After converting integer to string : 1

  21. complex(real,imag) : This function converts real numbers to complex(real,imag) number.


  22. # initializing integers  
    >>>a,b=1,2
    # initializing tuple
    >>>tup = (('a', 1) ,('f', 2), ('g', 3))
    # printing tuple converting to expression dictionary
    >>>c = dict(tup)
    >>>print ("After converting tuple to dictionary : ",end="")
    >>>print (c)

      After converting tuple to dictionary : {'a': 1, 'f': 2, 'g': 3}