Conditional branching of Python learned by chemoinformatics

Introduction

Following on from Python data structure learned by chemoinformatics, I will explain "conditional branching" with the theme of lipidomics (comprehensive analysis of lipids). We will mainly explain practical examples of chemoinformatics, so if you want to check the basics, please read the following article before reading this article.

Pharmaceutical researcher summarized Python control statements

if statement

As ʻif conditional expression:, describe the processing when the conditional expression is satisfied on the next line. The next line of ʻif starts writing with indentation of 4 single-byte characters.

Un = 0

if Un == 0:
    print('saturated fatty acid')
else:
    print('unsaturated fatty acid')

ʻElse is used to describe the processing when the conditional expression following ʻif does not hold. If you want to divide the condition more finely, you can describe the processing when another condition is satisfied by using ʻelif conditional expression:. By the way, ʻelif is an abbreviation for "else if". In the above program, if the variable ʻUn indicating the degree of unsaturation (the number of double bonds of fatty acids) is 0, it is output as saturated fatty acid (saturated fatty acid), and ʻUn is other than 0. If it is a numerical value, it is output as ʻunsaturated fatty acid` (unsaturated fatty acid).

Logical operator

It is possible to specify multiple conditions in the conditional expression part.

Cn = 18
Un = 0

if Cn == 16 and Un == 0:
    print('palmitic acid')
elif Cn == 18 and Un == 0:
    print('stearic acid')
else:
    print('other fatty acid')

In the conditional expression after ʻif, ʻand means something like "katsu". If you want to use "or", use ʻor`.

in operator

You can use the ʻin` operator to determine if an element is in the list.

fatty_acids = ['FA 16:0', 'FA 18:0', 'FA 18:1']

if 'FA 16:0' in fatty_acids:
    print('Palmitic acid is included')
else:
    print('Palmitic acid is not included')

Application: SMILES notation

Finally, as an application, let's consider determining whether it is a saturated fatty acid or an unsaturated fatty acid from the chemical structure described in SMILES notation.

smiles_fa = 'OC(CCCCCCCCCCCCCCC)=O'

if smiles_fa.count('=') <= 1:
    print('saturated fatty acid')
else:
    print('unsaturated fatty acid')

Whether it is an unsaturated fatty acid or not can be determined by whether or not the carbon chain contains a double bond. Since the carboxylic acid moiety also has a double bond, the above program determines if there are other double bonds.

Summary

Here, I explained about conditional branching in Python, focusing on practical knowledge that can be used in chemoinformatics. Let's review the main points again.

--The if statement describes the processing when the conditional expression is satisfied after breaking the line as ʻif conditional expression: and indenting four half-width spaces. If you want to subdivide the conditions, you can also use ʻelse and ʻelif. --You can also use logical operators such as ʻand and ʻor in conditional expressions. --You can use the ʻin operator to determine if the specified element is included in the array.

Next, the following article explains Python iterative processing.

Iterative processing of Python learned by chemoinformatics

Reference materials / links

Surprisingly few! ?? "Minimum" knowledge required for programming in a pharmaceutical company

Recommended Posts

Conditional branching of Python learned by chemoinformatics
Python classes learned in chemoinformatics
Python functions learned in chemoinformatics
[Python] Eliminate conditional branching by if by making full use of Enum and eval
What I learned by solving 30 questions of python Project Euler
[Python of Hikari-] Chapter 05-07 Control syntax (conditional branching of comprehension notation)
Python data structures learned with chemoinformatics
Behavior of python3 by Sakura's server
Story of power approximation by Python
[Introduction to Data Scientists] Basics of Python ♬ Conditional branching and loops
Explanation of production optimization model by Python
[Learning memo] Basics of class by python
Algorithm learned with Python 8th: Evaluation of algorithm
Grayscale by matrix-Reinventor of Python image processing-
Example of 3D skeleton analysis by Python
Pandas of the beginner, by the beginner, for the beginner [Python]
1. Statistics learned with Python 1-3. Calculation of various statistics (statistics)
Analysis of X-ray microtomography image by Python
Introduction of Python
VBA user tried using Python / R: conditional branching
Python variables and data types learned in chemoinformatics
Algorithm learned with Python 13th: Tower of Hanoi
A memorandum of extraction by python bs4 request
Basics of Python ①
1. Statistics learned with Python 1-2. Calculation of various statistics (Numpy)
Basics of python ①
Copy of python
Deep learning learned by implementation (segmentation) ~ Implementation of SegNet ~
Introduction of Python
"Principle of dependency reversal" learned slowly with Python
[Language processing 100 knocks 2020] Summary of answer examples by Python
Image processing by matrix Basics & Table of Contents-Reinventor of Python image processing-
List of posts related to optimization by Python to docker
[Programming learning] Logic comparison by language part.3 (conditional branching)
Summary of Python articles by pharmaceutical company researcher Yukiya
Derivatives Learned Using Python-(1) Calculation of Forward Exchange Rate-
Group by consecutive elements of a list in Python
Paiza Python Primer 2: Learn Conditional Branching and Comparison Operators
[Python] Chapter 05-01 Control syntax (comparison operator and conditional branching)
Memo of "Cython-Speeding up Python by fusing with C"
Primality test by Python
[Python] Operation of enumerate
List of python modules
Communication processing by Python
Unification of Python environment
[python] behavior of argmax
Usage of Python locals ()
the zen of Python
Installation of Python 3.3 rc1
# 4 [python] Basics of functions
Beamformer response by python
Basic knowledge of Python
Sober trivia of python3
Summary of Python arguments
Basics of python: Output
Installation of matplotlib (Python 3.3.2)
Application of Python 3 vars
Various processing of Python
[For beginners] Basics of Python explained by Java Gold Part 2
[Python] How to make a list of character strings character by character
Sample source of Observer pattern realized by Java, PHP, Python