Pharmaceutical company researchers summarized variables in Python

Introduction

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

Variable definition

When defining a variable, write "variable name = value". At this time, = (equal) does not mean that the left side and the right side are "equal", but that the left side and the right side are "equalized", that is, the right side is substituted for the left side. There may or may not be a space before and after the'=', but it is customary to insert a half-width space for easy viewing.

variable_1.py


var_str = 'Hello, World!'
var_int = 123
var_bool = True

Half-width alphanumeric characters and underscores ('_') can be used for variable names. You can use double-byte characters in Python3, but you won't have much chance to use them. When the variable name is more than 2 words, it is common to connect them with an underscore. Try to give the variable a name so that anyone can easily see what value is stored. However, the variable name must not be a reserved word (a word already defined inside Python) or start with a number.

variable_2.py


print = 'Hello, World!' #There is no error at this point, but when I run the print function I get an error.
1num = 123 #An error will occur.

In the above print example, if you write it a little difficult, you will get the image that the object called print was originally a function, but it is replaced with a variable and cannot be executed as a function.

As I write below, variables can be updated in value. On the other hand, when defining a variable (constant) on the assumption that the value will not be updated, it will be easier to understand that the value is not updated by making the variable name all uppercase.

constant.py


CONST = 'constant' #The value can be updated according to the specifications.

Variable value reference

You can refer to the value by specifying the variable name as an argument of the print function.

variable_3.py


var_str = 'Hello, World!'
print(var_str)

var_int = 123
print(var_int)

var_bool = True
print(var_bool)

print('var_str') # var_It is output as str.

Note that if you enclose the variable name in quotation marks, it will be output as the character string of the variable name itself, not the value of the variable.

Update the value of a variable

You can update the value of a variable by setting "variable name = value" for the variable that has already been defined.

variable_4.py


var = 'Hello, World!'
print(var) #Definition
var = 'Hello, Python!'
print(var) #update

Summary

Here, I explained variables in Python. Since it is used in various situations, I think it is good to deepen your understanding while actually writing a script.

Reference materials / links

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

Recommended Posts

Pharmaceutical company researchers summarized variables in Python
Pharmaceutical company researchers summarized classes in Python
Pharmaceutical company researchers summarized functions in Python
Pharmaceutical company researchers summarized regular expressions in Python
Pharmaceutical company researchers summarized file scanning in Python
Pharmaceutical company researchers summarized Python control statements
Pharmaceutical company researchers summarized Python unit tests
Pharmaceutical company researchers summarized Python exception handling
Pharmaceutical company researchers summarized Python coding standards
Pharmaceutical company researchers have summarized the operators used in Python
Pharmaceutical company researchers summarized SciPy
Pharmaceutical company researchers summarized RDKit
Pharmaceutical company researchers summarized scikit-learn
Pharmaceutical company researchers summarized web scraping using Python
Pharmaceutical company researchers summarized Pandas
Pharmaceutical company researchers summarized database operations using Python
Pharmaceutical company researchers summarized Matplotlib
Pharmaceutical company researchers summarized Seaborn
Pharmaceutical company researchers summarized Python's comprehensions
Pharmaceutical company researchers summarized Python's data structures
How to install Python for pharmaceutical company researchers
Handle environment variables in Python
Using global variables in python functions
A pharmaceutical company researcher summarized the basic description rules of Python
[Python] Variables
[Python3] Dynamically define global variables in functions
How to access environment variables in Python
How to dynamically define variables in Python
To reference environment variables in Python in Blender
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Python variables and data types learned in chemoinformatics
Unittest in python
[python] Difference between variables and self. Variables in class
Epoch in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Practice applying functions and global variables in Python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.