In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the code of Python operator". In daily operation, I believe many people have doubts about the code of Python operator. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the question of "what is the code of Python operator?" Next, please follow the editor to study!
01 arithmetic operator
Arithmetic operator is a series of symbols for arithmetic operation of operands, which can meet the general needs of operation. The arithmetic operators in Python are as follows.
+: add, two objects add together
Minus, get a negative number or one number minus another
*: multiply two numbers or return a string that has been repeated several times
/: divided by x divided by y
%: take the module and return the remainder of division
* *: power, which returns the y power of x
/ /: take the integral division and return the integer part of the quotient
The numerical type of the result of an arithmetic operation is related to the type of Operand. In the division (/) operation, whether the quotient is an integer or a floating-point number, the result is always a floating-point number. To get the quotient of an integer, you need to divide it with a double slash (/ /), and the divisor must be an integer. For other operations, as long as any Operand is a floating point number, the result is a floating point number. The basic usage of Python arithmetic operations is as follows.
Num_int = 4 num_float = 4 print ('the sum of integers and floating point numbers is:', num_int + num_float) # Out [1]: the sum of integers and floating point numbers is 8.0 print (the difference between integers and floating point numbers is:', num_int-floating point) # Out [2]: the difference between integers and floating point numbers is: 0 print ('the product of integers and floating point numbers is:' Num_int * num_float) # Out [3]: the product of integer and floating point number is 16.0print ('quotient of floating point number and integer:', num_float / num_int) # Out [4]: the quotient of floating point number and integer is 1.0print ('floating point number to integer module result is:' Num_float% num_int) # Out [5]: floating point integer modulo result: 0.0 print ('integer power of floating point number is:', num_float * * num_int) # Out [6]: integer power of floating point number is: 256.0
02 assignment operator
The assignment operator is used to assign and update variables. In addition to the basic assignment operator (=), the assignment operator of Python includes addition assignment operator, subtraction assignment operator and so on. Strictly speaking, except for the basic assignment operator, everything else belongs to a special assignment operator. The assignment operator in Python is as follows.
=: assignment operation
+ =: addition assignment operation
-=: subtraction assignment operation
* =: multiplication assignment operation
/ =: division assignment operation
% =: take module assignment operation
* =: power assignment operation
/ / =: integer division assignment operation
The special assignment operator in Table 2-2 can be seen as a quick update of a variable, which means that the variable exists, while for a variable that does not exist before, a special assignment operator cannot be used. The basic usage of the Python assignment operation is as follows.
Num_int1 = 4 print ('num_int1 after assignment:', num_int1) # Out [7]: num_int1 after assignment: 4 num_int1 = 4 + 6 print ('num_int1 after assignment:', num_int1) # Out [8]: num_int1 after assignment: 10 num_int1 = 4 * 2 print ('num_int1 after assignment:' Num_int1) # Out [9]: num_int1 after assignment: 8 num_int1 = 4 / 2 print ('num_int1 after assignment:', num_int1) # Out [10]; num_int1 after assignment: 2.0 num_int1 = 4% 2 print ('num_int1 after assignment:', num_int1) # Out [11]: num_int1 after assignment: 0 num_int1 = 4 * * 2 print ('num_int1 after assignment:' Num_int1) # Out [12]: num_int1 after assignment is: 16
03 comparison operator
The comparison operator is used to compare the size or equality between numbers. The comparison operator in Python is shown below.
= =: equals to compare whether objects are equal or not
! =: means it is not equal to compare whether the two objects are not equal.
>: indicates that it is greater than, and returns whether x is greater than y
=: indicates greater than or equal to, and returns whether x is greater than or equal to y
Num_float) # Out [15]: whether num_int is greater than num_float:False print ('num_int is less than num_float:', num_int
< num_float) #Out[16]: num_int是否小于num_float:False print('num_int是否大于等于numfloat:', num_int >= num_float) # Out [17]: whether num_int is greater than or equal to numfloat:True print ('num_int is less than or equal to num_float:', num_int > 2) # Out [27]: num_int1 moves two digits to the right and the result is: 3
In bitwise operation, the inversion operation is more difficult to understand, because it involves the calculation of complement.
The binary source code of a decimal number includes symbolic bits and binary values. Take "60" as an example, the original binary code is "00111100", the first bit is the symbol bit, 0 represents a positive number and 1 represents a negative number. The complement of a positive number is the same as the original binary code, while the complement of a negative number is that the symbol bit of the binary source code remains unchanged, and the rest of you reverse it and add 1 to the last bit.
The reverse operation can be summarized into the following five steps.
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
Take the binary source code of the decimal number.
Complement the original code.
The complement is reversed (the complement of the final result is obtained).
Take the inverse result and then take the complement (get the original code of the final result).
Binary source code to decimal number.
06 identity operator
The identity operator is used to compare the storage units of two objects, as shown below.
Is:is is used to determine whether the storage units of two objects are the same.
Is not:is not is used to determine whether the storage units of two objects are different.
The basic usage of Python identity operations is as follows.
Num_int1 = 15 num_int3 = 15 print ('whether the storage units of num_int1 and num_int3 are the same:', num_int1 is num_int3) # Out [28]: whether the storage units of num_int1 and num_int3 are the same: True num_int2 = 15.0 print ('whether num_int1 and num_int2 storage units are the same:' Num_int1 is num_int2) # Out [29]: whether the storage units of num_int1 and num_int2 are the same: False # returns True if the storage units are the same Otherwise, return False print (whether 'num_int1 and num_int3 storage units are different:', num_int1 is not num_int3) # Out [30]: whether num_int1 storage units are different from num_int3 storage units: False print ('num_int1 storage units are different from num_int2 storage units:', num_int1 is not num_int2) # Out [31]: whether num_int1 and num_int2 storage units are different: True
In identity operation, when two variables with the same memory address perform is operation, two variables with different True; memory addresses are returned for is not operation, True is returned. When an and b get the same value, the two variables get the same memory address.
07 member operator
The role of the member operator is to determine whether a specified value exists in a sequence, including strings, lists, and tuples, as shown below.
In: returns True if the specified value is found in the specified sequence, otherwise returns False
Not in: returns True if the specified value is not found in the specified sequence, otherwise returns False
The basic usage of the Python member operation is as follows.
Num_int1 = 15 list2 = [1, 'apple', 15] print (' whether num_int1 is in list2:', num_int1 in list2) # Out [32]: whether num_int1 is in list2: True array = ('orange', 6,15) print (' num_int1 is not in array:', num_int1 not in array) # Out [33]: whether num_int1 is not in array: False
08 operator precedence
Operator manipulation is common in Python, and operations are usually performed in the form of expressions. An expression consists of operators and operands. For example, "1x 2" is an expression, where "+" is the operator, and "1" and "2" are operands. An expression often contains more than one operator, and when there are multiple operators in an expression, you need to consider the order of operations, that is, the priority of the operators.
The priority of the operator is shown below, the priority decreases from top to bottom, and operators with the same priority operate from left to right.
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
* *: index (highest priority)
~ + -: flip bitwise, unary plus sign and minus sign (the last two methods are named + @ and-@)
* /% /: multiplication, division, modularization and integer division
+ -: addition, subtraction
> > num_int1-num_int3) # Out [35]: num_int1-num_int2 > num_int1-num_int3:False # first perform the addition and subtraction operation, then do the identity judgment print ('num_int1-num_int3 + num_int1 is num_int1:', num_int1-num_int3 + num_int1 is num_int1) # Out [36]: num_int1-num_int3 + num_int1 is num_int1:True # perform the index operation first Then perform the subtraction operation, and finally do the identity judgment print ('num_float * * 2-1 is not num_int2:', num_float * * 2-1 is not num_int2) # Out [37]: num_float * * 2-1 is not num_int2:True, the study of "what are the codes of Python operators" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.