Python Basic Tutorial Note two: Operators

Source: Internet
Author: User
Tags arithmetic operators bitwise bitwise operators logical operators

Python arithmetic operators

Instance:

A = 21b= 10C= 0C= A +Bprint (c) #  toC= A-Bprint (c) #  OneC= A *Bprint (c) #  AboutC= A/Bprint (c) # 2.1a= 2b= 3C= A * * b#a的b次幂 8print (c) a= 10b= 3C= A//bPrint (c)#c = A In addition to the quotient of B 3
Python comparison operators

Instance:

A = 21b= 10C= 0if(A = =b): Print ("1-a equals B")Else: Print ("1-a Not equal to B")if(A! =b): Print ("2-a Not equal to B")Else: Print ("2-a equals B")if(A <b): Print ("4-a less than B")Else: Print ("4-a is greater than or equal to B")if(A >b): Print ("5-a Greater than B")Else: Print ("5-a is less than or equal to B"# Modify the values of variables a and B= 5b= 20if(A <=b): Print ("6-a is less than or equal to B")Else: Print ("6-a Greater than B")if(b >=a): Print ("7-b is greater than or equal to a")Else: Print ("7-b less than a")

The results are as follows

1- A is not equal to B2- A is not equalto B 4-A is greater than or equal to B5-A is greater than B6-A is less than or equal to B7-b greater than or equal to a
Python assignment operator

Instance:

a = ten= 0= a + bprint (The value of "1-c:"+ = aprint ("2-c value is:"*= aprint ("3-c value is:"/= aprint ("4-c value is:"= 2%= aprint ("5 -The value of C is:"**= aprint (" value for "6-c:"//= aprint ("7-c value is:", c)

The results are as follows

The value of 1-C is: The value of 312-C is: The value of 523-C is: The value of 10924-C is: 52.05-c value is: 26-c value is: 99864
Python bitwise operators

A bitwise operator computes a number as a binary. The bitwise algorithms in Python are as follows:

The variable a in the following table is 60,b 13 and the binary format is as follows:

Instance:

A =  # of 0011 = 1100= 0  = 0000 = 1101 =  +/------------B; # = 0000 1100Print ("1-c value:"= a | b;  # 0011 = 1101Print ("The value of 2-c is:"= a ^ b;  # 0011 = 0001print ("The value of 3-c is:"= ~a;  # -61 = 1100 0011print ("4-c value is:"= a << 2;  # 1111 = 0000print ("5-c value:"= a >> 2;  # = 0000 1111Print ("6-c value:", c)

The results are as follows:

The value of the 1-C is: The value of 122-C is: The value of 613-C is: The value of 494-C is: -615-c value is: 15
Python logical operators

The Python language supports logical operators, with the following assumption that the variable A is ten and B is 20:

Instance:

A = 10b= 20if(A and B): print ("1-variables A and B are true")Else: Print ("1-variables A and B have a not true")if(A or B): print ("2-variables A and B are true, or one of the variables is true")Else: Print ("2-variables A and B are not true") # Modify the value of variable a= 0if(A and B): print ("3-variables A and B are true")Else: Print ("3-variables A and B have a not true")if(A or B): print ("4-variables A and B are true, or one of the variables is true")Else: Print ("4-variables A and B are not true")ifNot (A and B): print ("5-variables A and B are false, or one of the variables is false")Else: Print ("5-variables A and B are true")

The results are as follows:

true true true true true true false false
Python member operators

Instance:

A = 10b= 20List= [1, 2, 3, 4, 5];if(A in list): Print ("1-variable A in the given list")Else: Print ("1-variable A is not in the list given")if(b not in list): Print ("2-variable B is not in the list given")Else: Print ("2-variable B in the given list") # Modify the value of variable a= 2if(A in list): Print ("3-variable A in the given list")Else: Print ("3-variable A is not in the list given")

The results are as follows

1- variable A is not in the given list 2- variable B is not in the given list 3-variable A in the given list
Python identity operator

The identity operator is used to compare the storage units of two objects

Note: the ID () function is used to get the object memory address.

IS and = = difference:

is used to determine whether the two variables refer to the same object, = = is used to determine whether the value of the reference variable is equal.

Instance:

A = 20b= 20if(A is B): print ("1-a and B have the same identity")Else: Print ("1-a and B do not have the same identity")if(A is not B): print ("2-a and B do not have the same identity")Else: Print ("2-a and B have the same identity") # Modify the value of variable b b= 30if(A is B): print ("3-a and B have the same identity")Else: Print ("3-a and B do not have the same identity")if(A is not B): print ("4-a and B do not have the same identity")Else: Print ("4-a and B have the same identity")

The results are as follows:

1- A and B have the same identity 2- A and B have the same identity 3- A and B do not have the same identity 4-a and b do not have the same identity
Python operator Precedence

Instance:

a =ten= 5 = 5= 0= (A + b) * C/D  # (a) + b) * The result of the C/D operationis: "= ((A + b) * c)/d  # (5 *)/print (((A + b) * c)/d The result of the Operation  is:" = (A + b) * (C/D);  # (+) * (15/5) print ("(A + B) * (C/D) operation result:"= a + (b * c)/D;  # + (150/5) print ("A + (b * c)/d operation result is:", e)

The results are as follows:

(A + B) * The result of the C/D operationis: 90.0 + b) * c)/d operation resultis: 90.0 + b) * (C/D) operation resultis: 90.0 + (b * c)/d operation knot The fruit is: 50.0

Python Basic Tutorial Note two: Operators

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.