Friday, March 5, 2010

Getting Started

Program Output, the print Statement, and "Hello World!"

In some languages, such as C, displaying to the screen is accomplished with a function, e.g., printf(), while with Python and most interpreted and scripting languages, it is a statement. Many shell script languages use an echo command for program output.

In the following example, we assign a string variable, then use print to display its contents. Following that, we issue just the variable name.



Python's print statement, paired with the string format operator ( % ), supports string substitution, much like the printf() function in C:




Program Input and the raw_input()Built-in Function


The easiest way to obtain user input from the command line is with the raw_input() built-in function. It reads from standard input and assigns the string value to the variable you designate. You can use the int() built-in function to convert any numeric input string to an integer representation.




The earlier example was strictly for text input. A numeric string input (with conversion to a real integer) example follows below:

No comments:

Post a Comment