Quick start of automation -- Python -- [operation operator] - half an hour a day

Keywords: Python

        Spend half an hour reading this article, and you will get something!

catalogue

Operator

  Comparison operator

boolean operation

Binary Boolean operation

data type

operator

Operator

OperatoroperationexampleEvaluated as
**index2**38
%Modulo / remainder22%85
//Integer division / quotient rounding21//82
/division21/82.625
*multiplication3*515
-subtraction3-12
+addition3+14

  Comparison operator

Operatormeaning
==be equal to
!=Not equal to
<less than
>greater than
<=Less than greater than
>=Greater than or equal to
print(1 == 1)    #T
print(1 == 2)    #F
print(1 != 2)    #T
print(1 != 1)    #F
print('hello' == 'hello')    #T
print('hello' == 'world')    #F
print('t' == 'T')    #F
print(True == True)    #T
print(True != False)    #T
print(1 < 2)    #T
print(1 > 2)    #F
print(1 <= 2)    #T
print(1 < 1)    #F

   These are some comparisons of comparison operators!

boolean operation

   Boolean operators (or, and, not)

Binary Boolean operation

expressionEvaluated as
True and FalseTrue
True and TrueFalse
False and TrueFalse
False and FalseFalse
expressionEvaluated as
True or FalseTrue
True or TrueTrue
False or TrueTrue
False or FalseFalse
expressionEvaluated as
not TrueFalse
not FalseTrue

    The above are the different return values of the three operators. Let's take a look at the examples:

print((4 < 5) and (5 < 6))   # True
print((4 < 5) and (5 > 6))    #False
print((4 < 5) or (5 > 6))    #T
print((4 > 5) or (5 > 6))    #F
print(1 + 1 == 2 or not 2 + 2 == 4)    #T
print(1 + 1 == 2 and not 2 + 2 == 4)    #F

data type

Common data types are integer (int), floating point (float), and string type (str)

data typeexample
Integer (int)-1,-2,0,1,2,3
Floating point-1.1,-0.1,1.1,1.11
String (str)'a','b','abcd','123456'
# The str output in pycharm here is the same as the int type. In order to distinguish, it is recommended to use idle
print(str(10))
print(int(10))
ptint(float(10))

operator

         Integers: integers are the same as numbers in mathematics, such as 1, 2, 3, 4, 5... These are integers as long as they are numbers without decimal points, not Chinese characters or special characters.

         We can perform (+) (-) (*) (/) operations on integers. Look at the example!

print(2 + 3)  # addition
print(3 - 2)  # subtraction
print(6 / 3)  # Division. The result here has a decimal point. If you don't want to add an int integer to the decimal point for conversion, it's good
eg: print(int(6 / 3))
print(2 * 3)  # multiplication

         Floating point numbers: such as 0.3, 0.1 and 0.001 are all floating point numbers

         There are two special examples in the calculation process that get different results, but python will try its best to find accurate results for us. These two special examples have such problems in most languages:

print(0.2 + 0.1) # The printed result is: 0.300000000000000 4

print(3 * 0.1) # The printed result is: 0.300000000000000 4

print(0.2 + 0.3) # The printed result is: 0.5

         Integer and floating-point number: divide any two numbers and the result is a floating-point number. Even if an integer can be divided, one is an integer and the other is a floating-point number, the result is also a floating-point number. See example:

print(3 / 2) # The result is 3.0
print(1 + 2.0) # The result is: 3.0
print(2 * 3.0) # The result is: 6.0
print(2.0 ** 3) # To the third power of 2.0, the result is 8.0

         Underline in number: it is mainly used when the number is large and plays a role in grouping. The actual size of the number remains unchanged. See example:

#Set a variable i and print i
i = 10000_000
print(i)

        The result is:

10000000

         Assign values to multiple variables at the same time: this method is often used to assign a series of data to a group of variables. See an example:

x, y, z = 1, 2, 3
print(x, y, z)

         Here is just a demonstration of how to assign values. They are one-to-one correspondence (x = 1, y = 2, z = 3). This writing also improves the readability of the code. The result is:

1 2 3

         last! Let's have a comment!!! (≖ ‿ ≖)✧

         As I wrote at the door, the notes # are actually comments. Comments serve as a code description. When the amount of code is large, you can find the code location you need more effectively# The content of comments will be ignored by the python interpreter to run the code well! Adding comments is also a good habit.

Posted by gwolgamott on Thu, 14 Oct 2021 16:39:49 -0700