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

Dynamic versus static

Python is a dynamic typed language. Many other languages are static typed, such as C/C++ and Java. A static typed language requires the programmer to explicitly tell the computer what type of "thing" each data construct is.

For example, if you were writing a C inventory program and one of the variables was cost, you would have to declare cost as a float type, which tells the C compiler that the only data that can be used for that variable must be a floating point number, that is, a number with a decimal point, such as 3.14. If any other data type was assigned to that variable, like an integer or a text string, the compiler would give an error when trying to compile the program. (A programming variable is similar to a math variable; it's just a placeholder for a particular value.)

Python, however, doesn't require this. You simply give your variables names and assign values to them. The interpreter takes care of keeping track of the kinds of objects your program is using. This also means that you can change the size of the values as you develop the program. (For the curious, this is handled by adding metadata to a C construct. Every time the item is used, the metadata is looked at to determine how Python should interact with it, as well as potentially modifying the metadata.)

Say you have another decimal number you need in your program. With a static typed language, you have to decide the memory size the variable can take when you first initialize that variable. A double is a floating point value that can handle a much larger number than a normal float (the actual amount of memory used depends on the operating environment, but a float is typically 32 bits long while a double is 64 bits). If you declare a variable to be a float but later on assign a value that is too big to it, your program can develop errors or be slower than expected; changing it to a double will correct these problems.

With Python, it doesn't matter what type of data a construct is. You simply give it whatever number you want, and Python will take care of manipulating it as needed. It even works for derived values. For example, say you are dividing two numbers. One is a floating point number and one is an integer. Python realizes that it's more accurate to keep track of decimals so it automatically calculates the result as a floating point number. The following code example shows what it would look like in the Python interpreter—floating point and integer division:

>>> 6.0 / 2 
3.0
>>> 6 / 2.0
3.0

As you can see, it doesn't matter which value is the numerator or denominator; Python "sees" that a float is being used and gives the output as a decimal value.

This would be a good time to note one of the differences between Python 2 and Python 3. Python 2 truncates division operations, whereas Python 3 automatically converts to decimal values. The following section offers examples of the two versions.

主站蜘蛛池模板: 科技| 正蓝旗| 商洛市| 富阳市| 紫云| 荆州市| 汉源县| 深水埗区| 友谊县| 沁阳市| 罗山县| 寿阳县| 金寨县| 兴海县| 米易县| 阿鲁科尔沁旗| 台中县| 永新县| 马关县| 健康| 岚皋县| 老河口市| 慈溪市| 陕西省| 上犹县| 阜新市| 陈巴尔虎旗| 南昌县| 龙州县| 克东县| 安丘市| 鹤壁市| 吉林市| 桃园县| 天全县| 和林格尔县| 嘉定区| 肃南| 台山市| 迭部县| 龙里县|