A pharmaceutical company researcher summarized the basic description rules of Python

Introduction

Here, we will introduce the grammar and syntax that people who have started studying the programming language Python should first learn. It is assumed that you are using Python 3 series.

How to make a comment

comment.py


#Comment on the first line#It is also possible to write a comment from the middle of the line.
Comment on the second line#An error will occur.

'''
Multi-line comment
Multi-line comment
'''

Note that if you break a comment that starts with'#', the second and subsequent lines will be recognized as the execution range of the program, and an error may occur or unexpected behavior may occur. It is recommended that one line be 79 characters or less, including the script of the program execution range. You can write a comment as a memo of the script, and you can also use it to prevent the script you do not want to execute from being executed (comment out). When you write a "multi-line comment" in a function, it becomes an object that shows the explanation of the function, and it has a slightly different form from the comment mentioned here.

Character output

Characters obtained as a result of executing the program can be output by using the print function.

print.py


print('Hello, World!') # ă€ŒHello, World!Is output.

Data type

Like other programming languages, Python has data types. If you do not write the script while being aware of this data type, the program will not work and an error will occur, or unexpected behavior will occur.

String

Literally, it refers to the general "letter".

string.py


print('Hello, World!')
print("Hello, World!")
#It can be enclosed in single quotes or double quotes, but it is common to enclose it in single quotes.

#It is NG to start with single quotes and close with double quotes (or vice versa).
print('Hello, World!") #An error will occur.
print("Hello, World!') #An error will occur.
print('Say "Hello, World!"') #This is OK. If you want to use more quotation marks in the string, enclose them in quotation marks that you haven't used yet.
print("Say 'Hello, World!'") #This is also OK. However, as mentioned above, it is common to enclose the character string in single quotation marks, so it is more common to write it one line above.

Numerical value

It refers to a "numerical value" that can be calculated arithmetically. Arithmetic calculations cannot be performed if the character string is a "number".

number.py


print(123) #Write the numbers as they are, without enclosing them in quotation marks. This is an integer type.
print(123.4567) #You can also write with a decimal point (called a floating point number type).
print('123') #If you write it like this, it will be a "character string of numbers" instead of numbers (arithmetic operations cannot be performed accurately).
print(str(123)) #Conversion from number to string. The output result is the same as the one above.
print(int('123')) #Convert from a character string to a number (integer). int is an abbreviation for integer.
print(float('123.4567')) #Convert from string to floating point type. float means floating point.
print(int(123.4567)) #Floating point types cannot be converted to integer types, resulting in an error.

Data type conversion is confusing in the above example, but it often comes up when dealing with variables. For example, if a number is stored as a value in a variable that is inevitably defined as a character string, and you want to treat it as a number.

Boolean value (boolean)

It can be either'True'or'False'.

boolean.py


print(True) #"true"
print(False) #"false"

No quotations required. Write only the first letter in uppercase and the second and subsequent letters in lowercase. It is closely related to the if statement.

Indent

Indentation has the effect of making it easier to see the mass of scripts (processes). Python is a programming language that emphasizes indentation, and it should be noted that an error will occur if there is excess or deficiency of indentation. This is especially important for if and for statements, and the behavior may change depending on the presence or absence of indentation.

indent.py


print('Hello, World!') # OK
    print('Hello, World!') #An example of writing with indentation for 4 single-byte characters. Simply writing this much will result in an error.

Summary

Here, we explained how to output the execution result of the program and the data type. If you are new to programming, you should understand it well.

Reference materials / links

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

Recommended Posts

A pharmaceutical company researcher summarized the basic description rules of Python
Summary of Python articles by pharmaceutical company researcher Yukiya
Pharmaceutical company researchers have summarized the operators used in Python
A good description of Python decorators
A memorandum regarding the acquisition of the Python3 engineer certification basic exam
Pharmaceutical company researchers summarized Python control statements
Pharmaceutical company researchers summarized Python unit tests
Pharmaceutical company researchers summarized classes in Python
Pharmaceutical company researchers summarized functions in Python
Pharmaceutical company researchers summarized Python exception handling
[python] [meta] Is the type of python a type?
[Python] I personally summarized the basic grammar.
Pharmaceutical company researchers summarized Python coding standards
Python Basic Course (at the end of 15)
The story of blackjack A processing (python)
Pharmaceutical company researchers summarized variables in Python
Pharmaceutical company researchers summarized regular expressions in Python
Get the caller of a function in Python
Make a copy of the list in Python
A note about the python version of python virtualenv
Pharmaceutical company researchers summarized web scraping using Python
This is the only basic review of Python ~ 1 ~
This is the only basic review of Python ~ 2 ~
Pharmaceutical company researchers summarized file scanning in Python
[Python] A rough understanding of the logging module
Output in the form of a python array
Pharmaceutical company researchers summarized database operations using Python
This is the only basic review of Python ~ 3 ~
A discussion of the strengths and weaknesses of Python
Get the stock price of a Japanese company with Python and make a graph
[Python] A program that counts the number of valleys
Cut a part of the string using a Python slice
Make a note of the list of basic Pandas usage
the zen of Python
A python implementation of the Bayesian linear regression class
Python points from the perspective of a C programmer
Summary of the basic flow of machine learning with Python
Basic knowledge of Python
Tasks at the start of a new python project
Impressions of taking the Python 3 Engineer Certification Basic Exam
A reminder about the implementation of recommendations in Python
[Introduction to Python] Basic usage of the library matplotlib
[Python] A program that compares the positions of kangaroos.
Python Note: The mystery of assigning a variable to a variable
Find out the apparent width of a string in python
A simple Python implementation of the k-nearest neighbor method (k-NN)
I just changed the sample source of Python a little.
I wrote the basic grammar of Python with Jupyter Lab
Different from the import type of python. from A import B meaning
Get the number of specific elements in a python list
[Note] Import of a file in the parent directory in Python
A memo of writing a basic function in Python using recursion
Find the eigenvalues of a real symmetric matrix in Python
A Python script that compares the contents of two directories
Towards the retirement of Python2
Pharmaceutical company researchers summarized SciPy
Pharmaceutical company researchers summarized RDKit
About the ease of Python
Pharmaceutical company researchers summarized scikit-learn
Pharmaceutical company researchers summarized Pandas
Pharmaceutical company researchers summarized NumPy