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

Why integers? 

It may seem peculiar at first to distinguish between int and float as fundamentally different data types. The first reason to make the distinction is that computers store these numbers differently and, for the sake of computer programming, there are a couple of scenarios in which it makes more sense for a programming language to use one or the other. For example, as we will see later in this chapter, using a float to index an array will cause an error and thus prevent the program from running.

If you find yourself using Python 2, you may notice that the interpreter treats arithmetic differently for integers than it does for floats, rounding the results, which can lead to some unexpected errors.

You can add, divide, and multiply in a single statement, using the same rules you would expect in a scientific calculator. The order of operations is as follows:

  1. Expressions inside parentheses.
  2. Exponents.
  3. Division.
  4. Multiplication.
  5. Addition and subtraction.

The following commands are some examples of arithmetic in Python:

>> 2 * 3
>> 4 + 5 / 6
>> (4 + 5) / 6
>> ( ( 1 * 2 ) / 3 / 4 ) + 5 - (6 / 5)

Exponents in Python are denoted using two consecutive multiplication ** symbols. Careful not to use the ^ symbol for exponentiation in Python. The following is an example of arithmetic in python:

>> 3 ** 2
Implicit conversion: You might have noticed that all of the previous operations use integers, but sometimes yield floats as results. This is because the Python interpreter changes the data type of the values just before the operation so that you get a result that makes intuitive sense. This is called   implicit conversion  and it is one of the things that makes Python so easy to use and beginner friendly. However, implicit conversion doesn't always happen, so it is important to keep track of the data types that you are using throughout your program.
主站蜘蛛池模板: 耒阳市| 黔东| 余江县| 建瓯市| 教育| 垦利县| 天等县| 宝坻区| 奉新县| 康定县| 偃师市| 新绛县| 西乌珠穆沁旗| 广安市| 伊宁市| 美姑县| 赣州市| 榆社县| 紫阳县| 河北区| 长白| 万全县| 准格尔旗| 晋中市| 金平| 沙田区| 永胜县| 长宁县| 哈尔滨市| 南皮县| 托克逊县| 子洲县| 莱州市| 乌鲁木齐县| 梁河县| 桐城市| 鸡西市| 斗六市| 鹤壁市| 玛多县| 铅山县|