Python Input ------------ Updated: Thursday 5th February 2009, 7:53 PT, AHD Input means getting data into your program from an input device - usually the keyboard. Python needs a place to store whatever value is input. These storage places are known as variables, and have to have a name. In the Python statement below, the variable's name is thetext. thetext = raw_input("Enter some text ") The above Python statement, when executed (i.e. run) will output to the screen whatever is within the quote marks. This text is sometimes refered to as a "prompt". Python then waits for you to enter something from the keyboard. Type in some text and then press the Enter key on the keyboard. Whatever you typed in before you hit the Enter key is stored into the variable called thetext. thetext is the variable where the input data is stored. Future lines in the program can use this variable. The Assignment symbol = ----------------------- the = symbol in the line: thetext = raw_input("Enter some text ") is known as the assignment symbol. The purpose of the assignment symbol is to place whatever you typed in, into the variable on its left - i.e. thetext. The raw_input function ---------------------- the raw_input( ) in the line: thetext = raw_input("Enter some text ") is known as a built-in function. We study functions later in the course: http://www.annedawson.net/Python_Functions.htm but for now, just remember that the raw_input function allows you to get input to your program. ********************************************************************************** BUT, and a BIG BUT, the raw_input function always inputs TEXT (i.e. data type String). So if you want to input a number, it's done in two steps: ********************************************************************************** 1. Input the number as text: anumber = raw_input("Enter a number ") 2. Convert the text to a number: anumber = float(anumber) This converts the entered text to a floating point number (e.g. 3.142 or 2.75) or anumber = int(anumber) This converts the entered text to an integer (whole number) (e.g. 47 or 22) You can combine the two operations (input and data conversion) into one line, e.g.: anumber = int(raw_input("Enter a number: ")) Built-in functions ------------------ raw_input(), int() and float() are built-in Python functions. They can be used at any time in any Python statement. There are many more built-in functions. This is the subject of a future class: http://www.annedawson.net/Python_Functions.htm Strings ------- str() is a built-in function that converts a numeric value (or numeric variable) into text. In programming, text values are also called "string" values, hence the str() function's name. You will later be using some math built-in functions such as sqrt() to find the square root of a number: http://www.annedawson.net/Python_Functions.htm Data types of input values -------------------------- You can always check the data type of any variable by using the type() built-in function: see program 03-12.py in http://www.annedawson.net/PythonPrograms.txt #03-12.py a = "10" b = '99' c = a + b print c print type(c) c = int(c) print c print type(c) In common with most programming languages, Python allows you to create your own functions. This is the subject of a future class: http://www.annedawson.net/Python_Functions.htm