13
1.2 Core Python
The majority of mathematical functions are available in the math module.
Reading Input The intrinsic function for accepting user input is
raw input(prompt )
It displays the prompt and then reads a line of input which is converted to a string. To convert the string into a numerical value use the function
eval(string )
The following program illustrates the use of these functions: a = raw_ input(’Input a: ’) print a, type(a)
# Print a and its type
b = eval(a) print b,type(b)
# Print b and its type
The function type(a) returns the type of the object a; it is a very useful tool in debugging. The program was run twice with the following results:
Input a: 10.0 10.0 <type ’str’> 10.0 <type ’float’>
Input a: 11**2 11**2 <type ’str’> 121 <type ’int’>
A convenient way to input a number and assign it to the variable a is
a = eval(raw input(prompt))