Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Python basics – operators

1. The Boolean value

A Boolean data type represents one of two values: True or False. Once we start using comparison operators, the use of these data types becomes clear. Unlike JavaScript, the first letter T of True and F of False should be capitalized. Example:

print(True)
print(False)
Copy the code

2. The operator

The Python language supports many types of operators, and we’ll highlight a few of them.

3. Assignment operators

Assignment operators are used to assign values to variables. Let’s take lambda as an example. An equal sign in mathematics means that two values are equal, but in Python it means that we store a value in some variable, which we call assignment or assignment to the variable. The following table shows the different types of Python assignment operators.

4. Arithmetic operators:

  • Addition (+) : A + b
  • Subtraction (-) : A – b
  • Multiplication (*) : A * b
  • Division (/) : A/b
  • Modulus (%): A % B
  • Floor division (//): A // B
  • Exponentiation (**) : A ** b

5. Arithmetic operators

Example: integer

# Arithmetic operations in Python
# integer

print ( 'Addition: ' , 1  +  2 )         # 3 
print ( 'Subtraction: ' , 2  -  1 )      # 1 
print ( 'Multiplication: ' , 2  *  3 )   # 6 
print ( 'Division: ' , 4  /  2 )        # 2.0 Division in Python gives floating point numbers
print ( 'Division: ' , 6  /  2 )         # 3.0          
print ('Division: ' , 7  /  2 )         # 3.5 
print('Division without the remainder: '.7 // 2)    # 3, gives no float or remainder
print ('Division without the remainder: '.7 // 3)    # 2 
print ( 'Modulus: ' , 3  %  2 )          # 1, give me the remainder
print ( 'Exponentiation: ' , 2  **  3 )# 9 means 2 * 2 * 2
Copy the code

Example: floating point number

# floating point number
print ( 'Floating Point Number, PI' , 3.14 )
 print ( 'Floating Point Number, Gravity' , 9.81 )
Copy the code

Example: Plural

# the plural
print ( 'Complex number: ' , 1  +  1j )
 print ( 'Multiplying complex numbers: ', (1  +  1j ) * ( 1  -  1j ))
Copy the code

Let’s declare a variable and assign a numeric data type. I’ll use single-character variables, but remember not to get into the habit of declaring them. Variable names should always be mnemonics.

Example:

Declare the top variable first

a  =  3  # a is the variable name and 3 is the integer data type
b  =  2  # b is the variable name and 3 is the integer data type

# Do the arithmetic and assign the result to the variabletotal = a + b diff = a - b product = a * b division = a / b remainder = a % b floor_division = a // b exponential = a **  b# I should have used sum instead of total but sum is a built-in function - try to avoid overwriting built-in functions
print ( total ) # If you don't mark your print with some string, you never know where the result is from
print('a + b = ', total)
print('a - b = ', diff)
print('a * b = ', product)
print('a / b = ', division)
print('a % b = ', remainder)
print('a // b = ', floor_division)
print('a ** b = 'Exponentiation) example:print('== Addition, Subtraction, Multiplication, Division, Modulus ==')

Declare values and group them together
num_one  =  3 
num_two  =  4

# Arithmetic operation
total  =  num_one  +  num_two 
diff  =  num_two  -  num_one 
product  =  num_one  *  num_two 
div  =  num_two  /  num_one
remainder = num_two % num_one

# with the tag
print('total: ', total)
print('difference: ', diff)
print('product: ', product)
print('division: ', div)
print('remainder: ', remainder)
Copy the code

Let’s join the dots and start using the calculations we already know (area, volume, density, weight, circumference, distance, force).

Example:

# Calculate the area of a circle
radius  =  10                                  # Radius of the circle
area_of_circle  =  3.14  *  radius  **  2          # The two * symbols denote exponents or powers
print ( 'Area of a circle: , area_of_circle )

Calculate the area of the rectangle
length  =  10 
width  =  20 
area_of_rectangle  =  length  *  width 
print ( 'Area of a rectangle: , area_of_rectangle )

# Calculate the weight of the object
mass = 75
gravity = 9.81
weight = mass * gravity
print(weight, 'N')                      # Add the weight of the unit

# Count liquid
mass = 75 # in Kg
volume = 0.075 # in cubic meter
density = mass / volume # 1000 Kg/m^3
Copy the code

6. Compare operators

In programming we compare values, we use the comparison operator to compare two values. We check if one value is greater than or less than or equal to other values.

Example: Comparison operator

print ( 3  >  2 )      # true, because 3 is greater than 2
print ( 3> =2 )     # true, because 3 is greater than 2
print ( 3  <  2 )      # false, because 3 is greater than 2
print ( 2  <  3 )      # true, because 2 is less than 3
print ( 2< =3 )     # true, because 2 is less than 3
print ( 3= =2 )     # false, because 3 doesn't equal 2
print ( 3! =2 )     # true, because 3 doesn't make 2
print ( len ( 'mango') = =len ( 'avocado' ))   # False 
print ( len ( 'mango' ) !=  len ( 'avocado ' ))   # True 
print ( len ( 'mango' ) <  len ( 'avocado' ))    # True 
print ( len ( 'milk' ) != len ( 'meat' ))       # False 
print ( len ( 'milk') = =len ( 'meat' ))       # True 
print ( len ( 'tomato') = =len ( 'potato' ))   # True 
print ( len ( 'python' ) >  len ( 'dragon' ))   # False


# Compare something to give true or false

print('True == True: '.True= =True)
print('True == False: '.True= =False)
print('False == False:'.False= =False)
Copy the code

In addition to the above comparison operators, Python also uses:

  • Is: Returns true if two variables are the same object (x is y)
  • Is not: Returns true if the two variables are not the same object (x is not y)
  • In: Returns True if the query list contains an item (x in y)
  • Not in: Returns True if the queried list does not have an item (x in y)
print ( '1 is 1' , 1  is  1 )                    # True - Because the data values are the same
print ( '1 is not 2' , 1  is  not  2 )            # True - because 1 is not 2
print ( 'A in Asabeneh ' , 'A'  in  'Asabeneh' ) # True -a is found in the string
print ( 'B in Asabeneh' , 'B'  in  'Asabeneh' ) # False - No capital B
print('coding' in 'coding for all') # True - because of the word coding for all
print ( 'a in an:' , 'a'  in  'an' )       # True 
print ( '4 is 2 ** 2:' , 4  is  2  **  2 )    #
Copy the code

7. Logical operators

Unlike other programming languages, Python uses the keywords AND, or and not for logical operators. Logical operators are used to combine conditional statements:

print ( 3  >  2  and  4  >  3 ) # True - because both statements are True
print ( 3  >  2  and  4  <  3 ) # False - because the second statement is False
print ( 3  <  2  and  4  <  3 ) # False - Because both statements are False
print ( 'True and True: ' , True  and  True )
 print ( 3  >  2  or  4 >  3 )   # True - Because both statements are True
print ( 3  >  2  or  4  <  3 )   # True - Because one of the statements is True
print ( 3  <  2  or  4  <  3 )   # False - Because both statements are False
print ( 'True or False:' , True  or  False )
 print ( not  3  >  2 )      # False - Since 3 > 2 is True, then not True gives False
print (not  True )       # False - Negates the not operator that changes true to False
print ( not  False )      # True 
print ( not  not  True )   # True 
print ( not  not  False ) # False
Copy the code

If you have any help in learning Python, please bookmark it. For more information about Python learning experience and installation package, please email me, follow me and keep updating.