官术网_书友最值得收藏!

The print function

I've been taking it for granted so far that the Python shell automatically prints the result of any statement that is entered. However, if you want to see the result of a particular operation inside a Python program, you will need to explicitly print it using the print() function. To use the print() function, you can type print() with the value that you would like to print inside parentheses.

Here are some examples using the print() function:

or example:>> print(0)
>> print(1+1)
>> print('abc')
>> print("abc" == "cba")

The syntax used with the print function may seem a bit strange right now, but it will become more clear when I introduce functions later in the chapter. 

There are two areas that the print() function comes in handy. The first of these is to test and debug programs. In order to see whether your program is working properly, or why it is not working properly, it is helpful to look inside the program while it is running. You can do this by placing print() functions in the program where you want to investigate what is happening. To give a really basic example, if a program that outputs a number produces the wrong number, one approach is to print the number at various stages of the program to identify where things go wrong. 

The second use of print() functions is to track the progress or status of a program. Processing large datasets can often take a really long time, so it can be helpful to have some sense of how far along the program is and how long it might take to finish. For example, when processing a large dataset row by row, it can be helpful to print out the fraction of rows that have been processed.

主站蜘蛛池模板: 房产| 弥渡县| 扎赉特旗| 中卫市| 石家庄市| 乌拉特前旗| 读书| 江源县| 宁武县| 台东县| 北川| 哈巴河县| 柏乡县| 湾仔区| 浮梁县| 集安市| 临汾市| 勐海县| 凤城市| 迭部县| 甘泉县| 永兴县| 阿荣旗| 田阳县| 威海市| 凉城县| 高要市| 台湾省| 吴旗县| 武汉市| 定州市| 饶平县| 富裕县| 德江县| 奉化市| 湄潭县| 温宿县| 扎兰屯市| 长汀县| 正阳县| 吉首市|