The assignment and operator of the Python full stack Road series

Source: Internet
Author: User
Before proceeding to the following article, let's take a look at some of the operators that Python provides for us, define two variables, the values of a and b,a are 10,b values is 20.

Arithmetic operators

/tbody>
operator description instance
+ Plus, two objects added a+b =30
- a-b=-10
* a*b=200< /td>
Divide, divide two objects b/a=2
% take film, return the remainder of division b%a=0
** a**b=100000000000000000000l
// divisible remainder, returns the integer portion of the quotient a//b=0

Comparison operators

operator Description Example
== Equals, compares two objects for equality A==b return False
!= does not equal, compares two objects that are not equal A!=b returns True
<> does not equal, compares two objects that are not equal A<>b returns True
> Greater than, compare X is greater than Y, A>b return False
< Less than, compare X is less than Y A<b returns True
>= Greater than or equal, comparing x is greater than or equal to Y A>=b return False
<= Less than equals, compare X is less than or equal to Y A<=b returns True

Assignment operators

operator description instance
= Assignment operators C=a+b, assign the result of A+b to C
+= Addition copy operator c + = A is equivalent to C = C + A
-= Subtraction copy operator C-= A is equivalent to C = c-a
*= Multiply copy Operators c = A is equivalent to C = C A
/= Division copy operator C/= A is equivalent to C = c/a
%= Modulo assignment operator C%= A is equivalent to C = c% A
**= Power assignment operator c = A is equivalent to C = C A
//= Take the divisible assignment operator C//= A is equivalent to C = c//A

logical operators

operator Description Example
and and A and B return true
Or Or A or B returns true
Not Non- No (A and B) returns false

Member operators

operator Description
Inch Returns True if a value is found in the specified sequence, otherwise false
Not in Returns True if no value is found in the specified sequence, otherwise false

Syntax for assignment statements

arithmetic explain
spam= ' spam ' Basic form
Spam, ham= ' yum ', ' yum ' Tuple assignment operations
[Spam, han]=[' yum ', ' yum '] List Assignment operations
a,b,c,d= ' spam ' Sequence assignment operation, versatility
A, *b= ' spam ' Extended Sequence Unpacking
Spam = Ham = ' Hello ' Multi-Objective assignment operation
Spams + = 42 Enhanced assignment operations

Instance

Sequence operations

>>> nudge = 1>>> wink = 2>>> A, b = Nudge, wink>>> A (1, 2) # Nested Way >>> ((A, b ), c) = (' SP ', ' am ') >>> A, B, C (' S ', ' P ', ' am ')

Extended Sequence Unpacking

A list is assigned a name with an asterisk that collects all items in the sequence that are not assigned to another name.

First define a SEQ sequence for testing:

>>> seq = [1, 2, 3, 4]

aMatch the first item in the sequence to b match the rest of the content

>>> A, *b = Seq>>> A, b (1, [2, 3, 4])

bMatches the last item in the sequence, a matching everything before the last item in the sequence

>>> *a, B = seq>>> A, a ([1, 2, 3], 4)

The last item of the first item is assigned to a and c , and b all content between them is obtained.

>>> *a, b, C = seq>>> a,b,c ([1, 2], 3, 4)

Names with asterisks may match only a single item, but a list is always copied to it, and an empty list is returned if there is nothing left to match:

>>> a,b,c,*d = seq>>> print (a,b,c,d) 1 2 3 [4]>>> a,b,c,d,*e = seq>>> print (a,b,c,d,e) 1 2 3 4 []

A multi-objective assignment statement is a direct assignment of all supplied variable names to the right object

>>> A = b = c = ' as ' >>> a,b,c (' as ', ' as ', ' as ') # The values referenced are also the same >>> ID (a), id (b), ID (c) (4331109208, 4 331109208, 4331109208)


Before proceeding to the following article, let's take a look at some of the operators that Python provides for us, define two variables, the values of a and b,a are 10,b values is 20.

Arithmetic operators

/tbody>
operator description instance
+ Plus, two objects added a+b =30
- a-b=-10
* a*b=200< /td>
Divide, divide two objects b/a=2
% take film, return the remainder of division b%a=0
** a**b=100000000000000000000l
// divisible remainder, returns the integer portion of the quotient a//b=0

Comparison operators

operator Description Example
== Equals, compares two objects for equality A==b return False
!= does not equal, compares two objects that are not equal A!=b returns True
<> does not equal, compares two objects that are not equal A<>b returns True
> Greater than, compare X is greater than Y, A>b return False
< Less than, compare X is less than Y A<b returns True
>= Greater than or equal, comparing x is greater than or equal to Y A>=b return False
<= Less than equals, compare X is less than or equal to Y A<=b returns True

Assignment operators

operator description instance
= Assignment operators C=a+b, assign the result of A+b to C
+= Addition copy operator c + = A is equivalent to C = C + A
-= Subtraction copy operator C-= A is equivalent to C = c-a
*= Multiply copy Operators c = A is equivalent to C = C A
/= Division copy operator C/= A is equivalent to C = c/a
%= Modulo assignment operator C%= A is equivalent to C = c% A
**= Power assignment operator c = A is equivalent to C = C A
//= Take the divisible assignment operator C//= A is equivalent to C = c//A

logical operators

operator Description Example
and and A and B return true
Or Or A or B returns true
Not Non- No (A and B) returns false

Member operators

operator Description
Inch Returns True if a value is found in the specified sequence, otherwise false
Not in Returns True if no value is found in the specified sequence, otherwise false

Syntax for assignment statements

arithmetic explain
spam= ' spam ' Basic form
Spam, ham= ' yum ', ' yum ' Tuple assignment operations
[Spam, han]=[' yum ', ' yum '] List Assignment operations
a,b,c,d= ' spam ' Sequence assignment operation, versatility
A, *b= ' spam ' Extended Sequence Unpacking
Spam = Ham = ' Hello ' Multi-Objective assignment operation
Spams + = 42 Enhanced assignment operations

Instance

Sequence operations

>>> nudge = 1>>> wink = 2>>> A, b = Nudge, wink>>> A (1, 2) # Nested Way >>> ((A, b ), c) = (' SP ', ' am ') >>> A, B, C (' S ', ' P ', ' am ')

Extended Sequence Unpacking

A list is assigned a name with an asterisk that collects all items in the sequence that are not assigned to another name.

First define a SEQ sequence for testing:

>>> seq = [1, 2, 3, 4]

aMatch the first item in the sequence to b match the rest of the content

>>> A, *b = Seq>>> A, b (1, [2, 3, 4])

bMatches the last item in the sequence, a matching everything before the last item in the sequence

>>> *a, B = seq>>> A, a ([1, 2, 3], 4)

The last item of the first item is assigned to a and c , and b all content between them is obtained.

>>> *a, b, C = seq>>> a,b,c ([1, 2], 3, 4)

Names with asterisks may match only a single item, but a list is always copied to it, and an empty list is returned if there is nothing left to match:

>>> a,b,c,*d = seq>>> print (a,b,c,d) 1 2 3 [4]>>> a,b,c,d,*e = seq>>> print (a,b,c,d,e) 1 2 3 4 []

A multi-objective assignment statement is a direct assignment of all supplied variable names to the right object

>>> A = b = c = ' as ' >>> a,b,c (' as ', ' as ', ' as ') # The values referenced are also the same >>> ID (a), id (b), ID (c) (4331109208, 4 331109208, 4331109208)

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.