Python Getting Started notes (2) _ Data types and rounding operations Round,floor,ceil__python

Source: Internet
Author: User


Data Type


1. Data types include integers, floating-point numbers, and strings, which we all know are not much to say about definitions here.

However, there are two more words to say about integer floating-point computations. First look at the following calculation

11/4    # ==> 2
11.0/4    # ==> 2.75

Strangely enough, Python also outputs an integer to the result of an endless integer calculation.

Well, ask what the results of Python 0.5 + 10/4 are. Of course it's 5.0. OK, wrong ...

When calculating 0.5 + 10/4, Python first calculates 10/4 by priority, because the two numbers are integers, so the result is 2.

Then, proceed to calculate 0.5 + 2, get the final calculation result 2.5.

Therefore, to get the correct answer, the formula should be written 0.5 + 10.0/4!!!

2. The other is to talk about Boolean values.

The Boolean value is exactly the same as the representation of the Boolean algebra, a Boolean value of True, false two, or true, or false, in Python, you can directly use True, False to represent a Boolean value (note case), or you can compute it by Boolean operations.

Boolean values can be evaluated with and, or and not. These meanings are very simple, that is, with, or, not.

3. Null value

A null value is a special value in Python, represented by None. None is not understood to be 0, because 0 is meaningful, and none is a special null value. In addition, Python provides a list, a dictionary, and many other data types, and also allows you to create custom data types.

"Here's what to say:"

(1) Python regards 0, empty string ' and None as False, and other values and non-null strings are treated as True.

(2) in the calculation of A and B, if a is false, then the entire result must be false according to the algorithm, and therefore returns a; If A is True, the entire calculation must depend on B and therefore returns B.

In the case of a or B, if a is true, the entire calculation must be true according to or the algorithm, and therefore A; If a is False, the entire calculation will necessarily depend on B, so return B.

Well, according to the above, what the following code output should be.


(Print statement You should be familiar with it, you can output the specified text to the screen)

Yes, Hello python and Hello, world.


Rounding Operation Round,floor,ceil

Round can be invoked directly under Python, while floor and ceil need to load the math library first.

On the difference between the three, round away from the 0,floor always to the small direction of transformation, Ceil always to the big direction of transformation.

From math import *

print round (4.1), round (4.5), round (4.9)
print round ( -4.1), round ( -4.5), round ( -4.9)

Print floor (4.1), floor (4.5), floor (4.9)
print Floor ( -4.1), Floor ( -4.5), Floor ( -4.9)

print ceil (4.1), Ceil (4.5 ), Ceil (4.9)
print ceil ( -4.1), Ceil ( -4.5), ceil (-4.9)
Run Result:

4.0 5.0 5.0
-4.0-5.0-5.0
4.0 4.0 4.0
-5.0-5.0-5.0
5.0 5.0 5.0
-4.0-4.0-4.0





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.