Python operator, how much do you know?

Source: Internet
Author: User

New Year, document update continue ~

First, What is an operator?

We have previously defined variables, which are used to store the data, and the data is stored for the operation, and the operation uses the operator

To give a simple example 4 +5 = 9 . In the example,4 and 5 are called operands , and "+" is called an operator.

The Python language supports the following types of operators:

    • Arithmetic operators
    • Compare (relational) operators
    • Assignment operators
    • logical operators
    • Bitwise operators
    • Member operators
    • Identity operator
    • Operator Precedence

Next, let's learn about Python's operators.

Second, Python Arithmetic Operators

The following assumes that the variable A is 10 and the variable B is 21:

Example 1 :

Print (100+200)

Print (100-200)

Print (100*200)

Print (10/3) # Division is directly in addition to the decimal . javac# directly except to take an integer

Print (Round (10/3,2)) # via round function

Print ("%.2f"% (10/3)) # Basic formatted output

Print ("{:. 2f}". Format (10/3)) #format formatted output

Print (10//3) # take integer

Print (10%3)

Print (3**4) # The result is 3*3*3*3

Demonstrate:

Example 2 :

Enter a three-digit number, and then output the number for each position, for example: 719, as shown below:

Hundred digits: 70 digits: One digit number: 9

# Method on :

num = Int (input (" Please enter a three-digit number:"))

hundreds = num//100

Tens = num% 100//10

ones= num% 10

Print ( hundreds of Three-digit { 0} hundred {1}, 10-digit {2}, single digit {3} ". Format (num, hundreds, tens, ones))

# Method Geneva :

num = input (" Please enter a three-digit number:")

Print ( Hundreds of three-digit { 0} hundred {1}, 10-digit {2}, single-digit {3} ". Format (num, num[0], num[1], num[2]))

Examples of extended display:

a=10

B=20

if (A and b):

Print ("1- variables a and b are true")

Else:

Print ("1- variables a and b have one not true")

if (a orb):

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")

A=0

if (A and b):

Print ("3- variables a and b are true")

Else:

Print ("3- variables a and b have one not true")

if (a orb):

Print ("4- variables a and b are all 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")

A = 10

b = 20

list01 = [1,2,3,4,5]

if a in list01:

Print ("1- variable a in the given list of lists ")

Else:

Print ("1- variable a is not in the given list ")

if b notinlist01:

Print ("2- variable b is not in the given list ")

Else:

Print ("2- variable b in the given list ")

A = 2

if a in list01:

Print ("3- variable a in the given list of lists ")

Else:

Print ("3- variable a is not 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.

A = 20

b = 20

if 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 ID (a) = = ID (b):

Print ("2-a and b have the same identity ")

Else:

Print ("2-a and b do not have the same identity ")

B=30

if 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 ")

is with the == difference:

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

A = 20

b = 10

D = 15

D = 5

E = 0

E = (a+b) *c/d

Print ("(a+b) *c/d operation result is:", E)

E = ((a+b) *c)/d

Print ("((a+b) *c)/d operation result is:", E)

E = (a+b) * (C/D);

Print ("(a+b) * (C/D) operation result is:", E)

Python operator, how much do you know?

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.