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

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.
主站蜘蛛池模板: 江油市| 和田市| 湖州市| 吉隆县| 大关县| 曲水县| 乡城县| 德庆县| 垦利县| 铁力市| 思茅市| 吉林省| 台东市| 盐源县| 灵石县| 色达县| 固原市| 红安县| 游戏| 桑植县| 龙南县| 化德县| 孝感市| 双流县| 文水县| 屯昌县| 礼泉县| 门源| 桦川县| 云霄县| 东乡县| 太谷县| 承德市| 乌拉特中旗| 乐清市| 汉源县| 重庆市| 隆回县| 合山市| 铜陵市| 嫩江县|