I start Python Programming Language in these days, when i was trying to understand them
i come across to these problems so i will be so happy if you help me in :

1)

>>> # Integer division returns the floor:
... 7/3
2
>>> 7/-3
-3

I can't understand this becuase as normal 7/3 is not 3 or 7/-3 is not -3.

2)
In here :

>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_, 2)
113.06

I don't know this : (price+_) and also [ round(_,2)]

Recommended Answers

All 2 Replies

Hi jamshid,

In the first example, keep in mind that when you say "7/3" in Python, you are telling it to divide the integer 7 by the integer 3. Python, like many programming languages, makes a distinction between integers and real numbers, so that any operation carried out on integers results in another integer. The reasons for this are a little too complicated to get into; for now, just remember that doing integer division will give you a "floored" answer. Flooring a real number means this: if we have some number, like 2.3, which is between two integers - here, 2 and 3 - the floor operation takes the smaller of the two numbers: 2. Similarly, if we have a negative number, like -2.3, which is between two integers - here, -2 and -3 - the floor operation again takes the smaller (the more negative) of the two numbers: -3. Does that make sense? In order to get it to give you the unfloored answer, you can use the float() casting function, like so:

>> float(7)/3
2.3333333333333335

That will give you the answer you want.

For the second part, are you confused about what the underscore (_) means? That is just a variable which contains the last answer on the command prompt. So when you say something like

>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625

- on the last line, you are really saying "add the value of the variable 'price' and the value of the last computation, which is 12.5625." Every time you do a new computation, the value of the _ variable changes, so that after you do price+_ the value of _ is 113.0625. In the next line, you are rounding off the value of _ (the 113.0625) to the second decimal place - that is what the significance of the 2 is in round(_,2). This is why the answer is 113.06. If you were to print _ after that result, you would get an answer of 113.06 - is it clear?

Hope I have been able to help.

Really thanks G-Do for your nice explanations and help. I got all :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.