Site icon Kaizen.Personal computer work.

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:

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:

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:

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

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
Exit mobile version