With the old Ziko Python's dazzling operator _python

Source: Internet
Author: User
Tags arithmetic arithmetic operators logical operators numeric value in python

In computer advanced languages, operators are more diverse. In fact, it all stems from our daily needs.

Arithmetic operators

I've already talked about arithmetic, which involves some operators: subtraction, the corresponding symbols are: +-*/, in addition, there are remainder:%. These are arithmetic operators. In fact, arithmetic operators are more than that. According to the knowledge of high school mathematics, reader should also think that there should be the powers, roots and so on.

A table is listed below to show all the operators. Do not remember, but to take a serious look, know that there are those, if used later, but not confident to remember, you can check.

operator Description instance
+ Add-Two objects added 10+20 Output 30
- Minus-get a negative number or a number minus another 10-20 Output Results-10
* Multiply by-two numbers or return a string that is repeated several times 10 * 20 Output 200
/ Except-X divided by Y 20/10 Output Results 2
% Fetch-Returns the remainder of the division 20%10 Output 0
** Power-Returns the Y-power of X 10**2 Output 100
// Take Division-the integer part of the return quotient 9//2 output 4, 9.0//2.0 output 4.0

It is not strange to look at. Here is a suggestion for reader, please open your idle, and then experiment with the operator above.

Listed as reader can be based on the knowledge of high school mathematics, think of the above operator in the mixed operation, should be calculated in what order. and try it for yourself, in accordance with the rules of Middle school math. (should be consistent, computer scientists will not do another set of us to suffer with them.) )

Comparison operators

The so-called comparison, is more than two things. This is the most common in a country, parents often compare their children with other children, lest their children in a certain way, officials often compare their wages and banks, always feel less.

In computer advanced language programming, any two of the same type of volume can be compared, such as two numbers can be compared, two strings can be compared. Note that there are two of the same type. Can different types of quantity be compared? First of all, this comparison is meaningless. It is like 22 meat and three feet cloth to compare, who is the big one? This comparison is meaningless. So, in real programming, we have to be careful with this comparison of different types of quantities.

However, in some languages, this kind of meaningless comparison is allowed. Because when it is compared, the conversion of the Non-numeric value to the numeric type is compared. We'll do an experiment in the back.

For comparison operators, there are some learning in primary mathematics: greater than, less than, equal to, not equal to. There are no strange things, and so is Python. And look at the following table:

The following assumes variable A is 10 and the variable B is 20:

operator Description instance
== Equals-compares objects for equality (A = = B) returns FALSE.
!= Not equal to-compare two objects for unequal (a!= B) returns True.
> Greater-Returns whether x is greater than Y (A > B) returns FALSE.
< Less than-returns whether x is less than Y (A < B) returns TRUE.
>= Greater than or equal-returns whether X is greater than or equal to Y. (a >= B) returns FALSE.
<= Less than or equal-returns whether X is less than or equal to Y. (a <= B) returns True.

In the table instance above, the result of showing the comparison is to return a true or false, what does that mean? is to tell you that this comparison, if established, is true, returns True, otherwise returns false, stating that the comparison is not valid.

Please follow the following way to compare operations, and then according to their own imagination, the comparison of the operation of Skilled.

>>> a=10
>>> b=20
>>> a>b
False
>>> a<b
True
>>> a==b
false
>>> a!=b
True
>>> a>=b
false
>>> A<=b
True
>>> c= "5"  #a, C is two different types of quantity, can be compared, but do not advocate this.
>>> a>c
False
>>> a<c
True

logical operators

First of all to talk about what is logic, Mr. Han has a classification of logic:

Logic is divided into two kinds, one is logic, the other is Chinese logic. ———— Han Cold

This classification is indeed very accurate. In many cases, the Chinese people have very wonderful logic. But in Python, it's about logic, not the Chinese logic.

Logic (logic), also called rationale, reasoning, Inference, inference, is a philosophical study of effective inference. Logic is used in most intelligent activities, but it is regarded as a subject mainly in the fields of philosophy, mathematics, semantics and computer science. In mathematics, logic refers to the effective inference of the study of a formal language.

About logic, reader if you are interested, you can listen to the public class of National Taiwan University: Logic

Here's a simple understanding of logic.

Variable of Boolean type

In all high-level languages, there are such a class of variables, called Boolean types. From this name, reader will know, this is named after a person's name.

George Boole (George boole,1815 November-1864), mathematician and philosopher of England.

George Boole was the son of a cobbler born in Lincoln, England. Because of his poor family, Boer had to help his family while striving for his education, and anyway, he became one of the most important mathematicians of the 19th century. Although he had considered a priest, he eventually decided to teach, and soon opened his own school.

when he was preparing his lessons, Boolean was dissatisfied with the math textbook and decided to read the papers of the great mathematician. In reading the great French mathematician Lagrange's paper, Boolean has a new discovery in the variational approach. The Variational method is a branch of mathematical analysis that deals with curves and surfaces that seek to optimize certain parameters.

in 1848, Boolean published "The Mathematical analysis of the Logic", which is his first time in many contributions to the symbolic logic.

he was appointed in 1849 as a professor of mathematics at Queen's College in Cork, Ireland (now University of Cork or UCC). In 1854, he published "The Laws of thought", his most famous book. In this book Boolean introduces the Boolean algebra that is now named after his name. Boolean wrote textbooks for differential equations and difference equations that have been used in the UK until the end of 19th century.
because of its special contribution in symbolic logic operations, many computer languages refer to logical operations as Boolean operations, and their results are called Boolean values.

Please reader carefully read the life of Boolean, determined Ah.

This set of logic created by Boolean is called "Boolean algebra". Which stipulates that there are only two values, true and false, that correspond to 1 and 0 of the binary number on this computer. Therefore, Boolean algebra and computers are naturally consistent.

A Boolean type is a data variable that returns a result of 1 (True), 0 (False).

In Python (other high-level languages are similar, in fact, the algorithms for Boolean algebra), there are three operators that can implement the operations between variables of a Boolean type.

Boolean operations

Looking at the table below, this logical operator is easier to understand:

(assuming variable A is 10 and variable B is 20)

operator Description instance
and Boolean "and"-if x is False,x and Y returns false, it returns the calculated value of Y. (A and B) returns True.
Or Boolean "or"-if x is true, it returns true, otherwise it returns the calculated value of Y. (A or B) returns true.
Not Boolean "Non"-returns False if X is true. If x is False, it returns true. Not (A and B) returns false.

and

And, translated to the "and" operation, but in fact, this kind of translation is easy to arouse words too literally understanding. Let's talk about the right understanding. A and B, the meaning is: first operation A, if the value of a is true, calculates B and returns the result of B as the final result, if B is false, then the end result of a and B is false and if B is true, then the result of a and B is true If the value of a is false, then B is not computed, and a direct return of the result of A and B is false.

Like what:

4>3 and 4<9, first look at the 4>3 value, the value is true, and then look at the 4<9 value, which is true, so the final result of this expression is true.

>>> 4>3 and 4<9
True

4>3 and 4<2, look at the 4>3 first, return true, look at 4<2, return false, and the end result is false.

>>> 4>3 and 4<2
False

4<3 and 4<9, look at the 4<3 first, return to false, do not look at the back, directly return this result as the final result.

>>> 4<3 and 4<2
False

It is easy for words too literally to understand that there are quite a number of people who think that whenever you look at the values on both sides, true returns True, and one false returns false. The results obtained from this understanding are the same as those obtained in the previous understanding, but the computational volume is not the same.

Or

Or, translated to or. In A and B, this is how it operates:

if A==True:
  return True
else:
  if B==True:
    return True
  else if B==False:
    return False

The above paragraph is pseudo code. The so-called pseudo code, is not the real code, can not run. However, pseudocode can also be used to express a computational process in a way that is similar to code.

is reader able to read the above pseudo code? Add the comments on each line below. This pseudocode is similar to natural English.

ifa==True:#如果A的值是True
   return True   #返回True, the end result of an expression is true
Else:#否则, that is, the value of a is not true
  ifb==True:#看B的值, and then return the value of B as the final result. 
     return True
  Else ifb==False: return False
For example, according to the above calculation process, analysis of the following example, is not consistent with the results of the operation?
>>>4<3 or 4<9
True
>>>4<3 or 4>9
False
>>>4>3 or 4>9
True
Not

Not, translated into "not", secretly thought very good, regardless of what to face, is to deny it.

>>> not (4>3)
False
>>> not (4<3)
True

About the operator problem, in fact, more than these, there are, for example, the member operator in, later in the study will gradually encounter.

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.