Here, we will explain "operators" for Python beginners. It is assumed that you are using Python 3 series.
An operator for performing four arithmetic operations. Half-width spaces are often inserted before and after the operator.
operator_1.py
int_1 = 1 + 1 #addition
print(int_1)
int_2 = 2 - 1 #subtraction
print(int_2)
int_3 = 2 * 3 #multiplication
print(int_3)
int_4 = 6 / 3 #Division (even if it is divisible, the calculation result will be a floating point number type)
print(int_4)
int_5 = 2 ** 3 #Exponentiation
print(int_5)
int_6 = 5 // 2 #Division quotient
print(int_6)
int_7 = 5 % 2 #Remainder of division
print(int_7)
If you add the strings together, you get a combination of the strings. Also, if you multiply the character string by a number, the character string will be repeated only for the number sentence.
operator_2.py
str_1 = 'Hello, '
str_2 = 'World!'
print(str_1 + str_2) # ă€ŒHello, World!Is displayed.
str_bye = 'Bye'
print(str_bye * 2) #"Bye Bye" is displayed.
var_int = 123
# print(str_1 + var_int) #If the strings are not added together, an error will occur.
print(str_1 + str(var_int)) #If you convert a numerical value to a character string, you can add (combine character strings).
This operator is used when defining and updating variables. There is no "assignment operator" in Python, but it is a concept in other programming languages, so I will write it for the time being.
operator_3.py
var_int = 3
print(var_int)
var_int = var_int + 2 #Original var_Add 2 to the value of int to update the value of num.
print(var_int)
var_int = var_int - 2 #Original var_Update the value of num by subtracting 2 from the value of int.
print(var_int)
var_int = var_int * 2 #Original var_Multiply the int value by 2 to update the num value.
print(var_int)
var_int = var_int / 2 #Original var_Divide the int value by 2 to update the num value.
print(var_int)
var_int = var_int % 2 #Original var_Update the value of num by calculating the remainder of dividing the value of int by 2.
print(var_int)
The above script can be rewritten as follows. This is the way to write because it can be written shorter.
operator_4.py
var_int = 3
print(var_int)
var_int += 2
print(var_int)
var_int -= 2
print(var_int)
var_int *= 2
print(var_int)
var_int /= 2
print(var_int)
var_int %= 2
print(var_int)
An operator used to compare the left and right sides.
operator_5.py
var_bool = 2 == 2 #Whether the left and right sides are equal
print(var_bool)
var_bool = 2 != 2 #Whether the left and right sides are not equal
print(var_bool)
var_bool = 2 < 3 #Whether the left side is smaller than the right side (the left side is less than the right side)
print(var_bool)
var_bool = 2 > 3 #Whether the left side is larger than the right side
print(var_bool)
var_bool = 2 <= 3 #Whether the left side is equal to or less than the right side (the left side is less than or equal to the right side)
print(var_bool)
var_bool = 2 >= 3 #Whether the left side is equal to or greater than the right side (the left side is greater than or equal to the right side)
print(var_bool)
An operator used for boolean values (boolean values).
operator_6.py
var_bool = 2 == 2 and 3 == 3 #Whether all the conditions connected by and are True
print(var_bool)
var_bool = 2 !=2 or 2 != 3 #Whether any of the conditions connected by or is True
print(var_bool)
var_bool = not (2 == 2 and 3 == 3) #True if the parentheses are False, False if the parentheses are True
print(var_bool)
This section describes operators in Python. It's something you use casually, so you don't have to worry too much about it, but it's a good idea to keep it in mind.
What is the programming language Python? Can it be used for AI and machine learning?
Recommended Posts