Learning Note (October 19)--python operator

Source: Internet
Author: User
Tags arithmetic operators

Four lessons a week (October 19)

1. Python Arithmetic operators

Assuming that the value of the variable a is 10 , the b value of the variable is 21 , then-

operator Description Example
+ An addition operation that increases the operands on either side of the operator. a + b = 31
- Subtraction operation, subtracting the operand on the left of the operator from the right operand. a – b = -11
* Multiplication, multiplying the operands on either side of the operator a * b = 210
/ Division operation, with right operand in addition to left operand b / a = 2;21.0/10 = 2.1
% Modulo operation, dividing the left operand with the right operand and returning the remainder b % a = 1
** To calculate an exponent (power) on an operator a ** bThat represents 10 the 21 power of the second
// Floor except-The division of the operand, which results in the deletion of the quotient after the decimal point. However, if one of the operands is a negative number, the result will be preserved, i.e. from zero (to negative infinity) 9//2 = 4,,, 9.0//2.0 = 4.0 -11//3 = -4-11.0//3 = -4.0


2. Python relational operators

Relational operators compare the values on either side of them and determine the relationship between them. Assuming the value of the variable a 10 , b the value of the variable is 20 , then-

operator description example
== If the values of the two operands are equal, the condition is true. (A = = B) evaluates to false
! = If the number of two operands Values are not equal, the condition is true. (A! = b) evaluates to true
; If the left-hand operand Value is greater than the right operand value, the condition becomes true. (a > B) evaluates to false
< if Zoka The condition becomes true if the value of count is less than the value of the right operand. (A < b) evaluates to true
>= if Zoka The condition becomes true if the value of count is greater than or equal to the value of the right operand. (a >= B) evaluates to false
<= if The condition becomes true if the value of the left operand is less than or equal to the value of the right operand. (a <= B) evaluates to true


3. Python assignment operator

Assuming the value of the variable a 10 , b the value of the variable is 20 , then-

operator Description Example
= Assign the value of the right operand to the left operand c = a + bRepresents a + b the value assigned to thec
+= Adds the right operand to the left operand and assigns the result to the left operand c + = aEquivalent toc = c + a
-= Subtracts the right operand from the left operand and assigns the result to the left operand c -= aEquivalent toc = c - a
*= Multiplies the right operand by the left operand and assigns the result to the left operand c *= aEquivalent toc = c * a
/= Divides the left operand by the right operand and assigns the result to the left operand c /= aEquivalent toc = c / a
%= Divides the left operand by the modulus of the right operand and assigns the result to the left operand c %= aEquivalent toc = c % a
**= Performs an exponential (power) calculation and assigns a value to the left operand c **= aEquivalent toc = c ** a
//= operator performs a floor divide operation and assigns a value to the left operand c //= aEquivalent toc = c // a


4. Python logical operators

The following logical operators are supported in the Python language. Assuming that the value of the variable is, the value of the a True variable is b False , then-

operator Description Example
and If the two operands are true, the condition is set. (a and b)The result isFalse
or If any one of the two operands is nonzero, the condition becomes true. (a or b)The result isTrue
not The logical state used to invert the operand. not(a and b)The result is True .


Descending priority: (), Not,and,or

Learning Note (October 19)--python operator

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.