Python Division and Remainders

Use a double percent sign (%%) if the command is used inside a batch file.

Division Operators

In general, the data type of an expresion depends on the types of the arguments. This rule meets our expectations for most operators: when we add two integers, the result should be an integer. However, this doesn't work out well for division because there are two different expectations. Sometimes we expect division to create precise answers, usually the floating-point equivalents of fractions. Other times, we want a rounded-down integer result.

The classical Python definition of / depended entirely on the arguments. 685/252 was 2 because both arguments where integers. However, 685./252. was 2.7182539682539684 because the arguments were floating point.

This definition often caused problems for applications where data types were used that the author hadn't expected. For example, a simple program doing Celsius to Fahrenheit conversions will produce different answers depending on the input. If one user provides 18 and another provides 18.0, the answers were different, even though all of the inputs all had the equal numeric values.

>>> 

print 18*9/5+32

64
>>> 

print 18.0*9/5 + 32

64.4
>>> 

18 == 18.0

True
>>> 

This unexpected inaccuracy was generally due to the casual use of integers where floating-point numbers were more appropriate. (This can also occur using integers where complex numbers were implictly expected.) An explicit conversion function (like float( x )) can help prevent this. The idea, however, is for Python be a simple and sparse language, without a dense clutter of conversions to cover the rare case of an unexpected data type.

Starting with Python 2.2, a new division operator was added to clarify what was expectred. The ordinary / operator will, in the future, return floating-point results. A special division operator, //, will return rounded-down results. Generally, the new /operator is what most mathematical processing will use.

In additional two tools were made avialable that will end with Python 3.0. These tools will allow the division operator, /, to operate either under the old (classical, prior to version 2.2) rules or the new rules. This gives programmers a way to keep older applications running; it also gives them a way to explicitly declare that a program uses the newer operator definition. There are two parts to this: a statememt that can be placed in a program, as well as a command-line option that can be used when starting the Python interpreter.

Program Statements. To ease the transition from older to newer language features, there is a __future__ module available. This module includes a division definition that changes the definition of the / operator from classical to future. You can include the following from statement to state that your program depends on the future definition of division.

from __future__ import division
print 18*9/5+32
print 18*9//5+32

This produces the following output. The first line shows the new use of the / operator to produce floating point results, even if both arguments are integers. The second line shows the // operator, which produces rounded-down results.

64.4
64

The from __future__ statement will set the expectation that your script uses the new-style floating-point division operator. This allows you to start writing programs with version 2.2 that will work correctly with all future versions. By version 3.0, this import statement will no longer be necessary, and these will have to be removed from the few modules that used them.

Programmers should put the from __future__ statement in every Python script file, to be sure that what they are writing will work correctly. Eventually, these can be removed, but for the forseeable future, they are a necessary part of each Python script file.

Command Line Options. Another tool to ease the transition is a command-line option used when running the Python interpreter. This can force old-style interpretation of the / operator or to warn about old-style use of the / operator between integers. It can also force new-style use of the / operator and report on all potentially incorrect uses of the / operator.

The Python interpreter command-line option of -Q will force the / operator to be treated classically (“old”), or with the future (“new”) semantics. If you run Python with -Qold, the / operator's result depends on the arguments. If you run Python with -Qnew, the / operator's result will be floating point. In either case, the // operator returns a rounded-down integer result.

You can use -Qold to force old modules and programs to work with version 2.2 and higher. When Python 3.0 is released, however, this transition will no longer be supported; by that time you should have fixed your programs and modules.

To make fixing easier, the -Q command-line option can take two other values: warn and warnall. If you use -Qwarn, then the /operator applied to integer arguments will generate a run-time warning. This will allow you to find and fix situations where the // operator might be more appropriate. If you use -Qwarnall, then all instances of the / operator generate a warning; this will give you a close look at your programs.

You can include the command line option when you run the Python interpreter. For Linux and MacOS users, you can also put this on the #! line at the beginning of your script file.

#!/usr/local/bin/python -Qnew

1.4.1. Addition and Subtraction

We start with the integers and integer arithmetic, not because arithmetic is exciting, but because the symbolism should be mostly familiar. Of course arithmetic is important in many cases, but Python is probably more often used to manipulate text and other sorts of data, as in the sample program in ref:Running-A-Sample.

Python understands numbers and standard arithmetic. For the whole section on integer arithmetic, where you see a set-off line in typewriter font, type individual lines at the >>> prompt in the Python Shell. Press Enter after each line to get Python to respond:

77
2 + 3
5 - 7

Python should evaluate and print back the value of each expression. Of course the first one does not require any calculation. It appears that the shell just echoes back what you printed.

The Python Shell is an interactive interpreter. As you can see, after you press Enter, it is evaluating the expression you typed in, and then printing the result automatically. This is a very handy environment to check out simple Python syntax and get instant feedback. For more elaborate programs that you want to save, we will switch to an Editor Window later.

1.4.2. Multiplication, Parentheses, and Precedence

Try in the Shell:

2 x 3

You should get your first syntax error. The x should have become highlighted, indicating the location where the Python interpreter discovered that it cannot understand you: Python does not use x for multiplication as you may have done in grade school. The x can be confused with the use of x as a variable (more on that later).

Instead the symbol for multiplication is an asterisk *. Enter each of the following. You may include spaces or not. The Python interpreter can figure out what you mean either way. Try in the Shell:

2*5
2 + 3 * 4

If you expected the last answer to be 20, think again: Python uses the normal precedence of arithmetic operations: Multiplications and divisions are done before addition and subtraction, unless there are parentheses. Try

(2+3)*4
2 * (4 - 1)

Now try the following in the Shell, exactly as written, followed by Enter, with no closing parenthesis:

5 * (2 + 3

Look carefully. There is no answer given at the left margin of the next line and no prompt >>> to start a new expression. If you are using Idle, the cursor has gone to the next line and has only indented slightly. Python is waiting for you to finish your expression. It is smart enough to know that opening parentheses are always followed by the same number of closing parentheses. The cursor is on a continuation line. Type just the matching close-parenthesis and Enter,

)

and you should finally see the expression evaluated. (In some versions of the Python interpreter, the interpreter puts ‘...’ at the beginning of a continuation line, rather than just indenting.)

Negation also works. Try in the Shell:

-(2 + 3)

1.4.3. Division and Remainders

If you think about it, you learned several ways to do division. Eventually you learned how to do division resulting in a decimal. Try in the Shell:

5/2
14/4

As you saw in the previous section, numbers with decimal points in them are of type float in Python. They are discussed more in Floats, Division, Mixed Types.

In the earliest grades you would say “14 divided by 4 is 3 with a remainder of 2”. The problem here is that the answer is in two parts, the integer quotient 3 and the remainder 2, and neither of these results is the same as the decimal result. Python has separate operations to generate each part. Python uses the doubled division symbol // for the operation that produces just the integer quotient, and introduces the symbol % for the operation of finding the remainder. Try each in the Shell:

14/4
14//4
14%4

Now predict and then try each of

23//5
23%5
20%5
6//8
6%8
6/8

Finding remainders will prove more useful than you might think in the future!



decimal

  常用词汇  
英 ['desɪml]     美 ['desɪml]   
  • adj.小数的;十进位的
  • n.小数



Noun:

  1. a proper fraction whose denominator is a power of 10

  2. a number in the decimal system

Adjective:
  1. numbered or proceeding by tens; based on ten;

    "the decimal system"

用作形容词 (adj.)
  1. The first two figures after the decimal point indicate tenths and hundredths respectively.
    小数点后的头两位数分别表示十分位和百分位。
  2. Most countries have a decimal currency.
    大多数国家使用十进制货币。
  3. There were only decimal math problem on the quiz.
    测验中只有十进位的数学题。
  4. Britain converted to a decimal currency system in 1971.
    英国于1971年改行十进制货币体系。
收起
用作名词 (n.)
  1. 0.3 is a decimal.
    0.
  2. What kind of decimal is called a repeating decimal ?
    谁能说说什么样的小数叫循环小数?

quotient

  扩展词汇  
Intelligence Quotient
英 ['kwəʊʃnt]     美 ['kwoʊʃnt]   
  • n.份额;[数]商
  • Noun:
    1. the ratio of two quantities to be divided

    2. the number obtained by division
    用作名词 (n.)
    1. He got a small quotient of his father's property.
      他得到了他父亲财产的一小部分。
    2. The result is the quotient of the two operands.
      结果是两个操作数的商。
    3. His intelligence quotient is very high.
      他的智商很高。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
校园失物招领系统管理系统按照操作主体分为管理员和用户。管理员的功能包括字典管理、论坛管理、公告信息管理、失物招领管理、失物认领管理、寻物启示管理、寻物认领管理、用户管理、管理员管理。用户的功能等。该系统采用了Mysql数据库,Java语言,Spring Boot框架等技术进行编程实现。 校园失物招领系统管理系统可以提高校园失物招领系统信息管理问题的解决效率,优化校园失物招领系统信息处理流程,保证校园失物招领系统信息数据的安全,它是一个非常可靠,非常安全的应用程序。 ,管理员权限操作的功能包括管理公告,管理校园失物招领系统信息,包括失物招领管理,培训管理,寻物启事管理,薪资管理等,可以管理公告。 失物招领管理界面,管理员在失物招领管理界面中可以对界面中显示,可以对失物招领信息的失物招领状态进行查看,可以添加新的失物招领信息等。寻物启事管理界面,管理员在寻物启事管理界面中查看寻物启事种类信息,寻物启事描述信息,新增寻物启事信息等。公告管理界面,管理员在公告管理界面中新增公告,可以删除公告。公告类型管理界面,管理员在公告类型管理界面查看公告的工作状态,可以对公告的数据进行导出,可以添加新公告的信息,可以编辑公告信息,删除公告信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值