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

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.
主站蜘蛛池模板: 房产| 肇源县| 衡水市| 和平区| 滨海县| 凌源市| 婺源县| 舟曲县| 攀枝花市| 盐山县| 斗六市| 民乐县| 靖远县| 胶州市| 桑日县| 昌乐县| 綦江县| 应用必备| 和龙市| 大安市| 玛多县| 根河市| 双牌县| 甘孜县| 宜兴市| 淮阳县| 洛川县| 孝义市| 突泉县| 朝阳市| 陵川县| 诸城市| 青龙| 应城市| 昭平县| 图木舒克市| 吴旗县| 永和县| 恩施市| 阿瓦提县| 绩溪县|