Operators(Python)

Japanese version.

Python's operators are used to manipulate values in a program.

How to Use

Arithmetic Operators

Arithmetic operators are operators used for numerical operations, and there are six of them:

  • Addition operator (+): Adds two numbers together.
  • Subtraction operator (-): Subtracts the right operand from the left operand.
  • Multiplication operator (*): Multiplies two numbers together.
  • Division operator (/): Divides the left operand by the right operand.
  • Floor division operator (//): Divides the left operand by the right operand and returns the integer part of the result.
  • Modulo operator (%): Divides the left operand by the right operand and returns the remainder.

Here are some examples of how to use arithmetic operators:

x = 10
y = 3

print(x + y)  # 13
print(x - y)  # 7
print(x * y)  # 30
print(x / y)  # 3.3333333333333335
print(x // y) # 3
print(x % y)  # 1

Additionally, there is also an exponentiation operator (**), which returns the result of raising the left operand to the power of the right operand.

x = 2
y = 3

print(x ** y)  # 8

Comparison Operators

Comparison operators are used to compare two values, and there are six operators:

  • Equal to (==): Returns True if the left side is equal to the right side.
  • Not equal to (!=): Returns True if the left side is not equal to the right side.
  • Greater than (>): Returns True if the left side is greater than the right side.
  • Greater than or equal to (>=): Returns True if the left side is greater than or equal to the right side.
  • Less than (<): Returns True if the left side is less than the right side.
  • Less than or equal to (<=): Returns True if the left side is less than or equal to the right side.

Here is an example of using comparison operators:

x = 5
y = 3

print(x == y)  # False
print(x != y)  # True
print(x > y)   # True
print(x >= y)  # True
print(x < y)   # False
print(x <= y)  # False

Comparison operators are commonly used for evaluating conditions or controlling the loop of a program. For example, they can be used in the condition of an if statement:

if x > y:
    print("x is greater than y")
else:
    print("x is less than or equal to y")

Logical operators

Logical operators are used to combine multiple logical expressions and return a boolean value. Python has three logical operators:

  • and (logical and): Returns True if both expressions are True.
  • or (logical or): Returns True if at least one expression is True.
  • not (logical not): Inverts the result of the expression. True becomes False, and False becomes True.

Here are some examples of using logical operators:

x = 5
y = 3
z = 7

print(x < y and x < z)   # False
print(x < y or x < z)    # True
print(not x < y)         # True

Logical operators are often used in control structures such as conditional statements and loops. For example, here is how to use the and operator in an if statement:

x = 5
y = 3
z = 7

if x < y and x < z:
    print("x is the smallest")
elif y < z:
    print("y is the smallest")
else:
    print("z is the smallest")

In this code, the and operator is used to determine if x is the smallest, or if either y or z is the smallest.

Assignment Operators

Assignment operators are used to assign values on the right side to variables on the left side; Python has several assignment operators

  • =: assigns a value.
  • +=: assigns the value of the right-hand side to the variable on the left-hand side by adding it to the variable on the right-hand side.
  • -=: subtracts the value of the right-hand side from the left-hand side variable and assigns it to it.
  • *=: Assigns the value on the right-hand side to the variable on the left-hand side by multiplying it.
  • /=: Substitutes the value on the right-hand side by dividing it by the variable on the left-hand side.
  • //=: Substitutes the value on the right-hand side for the value on the left-hand side divided by an integer.
  • %=: Substitutes the value on the right-hand side for the value on the left-hand side using the remainder operation.
  • **=: Assigns the value on the right-hand side to the variable on the left-hand side by exponentiation.

The following are examples of assignment operator usage.

x = 5
x += 2  
print(x)

y = 10
y //= 3 
print(y)

Assignment operators can improve code brevity and reduce the number of types. Some assignment operators can also optimize the computation process and improve performance.

For those who want to learn Python efficiently.

The information on this site is now available in an easy-to-read e-book format for $3.00.

Or Kindle Unlimited (unlimited reading).

This textbook is used for new employees with no programming experience.

This book focuses on basic programming topics.

We do not cover topics such as object-oriented programming, statistical analysis using Python-specific libraries, machine learning, as these topics are difficult to understand from the standpoint of initial programming learning.

This book is especially recommended for those who have no experience with programming.

Links

Python Articles

Python