the basic operator of Python
What is an operator. Similar to 5+7=12, 12-4=8, 3*9=27, 1/4=0.25.
Python Arithmetic operators:
operator |
Description |
instance |
+ |
Add-two objects added (numbers, strings, lists, tuples) |
3+2 Output 5 "A" + "B" Output "AB" |
- |
Subtract-a number minus another number or set's difference |
9-1 Output 8 {1,2,3}-{2} output {1,3} |
* |
Multiply by-two numbers or return an object (string, list, tuple) that is repeated several times |
4*5 output 20 [0,1]*2 output [0,1,0,1] |
/ |
Divide-two numbers |
5/2 Output 2.5 |
// |
Division-integral part of the return quotient |
5//2 Output 2 |
% |
Modulo-Returns the remainder of the division |
5%2 Output 1 |
** |
Power-Returns the Y-power of X |
3**2 Output 9 |
Python comparison operators:
operator |
Description |
instance |
== |
Equals-compares objects for equality |
5==3 returns False |
。 = |
Not equal to-compare two objects for unequal |
' A '!= ' B ' returns true |
> |
Greater-Returns whether x is greater than Y |
8>5 returns True |
< |
Less than-returns whether x is less than Y |
7<3 returns False |
>= |
Greater than or equal-returns whether X is greater than or equal to Y |
3>=2 returns True |
<= |
Less than or equal-returns whether X is less than or equal to Y |
5<=3 returns False |
Note: Boolean values true and false are equivalent to integers 1 and 0.
Python assignment operator:
operator |
Description |
instance |
= |
Simple assignment operator |
C=a+b assigns the result of the a+b operation to C |
+= |
Addition assignment operator |
B+=a equivalent to B=b+a |
-= |
Subtraction assignment operator |
B-=a equivalent to B=b-a |
*= |
Multiplication assignment operator |
B*=a equivalent to B=b*a |
/= |
Division assignment operator |
B/=a equivalent to B |
%= |
Modulo assignment operator |
B%=a equivalent to B=b%a |
**= |
Power assignment operator |
b**= equivalent to B=b**a |
//= |
Take the integer division operator |
B//=a equivalent to b=b//a |
Python bitwise operators:
operator |
Description |
instance |
& |
Bitwise AND Operator: Two values that participate in the operation, if two corresponding bits are 1, the result of this bit is 1, otherwise 0 |
1&1 is 1 1&0 is 0 |
| |
Bitwise OR operator: As long as the corresponding two bits have one 1 o'clock, the result bit is 1 |
1|0 return 1 0|0 return 0 |
^ |
Bitwise XOR Operator: When two corresponding bits are different, the result is 1 |
1^0 return 1 1^1 return 0 |
~ |
Bitwise inverse operator: each bits of the data is reversed, which turns 0 to 1, 1 to 0 |
~ Return 0 ~0 return 1 |
〈〈 |
Left-shift operator: "〈〈" to the left of the operands of the binary digits all shifted left several bits, "〈〈" to the right of the number to specify the number of digits moved, high drop, low fill 0 |
1〈〈1 return 2 |
〉〉 |
Right shift operator: "〉〉" to the left of the operands of the binary digits all to the right of several bits, "〉〉" to the right of the number to specify the number of digits to move, low drop, high position complement symbol bit
|
8〉〉1 return 4 |
Python logical operators:
operator |
Logical Expression |
Description |
instance |
and |
X and Y |
Boolean "and"-returns the value of Y if x is 0 or false,x and y returns the value of X |
(8 and 15) return 15 |
Or |
X or Y |
Boolean "or"-returns the value of Y if X is not 0 or true,x or y returns the value of X |
(8 or 15) return 8 |
Not |
Not X |
Boolean "Non"-returns False if X is a value other than 0 or true. Returns true if X is 0 or False |
Not (8 and 15) returns false |
Python member Operator:
operator |
Description |
instance |
In |
Returns False if the value found in the specified sequence returns true |
"A" in "ABC" returns True |
Not in |
Returns True if no value is found in the specified sequence, otherwise returns false |
1 not in [2,3] returns True |
Python identity operator:
operator |
Description |
instance |
Is |
Determines whether two identifiers refer to the same object, returns True if the same object is referenced, or returns false |
' A ' is ' a ' returns True |
is not |
Determines whether two identifiers are referenced from different objects, returns true if the reference is not the same object, or returns false |
' A ' is not ' B ' returns False |
X is Y, similar to ID (x) ==id (y), x is not y similar to ID (x)!=id (y) Note: ID () function is used to get the memory address of an object
Python operator Precedence:
Serial Number |
operator |
Description |
1 |
** |
Index (highest priority) |
2 |
~x |
Flip by bit |
3 |
+x,-x |
PLUS sign |
4 |
*,/,//,% |
Multiplication, division, divisible, and remainder |
5 |
+,- |
Addition and subtraction |
6 |
〈〈,〉〉 |
Shift operator |
7 |
& |
Bitwise AND |
8 |
^ |
Per-bitwise XOR OR |
9 |
| |
by bit or |
10 |
<,<=,>,>=,!=,== |
Comparison operators |
11 |
=,%=,/=,//=,-=,+=,*=,**= |
Assignment operator |
12 |
Is,is not |
Identity operator |
13 |
In,not in |
Member operators |
14 |
Not X |
Logical operator "NON" |
15 |
and |
Logical operators "and" |
16 |
Or |
Logical operator "or" |
Three different numeric types:
Integer (int)-usually referred to as an integer or an integer, is a positive or negative integer, with no decimal points. There is no size limit for integral types in Python3.
Floating-point type (float)-floating-point types are usually composed of integer parts and decimal parts, and floating-point types can also be represented by scientific notation (1.8e2=1.8x10^2=180.0).
Complex numbers (complex)-complex numbers are composed of the real part and the imaginary part, which can be represented by A+BJ or complex (a,b), and the real and imaginary parts of the complex numbers are floating-point types.
Python Numeric type conversions:
Sometimes, we need to convert the type of data, just the data type as a function name.
For example: Int (x) converts x to an integer >>>int (2.7) 2
Float (x) converts x to a floating-point number >>>float (9) 9.0
Complex (x) converts x to a plural, and the real number is divided into X, and the imaginary part is divided into 0
Complex (X,y) converts x and Y to a complex number, and the real numbers are divided into X, and the imaginary parts are y >>>complex (3,5) 3+5j
Math function (import math):
Function |
return value (description) |
ABS (x) |
Returns the absolute value of a number, such as ABS (-5) Returning 5 |
Ceil (x) |
Returns the upper integer of a number, such as Math.ceil (3.2) Returning 4 |
Floor (x) |
Returns the lower integer of a number, such as Math.floor (5.6) Returning 5 |
Log (x) |
logarithm, which defaults to the natural number e as the base, such as Math.log (MATH.E) returns 1.0 Math.log (100,10) returns 2.0 |
Max (x1,x2,...) |
Returns the maximum value of the given parameter, which can be a sequence |
Min (x1,x2,...) |
Returns the minimum value of a given parameter, which can be a sequence |
Round (X,[,n]) |
Returns the rounded value of a floating-point number x, such as an n value, that represents the digits rounded to the decimal point |
sqrt (x) |
Returns the square root of a number x and X cannot be negative |
Note: The above sequence can be a string, a list, a tuple, a collection. And the elements in the sequence must be of the same type.
Example:
>>>import Math
>>>math.ceil (5.2) # 6
>>>math.floor (9.8) # 9
>>>math.log (16,2) 4.0
>>>max ("Happy") ' Y ' to find the character with the largest ASCII value in the string
>>>min ([5,9,3,7,6]) 3
>>>round (3.14159,2) 3.14 #保留两位小数
>>>math.sqrt (81) # 9.0
Random number function: (Import random)
Function |
return value (description) |
Choice (seq) |
Randomly select an element from the elements of a sequence, such as Random.choice (range (10)), and randomly select an integer from 0 to 9. |
Randrange ([Start],stop,[step]) |
Gets a random number from the specified range, in the set incremented by the specified step size, and the start defaults to the 0,step default value of 1. |
Random () |
Randomly generates the next real number, which is in the range of [0,1]. |
Seed ([x]) |
Change the seed of the random number generator, if you provide the same seed value each time you call seed (), you will get the same sequence of random numbers. |
Shuffle (LST) |
Sorts all the elements of a sequence randomly. |
Uniform (x,y) |
Randomly generates the next real number, which, in the [X,y] range, obeys the average distribution. |
The sequence here can only be ordered sequences, such as strings, lists, tuples.
Example:
>>>import Random
>>>random.choice (["Apple", "orange", "banana"]) ' orange '
>>>random.choice ("xxxxyyyyyyzzzzz") ' Y '
>>>random.randrange (0,100,2) #随机生成0 an even 48 between ~100
>>>random.random () *10 #生成0 the random number between ~10 8.192738512619767
>>>list=[1,2,3,4,5]
>>>random.shuffle (list) #打乱列表list
>>>list [3,2,4,1,5]
Random number between >>>random.uniform (5,20) #生成5 ~20 12.395766517579691
>>>random.seed (5)
>>>random.random () 0.6229016948897019
>>>random.random () 0.7417869892607294
>>>random.seed (5)
>>>random.random () 0.6229016948897019
>>>random.random () 0.7417869892607294
Trigonometric functions: (Import Math)
Function |
return value (description) |
Sin (x) |
Returns the sine value of x radians |
COS (x) |
Returns the cosine of x radians |
Tan (x) |
Returns the tangent of x radians |
ASIN (x) |
Returns the inverse chord Radian value of X |
ACOs (x) |
Returns the inverse cosine radian value of x |
Atan (x) |
Returns the tangent radian value of x |
Hypot (X,y) |
Return to Euclidean norm sqrt (x*x+y*y) |
Degrees (x) |
Converts radians to angles such as math.degrees (Math.PI) returns 180.0 |
Radians (x) |
Convert angle to radians such as Math.radians (180) return 3.1415926 |
Python string operators:
A= "Nice" b= "Day"
Operator |
Describe |
Instance |
+ |
string concatenation |
A+b Output Result: Niceday |
* |
Duplicate output string |
A*2 Output Result: Nicenice |
[ ] |
Get characters in a string by index |
A[2] The output of the result is C |
[:] |
To intercept a part of a string |
A[1:3] Output IC |
In |
Member operator-Returns TRUE if the character contains a given character |
' C ' in a output result 1 |
Not in |
Member operator-Returns true if the character does not contain the given characters |
' J ' not in a output result 1 |
R/r |
Raw string-all strings are used literally, without escaping special or nonprinting characters |
Print (R ' \ n ') is printed as is instead of outputting a newline |
Python string formatting:
Python uses a string as a template with format characters that are reserved for real values and indicate the format in which the real values should be rendered. Python uses a tuple to pass multiple values to the template, and each value corresponds to a format character.
[' I ' m%s. I ' m%d years old. '] % ("Amy", 20) [] string template
Template format characters pass a tuple of values
Python string format symbol:
symbols |
Description |
instance |
%c |
Formatting characters or ASCII code |
'%c '%65 output A |
%s |
format string |
"%s"% "Hello" output result hello |
%d |
formatting integers |
'%d '%8.5 output 8 |
%o |
Format unsigned octal number |
"%o"%16 output 20 |
%x %x |
Format unsigned hexadecimal numbers |
"%x"%15 output F "%x"%15 output F |
%f |
Format floating-point numbers to specify the precision after the decimal point |
"%.2f"%3.1415 output 3.14 |
%e %E |
Using scientific counting method to format floating point numbers |
"%e"%150 output 1.500000e+02 "%E"%150 output 1.500000E+02 |
Python string Function-1:
function |
return value (description) |
Isalnum () |
Returns true if there is at least one character in the string and all characters are letters or numbers, or false |
Isalpha () |
Returns true if there is at least one character in the string and all characters are letters, or false |
IsDigit () |
Returns True if the string contains only digits, otherwise returns false |
Lower () |
All uppercase characters in the conversion string are lowercase |
Upper () |
Capitalize all lowercase characters in a transform string |
Max (str) |
Returns the largest letter in Str in a string |
Min (str) |
Returns the smallest letter in Str in a string |
Python string Function-2:
function |
return value (description) |
count (Str,beg=0,end=len (String)) |
Returns the number of times that STR appears in the string, and returns the number of STR occurrences in the specified range if beg or end specifies |
|