Into the Python World (c) Variables and operators

Source: Internet
Author: User

Variable

Variables in Python are similar to variables in other languages, which are a variable amount that stores the specified range of values.

At a deep level, it is actually a reference that refers to the value of an area of the computer's memory that exists.

Name of the variable

Variables are made up of letters, underscores and numbers

Cannot start with a number

You cannot use a keyword as a variable name

Assigning values to variables

A = 1: Assigns a value of 1 to the variable a

LD (a): Gets the memory address referenced by the variable A. If, after a=1, LD (a) is the memory address of the portion of memory stored as 1

OperatorAssignment operators
operator Description Example
= Simple assignment operator, assignment from left operand of right operand c = A + B will specify the value A + B to c
+= The addition and assignment operator, which increases the left operand of the right operand and assigns the result to the left operand c + = a equals c = C + A
-= Minus and assignment operator, which subtracts the right operand from the left operand and assigns the result to the left operand c-= a equals c = c-a
*= Multiplication and assignment operator, multiplied by the right operand and left operand, and assigns the result to the left operand C *= a equals c = c * A
/= The Division and assignment operator, which puts the left operand with the correct operand and assigns the result to the left operand C/= a equals = C/A
%= Modulus and assignment operator, which needs to use the modulus of two operands and assign the result to the left-hand operand C%= A is equivalent to C = c% A
**= Exponential and assignment operators, performing exponential (power) calculation operators and assigning to left operands C **= a equals c = c * * A
//= The floor is divided and assigned a value that performs the floor in addition to the operation and assignment to the left operand C//= a equals c = c//A
Arithmetic operators
operator Descriptor Example
+ Addition-adds value to both sides of the operator A + b = 30
- Subtract-subtracts the operand from the right side of the left operand A-B =-10
* Multiply-multiplies the values on both sides of the operator A * b = 200
/ Divide-by right operand divided by left operand b/a = 2
% Modulo-by the right operand and remainder return divided by the left operand B% A = 0
** Exponent-Performs a calculation of the operation Exponent (power) A**b = Power of 10 20
// Floor except-The division of the operand, where the result is the quotient of the number of digits after the decimal point is removed. 9//2 = 4 and 9.0//2.0 = 4.0
Relational operators
operator Description Example
== Checks whether the values of the two operands are equal, and if so, the condition becomes true. (A = = B) is not true.
!= Checks whether the values of the two operands are equal, and if the values are not equal, the condition becomes true. (A! = B) is true.
<> Checks whether the values of the two operands are equal, and if the values are not equal, the condition becomes true. (a <> B) is true. This is similar to! = operator
> Checks if the value of the left operand is greater than the value of the right operand, and if so, the condition is true. (A > B) is not true.
< Checks if the value of the left operand is less than the value of the right operand, and if so, the condition is true. (A < b) is true.
>= Checks whether the value of the left operand is greater than or equal to the value of the right operand, and if so, the condition is true. (a >= B) is not true.
<= Checks whether the value of the left operand is less than or equal to the value of the right operand, and if so, the condition is true. (a <= B) is true.
logical operators
and So-called logic and operators. If the two operands are true, the condition is true. (A and B) is true.
Or The so-called logical OR operator. If there are two operands that are nonzero, the condition becomes true. (A or B) is true.
Not The so-called logical non-operator. Reverses the logical state of the operand. If the condition is true, the logical non-operator will be false. Not (A and B) is false.
Bitwise operators
operator Description Example
& Binary and copy operations have been done, as a result, if it exists in two operands. (A & B) = 12 that is 0000 1100
| A binary or copy operation has a bit, if it exists in an operand. (A | b) = 61 that is 0011 1101
^ A copy of the binary XOR operator, if it is set to an operand instead of two bits. (a ^ b) = 49 that is 0011 0001
~ The binary complement operator is unary and has the effect of a "flip" bit. (~a) = 61 is 1100 0011 in the form of a 2 complement due to the signed binary number.
<< Binary left shift operator. The left operand's value is shifted left by the number of digits specified by the right operand. A << 2 = 240 or 1111 0000
>> Binary right shift operator. The value of the left operand is moved to the right by the number of digits specified by the right operand. A >> 2 = 15 or 0000 1111
Member operators
operator Description Example
Inch Evaluates to TRUE if it is specified in the order in which the variable is found, otherwise false. X in Y, where a 1 is generated if X is a member of sequence Y.
Not in Evaluates to TRUE if it is not found in the specified variable order, otherwise false. x not in Y, where the result is not 1 if X is not a member of sequence Y.
Indicator operator
operator Description Example
Is Evaluates to TRUE if the variable on either side of the operator points to the same object, false otherwise. X is y, where the result is 1 if the value of ID (x) is ID (y).
is not Evaluates to False if the variable operator on both sides points to the same object, otherwise true. X is not y, where the result is not 1, when the ID (x) does not equal the ID (y).
Operator Precedence
operator Description
** Power (raised to exponent)
~ + - Complement, unary Plus and minus (the last two +@ of the method name and-@)
* / % // Multiply, divide, take the mold and remove the floor
+ - Addition and subtraction
>> << Left, right-click Shift
& Bit ' and '
^ | Bitwise XOR ' or ' and periodic ' or '
<= < > >= Comparison operators
<> = = = Equality operators
= %= /= //= -= += *= **= Assignment operators
Is isn't Identity operator
In No in Member operators
Not OR and logical operators


Into the Python World (c) Variables and operators

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.