01. Expression in Python learning tutorial

Source: Internet
Author: User

Python provides powerful expression functions for use. The following describes the main expression operators:

1. +,-, * operator

There is nothing to say about these three operators, which can be used by elementary school students ......

2. "/", "//", "**" Operator

(1) "/" Operator

 
>>> 1/20 >>> 1.0/20.5 >>> 1.0/2.00.5 >>> 2.0/1.02.0 >>>

Have you found anything? "/" Is an integer division operator and the result is also an integer. However, for a floating point number, it is a division operator and the result is also a number of points. This is different from other languages in Python.

(2) "//" Operator

What is the "//" Operator Used?

 
>>> 1 // 20 >>> 2 // 12 >>>> 1.0 // 2.00.0 >>> 1 // 2.00.0 >>>>

"//" The operator is a real division operator in Python. the return value is an integer, regardless of the number of points or integer. However, the data type of the returned result is determined by the divisor and the divisor.

(3) "**" power operator

 
>>> 1 ** 21 >>> 2 ** 24 >>> 2 ** 38 >>> 2 ** 416 >>> 5 ** 53125 >>>

If you see the result, do you think it will be very convenient to perform a power operation in the future. Note the following:

 
>>>-2 ** 2-4> (-2) ** 24

-2 ** 2 does not show the expected result: 4 and the result is-4, because the power operator takes precedence over the unary operator, therefore, you must add parentheses to calculate the power of (-2) (-2) ** 2 = 4

 (4) remainder operator for "%"

 
>>> 5% 32 >>> 4% 20 >>> 4% 31 >>> 4.0% 2.00.0 >>> 4.0% 3.01.0 >>>> 4.0% 5.04.0 >>> 2.75% 0.50.25 >>>

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.