Chapter 5 of python3, python3

Source: Internet
Author: User

Chapter 5 of python3, python3

When reading this chapter, there are many operators, so do not memorize them. It doesn't matter if you can't remember it now. In the subsequent study, we will gradually deepen our understanding, and it will be much easier to remember in understanding.

1. Variables

What is a variable? In general, it is the amount that can be changed in the memory. Lenovo's current year's algebra:

If the side length of a square is a, the area of the square is a x. Considering side length a as a variable, we can calculate the square area based on the value of a, for example:

If a = 2, the area is a x a = 2x2 = 4;

If a = 3.5, the area is a x a = 3.5x3.5 = 12.25.

 

In computer programs, variables can be integers or floating-point numbers, or strings. the name in the previous chapter serves as a variable as a string.

A variable is only the name of a storage area for program operations. In python, variables should not be stated in advance.

 

Equals sign (=) is used to assign values to variables.

On the left side of the equal sign (=) operator is a variable name, and on the right side of the equal sign (=) operator is the value stored in the variable. For example:

name = 'roy'

In Python, a variable is a variable and has no type. The "type" we call is the type of the object in memory referred to by the variable.

 

2. Operators

What is an operator?

For example, 4 + 5 = 9. In this example, 4 and 5 are called operands and "+" are called operators.

Python supports the following types of operators:

  • Arithmetic Operators
  • Comparison (relational) Operator
  • Value assignment operator
  • Logical operators
  • Bitwise operators
  • Member Operators
  • Identity Operators
  • Operator priority

 

(1) Python Arithmetic Operators

Assume that variable a is 10 and variable B is 21:

Operator Description Instance
+ Add-add two objects Output result 31 of a + B
- Minus-get a negative number or a number minus another number A-B output result-11
* Multiply by two numbers or return a string that has been repeated for several times. A * B output 210
/ Divide By-x by y B/a Output 2.1
% Modulo-return the remainder of the Division B % a output result 1
** Power-returns the y Power of x. A ** B is the 2nd power of 10
// Returns the integer portion of the operator. 9 // 2 output result 4, 9.0 // 2.0 output result 4.0

The following example demonstrates the operations of all Arithmetic Operators in Python:

A = 21b = 10c = 0c = a + bprint ("1-c value:", c) c = a-bprint ("2-c value :", c) c = a * bprint ("3-c value:", c) c = a/bprint ("4-c value:", c) c = a % bprint ("5-c value:", c) # modify variables a, B, ca = 2b = 3c = a ** bprint ("6-c value:", c) a = 10b = 5c = a // bprint ("7-c value:", c)

Execute the program and output:

1-c value: 312-c value: 113-c value: 2104-c value: 2.15-c value: 16-c: 87-c: 2

 

(2) Python comparison Operators

Assume that variable a is 10 and variable B is 20:

Operator Description Instance
= Equal to-compare whether the object is equal (A = B) returns False.
! = Not equal to-compare whether two objects are not equal (! = B) returns True.
> Greater than-returns whether x is greater than y (A> B) return False.
< Less than-returns whether x is less than y. All comparison operators return 1 to indicate true, and 0 to indicate false. This is equivalent to the special variables True and False. Note: These variable names are capitalized. (A <B) returns True.
> = -Returns whether x is greater than or equal to y. (A> = B) returns False.
<= Less than or equal to-returns whether x is less than or equal to y. (A <= B) returns True.

The following example demonstrates all Python comparison operators:

A = 21b = 10c = 0if (a = B): print ("1-a equals B") else: print ("1-a not equal to B ") if (! = B): print ("2-a is not equal to B") else: print ("2-a is equal to B") if (a <B ): print ("3-a less than B") else: print ("3-a greater than or equal to B") if (a> B): print ("4-a greater than B ") else: print ("4-a less than or equal to B") # modify the values of variables a and B a = 5; B = 20; if (a <= B ): print ("5-a less than or equal to B") else: print ("5-a greater than B") if (B> = ): print ("6-B greater than or equal to a") else: print ("6-B less than ")

Execute the program and output:

1-a is not equal to b2-a is not equal to b3-a is greater than or equal to b4-a is greater than b5-a is less than or equal to b6-B is greater than or equal to

 

(3) Python value assignment operator

Assume that variable a is 10 and variable B is 20:

Operator Description Instance
= Simple value assignment operator C = a + B: Assign the operation result of a + B to c.
+ = Addition assignment operator C + = a is equivalent to c = c +
-= Subtraction assignment operator C-= a is equivalent to c = c-
* = Multiplication and assignment operators C * = a is equivalent to c = c *
/= Division assignment operator C/= a is equivalent to c = c/
% = Modulo assignment operator C % = a is equivalent to c = c %
** = Power assignment operator C ** = a is equivalent to c = c **
// = Integer Division assignment operator C // = a is equivalent to c = c //

The following example demonstrates the operations of all the value assignment operators in Python:

A = 21b = 10c = 0c = a + bprint ("1-c value:", c) c + = aprint ("2-c value :", c) c * = aprint ("3-c value:", c) c/= aprint ("4-c value:", c) c = 2c % = aprint ("5-c value:", c) c ** = aprint ("6-c value:", c) c // = aprint ("7-c value:", c)

Execute the program and output:

1-c value: 312-c value: 523-c value: 10924-c value: 52.05-c value: 26-c: 20971527-c: 99864

 

(4) Python bitwise operators

Bitwise operators regard numbers as binary values for calculation. The bitwise Algorithm in Python is as follows:

The binary format of variable a is 60 and variable B is 13 in the following table:

a = 0011 1100b = 0000 1101-----------------a&b = 0000 1100a|b = 0011 1101a^b = 0011 0001~a  = 1100 0011

 

Operator Description Instance
& Bitwise AND operator: two values involved in the operation. If both corresponding bits are 1, the result of this bits is 1; otherwise, it is 0. (A & B) output result 12, binary explanation: 0000 1100
| Bitwise OR operator: if one of the two binary numbers is 1, The result bit is 1. (A | B) output result 61, binary explanation: 0011 1101
^ Bitwise OR operator: When two binary numbers are different, the result is 1. (A ^ B) output result 49, binary explanation: 0011 0001
~ Bitwise inversion OPERATOR: returns the inverse of each binary bit of the data. That is, 1 is changed to 0, and 0 is changed to 1 .~ X is similar to-X-1 (~ A) output result-61, binary explanation: 1100 0011, in a signed binary complement form.
< Left moving OPERATOR: all the binary places of the number of operations move several places to the left. The number on the right specifies the number of digits to move. The number is discarded at a high level and 0 is supplemented at a low level. A <2 output result 240, binary explanation: 1111 0000
> Right-moving OPERATOR: shifts all the binary numbers on the left several places to the right, and ">" the number on the right specifies the number of digits to move. A> 2 output result 15. Binary explanation: 0000 1111

The following example demonstrates the operations of all bitwise operators in Python:

A = 60 #60 = 0011 1100b = 13 #13 = 0000 1101c = 0c = a & B; #12 = 0000 1100 print ("1-c value :", c) c = a | B; #61 = 0011 1101 print ("2-c value:", c) c = a ^ B; #49 = 0011 0001 print ("3-c:", c) c = ~ A; #-61 = 1100 0011 print ("4-c value:", c) c = a <2; #240 = 1111 0000 print ("5-c value:", c) c = a> 2; #15 = 0000 1111 print ("6-c value:", c)

Execute the program and output:

1-c value: 122-c value: 613-c value: 494-c value:-615-c value: 2406-c value: 15

 

(5) Python logical operators

Python supports logical operators. Assume that the variable a is 10 and the variable B is 20:

Operator Logical expression Description Instance
And X and y Boolean "and"-If x is False, x and y returns False, otherwise it returns the calculated value of y. (A and B) returns 20.
Or X or y Boolean "or"-If x is True, it returns the value of x; otherwise, it returns the calculated value of y. (A or B) returns 10.
Not Not x Boolean "not"-If x is True, False is returned. Returns True if x is False. Not (a and B) returns False

The following example demonstrates the operations of all Python logical operators:

A = 10b = 20if (a and B): print ("1-variables a and B are both true") else: print ("1-variables a and B have a value not true") if (a or B): print ("2-variables a and B are both 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 both true") else: print ("3-variables a and B have a not true") if (a or B ): print ("4-variables a and B are both true, or one of them is true") else: print ("4-variables a and B are not true ") if not (a and B): print ("5-variables a and B are both false, or one of the variables is false") else: print ("5-variables a and B are both true ")

Execute the program and output:

1-variables a and B are both true2-variables a and B are both true, or one of the variables true3-variables a and B is not true4-variables a and B are both true, or one of the variables is true5-variables a and B are both false, or one of the variables is false.

 

(6) Python member operators

In addition to the preceding operators, Python also supports member operators. The test instance contains a series of members, including strings, lists, and tuples.

Operator Description Instance
In If a value is found in the specified sequence, True is returned; otherwise, False is returned. X is in the y sequence. If x is in the y sequence, True is returned.
Not in If no value is found in the specified sequence, True is returned; otherwise, False is returned. X is not in the y sequence. If x is not in the y sequence, True is returned.

The following example demonstrates all Python member operators:

A = 10b = 20 list = [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 of the given list ") if (B not in list): print ("2-variable B is not in list in the given list") 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 list given") else: print ("3-variable a is not in the list given ")

Execute the program and output:

1-variable a is not in the given list. list 2-variable B is not in the given list. list 3-variable a is in the list of the given list.

 

(7) Python identity Operators

Identity operators are used to compare the storage units of two objects.

Operator Description Instance
Is Is used to determine whether two identifiers are referenced from an object X is y, SimilarId (x) = id (y)If the same object is referenced, True is returned; otherwise, False is returned.
Is not Is not indicates whether two identifiers are referenced from different objects. X is not y, SimilarId ()! = Id (B). If the referenced object is not the same, True is returned; otherwise, False is returned.

Note: The id () function is used to obtain the object memory address.

The following example demonstrates all Python identity operators:

A = 20b = 20if (a is B): print ("1-a and B have the same ID") else: print ("1-a and B do not have the same id") if (id (a) = id (B )): print ("2-a and B have the same identifier") else: print ("2-a and B do not have the same identifier ") # modify the value of variable B = 30if (a is B): print ("3-a and B have the same identifier") else: print ("3-a and B do not have the same identifier") if (a is not B): print ("4-a and B do not have the same identifier") else: print ("4-a and B have the same ID ")

Execute the program and output:

1-a and B have the same ID. 2-a and B have the same ID. 3-a and B do not have the same ID. 4-a and B do not have the same ID.

Is and =:

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

 

(8) Python operator priority

The following table lists all operators with the highest to lowest priority:

Operator Description
** Index (highest priority)
~ +- Flip by bit, one-dollar plus sign and minus sign (the last two methods are named + @ and -@)
*/% // Multiplication, division, modulo and Division
+- Addition and subtraction
>>< < Right Shift, Left Shift Operator
& AND'
^ | Bitwise operators
<=<>>= Comparison Operators
<>==! = Equal to operator
= % =/= // =-= + = * = ** = Value assignment operator
Is not Identity Operators
In not in Member Operators
Not or and Logical operators

The following example demonstrates the operations on the priority of all operators in Python:

A = 20b = 10c = 15d = 5e = 0e = (a + B) * c/d #(30*15)/5 print ("(a + B) * c/d: ", e) e = (a + B) * c)/d # (30*15) /5 print ("(a + B) * c)/d:", e) e = (a + B) * (c/d ); # (30) * (15/5) print ("(a + B) * (c/d):", e) e = a + (B * c) /d; #20 + (150/5) print ("a + (B * c)/d:", e)

Execute the program and output:

(A + B) * c/d Calculation Result: 90.0 (a + B) * c)/d Calculation Result: 90.0 (a + B) * (c/d) Calculation Result: 90.0a + (B * c)/d Calculation Result: 50.0

 

 

3. Expression

What is an expression?

An expression is composed of one or more operands by operators. The simplest expression contains only one nominal value constant or variable. Complex expressions are composed of operators and one or more operands.

For example, arithmetic expressions, logical expressions, and character expressions. An operation is a process of processing data. The mathematical formula or other formulas used to obtain the calculation result are collectively referred to as expressions. An expression can be a constant, a variable, or a formula. It can be divided into arithmetic expressions, logical expressions, and string expressions.

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.