Pharmaceutical company researchers have summarized the operators used in Python

Introduction

Here, we will explain "operators" for Python beginners. It is assumed that you are using Python 3 series.

Arithmetic operator

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).

Assignment operator

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)

Comparison operator

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)

Logical operator

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)

Summary

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.

Reference materials / links

What is the programming language Python? Can it be used for AI and machine learning?

Recommended Posts

Pharmaceutical company researchers have summarized the operators used in Python
Pharmaceutical company researchers summarized classes in Python
Pharmaceutical company researchers summarized functions in Python
Pharmaceutical company researchers summarized variables in Python
Pharmaceutical company researchers summarized regular expressions in Python
Pharmaceutical company researchers summarized file scanning in Python
Pharmaceutical company researchers summarized Python unit tests
Pharmaceutical company researchers summarized Python exception handling
Pharmaceutical company researchers summarized Python coding standards
Pharmaceutical company researchers summarized web scraping using Python
Pharmaceutical company researchers summarized database operations using Python
Pharmaceutical company researchers summarized scikit-learn
Pharmaceutical company researchers summarized NumPy
Pharmaceutical company researchers summarized Matplotlib
Pharmaceutical company researchers summarized Seaborn
Pharmaceutical company researchers summarized Python's comprehensions
A pharmaceutical company researcher summarized the basic description rules of Python
How to install Python for pharmaceutical company researchers
Note the frequently used options in Python + Selenium + Chrome
Operators ++,-cannot be used in python (difference from php)
Download the file in Python
Have the equation graph of the linear function drawn in Python
Nesting ternary operators in python
We have summarized the tech conferences scheduled to be held in 2020
New features in Python 3.9 (1)-Union operators can be used in dictionary types
[Python] Do not put Japanese in the path used by OpenCV
Getting the arXiv API in Python
8 Frequently Used Commands in Python Django
Python in the browser: Brython's recommendation
Save the binary file in Python
Get the desktop path in Python
Get the script path in Python
In the python command python points to python3.8
Implement the Singleton pattern in Python
Have python read the command output
I wrote the queue in Python
Calculate the previous month in Python
Examine the object's class in python
Get the desktop path in Python
Get the host name in Python
Access the Twitter API in Python
The first step in Python Matplotlib
I wrote the stack in Python
Master the weakref module in Python
[Introduction to Python] Thorough explanation of the character string type used in Python!
Learn the design pattern "Prototype" in Python
Learn the design pattern "Builder" in Python
Load the remote Python SDK in IntelliJ
Try using the Wunderlist API in Python
Check the behavior of destructor in Python
Try using the Kraken API in Python
Learn the design pattern "Observer" in Python
Learn the design pattern "Proxy" in Python
Write the test in a python docstring
Learn the design pattern "Command" in Python
OR the List in Python (zip function)
[python] Frequently used techniques in machine learning
Display Python 3 in the browser with MAMP
Tweet using the Twitter API in Python
Learn the design pattern "Visitor" in Python
Learn the design pattern "Bridge" in Python