Get started with Paython (5) integer, floating point, Boolean, and paython Boolean

Source: Internet
Author: User

Get started with Paython (5) integer, floating point, Boolean, and paython Boolean

PythonInteger and floating point number

Python allows you to directly perform a four-character hybrid operation on integers and floating-point numbers. The operation rules are exactly the same as those in mathematics.

Basic operations:

1 + 2 + 3 # => 6

4*5-6 # => 14

7.5/8 + 2.1 #=> 3.0375

Brackets can be used to increase the priority, which is exactly the same as the mathematical operation. Note that parentheses can only be used, but parentheses can be nested in many layers:

(1 + 2) * 3 #=> 9

(2.2 + 3.3)/(1.5 * (9-0.3) # => 0.42145593869731807

Different from mathematical operations, Python's Integer Operation results are still integers and floating-point calculation results are still floating-point:

1 + 2 #=> integer 3

1.0 + 2.0 #=> floating point number 3.0

However, the result of the mixed integer and floating-point operations becomes a floating-point number:

1 + 2.0 #=> floating point number 3.0

Why do we need to distinguish between integer and floating-point operations? This is because the result of integer calculation is always accurate, and the result of floating point calculation is not necessarily accurate, because the computer memory is too large to accurately express the infinite loop decimal, for example, if 0.1 is changed to binary, it means an infinite repeating decimal number.

Isn't the result a floating point number when Division operations of integers encounter division? Let's give it a try:

11/4 # => 2

Many beginners are surprised that, even if the division of integers in Python is different, the result is still an integer, and the remainder is directly thrown away. However, Python provides a remainder calculation % to calculate the remainder:

11% 4 # => 3

If we want to calculate the exact result of 11/4, we can convert one of the two numbers into a floating-point number by following the "integer and floating-point mixed operation result is a floating-point number" rule:

11.0/4 #==> 2.75

 

 

PythonBoolean Type

We have learned that Python supports boolean data. The boolean type has only True and False values, but the boolean type has the following operations:

And operations: The calculation result is True only when both boolean values are True.

True and True #=> True

True and False #=> False

False and True #=> False

False and False #=> False

Or operation: If a Boolean value is True, the calculation result is True.

True or True #=> True

True or False #=> True

False or True #=> True

False or False #=> False

Non-operation: Change "True" to "False" or "False" to "True:

Not True #=> False

Not False #=> True

Boolean operations are used in the computer for condition determination. Based on the True or False calculation result, the computer can automatically execute different subsequent codes.

In Python, The boolean type can perform the and, or, and not operations with other data types. See the following code:

A = True

Print a and 'a = t' or 'a = f'

The calculation result is a string 'a = t' instead of a boolean type. Why?

Because Python regards 0, Null String '', and None as False, and other values and non-null strings as True, so:

True and 'a = T' is calculated as 'a = t'

Continue to calculate 'a = t' or 'a = f'. The calculation result is 'a = t'

To explain the above results, there is also an important rule for the and or operations:Short circuit calculation.

1. when calculating a and B, if a is False, then according to the calculation rule, the entire result must be False, so a is returned; if a is True, the entire calculation result depends on B, so B is returned.

2. when calculating a or B, if a is True, then according to the or algorithm, the entire calculation result must be True, so a is returned; if a is False, then the entire calculation result depends on B, so B is returned.

Therefore, when the Python interpreter performs a Boolean operation, as long as the calculation result can be determined in advance, it will not calculate the result and return the result directly.

Related Article

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.