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

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.
主站蜘蛛池模板: 永寿县| 河曲县| 冷水江市| 视频| 安庆市| 开江县| 色达县| 林州市| 鄂尔多斯市| 乌兰浩特市| 客服| 壤塘县| 古交市| 桐梓县| 阿拉善左旗| 双牌县| 云梦县| 托里县| 平山县| 红原县| 文安县| 上饶县| 拉孜县| 本溪市| 永新县| 崇左市| 岚皋县| 镇巴县| 汤原县| 泗水县| 会理县| 寻甸| 兴安盟| 南乐县| 鹤峰县| 潞西市| 西吉县| 顺义区| 湖州市| 塔河县| 库车县|