[Python] Use and and or when creating variables

How to use and and or did not know

The logical operators and, or not only output bool types such as True and False, but also output numerical values, character strings, lists, etc. depending on the conditions. </ b>

conditions

The expressions x and y first evaluate x; if x is false, return the value of x; otherwise, evaluate the value of y and return the result. The expression x or y first evaluates x; returns the value of x if x is true; otherwise it evaluates the value of y and returns the result. 6. expression — Python 3.8.3 documentation

The objects that are considered False are:
  • bool type False
  • None
  • Numerical value (int type or float type) 0, 0.0
  • Empty string''
  • Empty containers (lists, tuples, dictionaries, etc.) [], (), {}

Example

x = 5  # True
y = 0  # False

print(x and y)
# 0

print(x or y)
# 5

print(not x)
# False
x = 10  # True
y = 100  # True

print(x and y)
# 100

print(y and x)
# 10

print(x or y)
# 10

Reference site [https://note.nkmk.me/python-boolean-operation/:embed:cite]

Recommended Posts

[Python] Use and and or when creating variables
Use pydantic when reading environment variables in Python
Python: Class and instance variables
About Python variables and objects
Python class variables and instance variables
Python bitwise operator and OR
Python variables and object IDs
Python or and and operator trap
[Python / matplotlib] Understand and use FuncAnimation
Read and use Python files from Python
Precautions when creating a Python generator
[Python] return A [or / and] B
Use gcc-4.2 when compiling Python (MacOS)
[Python] Variables
How to use Serverless Framework & Python environment variables and manage stages
[Python] When you want to use all variables in another file
[Introduction to Python] How to use the Boolean operator (and ・ or ・ not)
Use pytest-qt when pytesting PySdie and PyQt
How to install and use pandas_datareader [Python]
Note when creating an environment with python
Connect a lot of Python or and and
Use PIL and Pillow with Cygwin Python
python: How to use locals () and globals ()
How to use Python zip and enumerate
How to use is and == in Python
Use Python and MeCab with Azure Functions
Recommended environment and usage when developing with Python
Firebase: Use Cloud Firestore and Cloud Storage from Python
For Python 3.4 or later, you should just throw away os.path and use pathlib
Python variables and data types learned in chemoinformatics
[python] Difference between variables and self. Variables in class
Use OpenSeesPy regardless of OS or Python version
[Python] Chapter 02-01 Basics of Python programs (operations and variables)
Practice applying functions and global variables in Python
Trouble with Python pseudo-private variables and class inheritance
A memo when creating a python environment with miniconda
[Python for Hikari] Chapter 09-02 Classes (Creating and instantiating classes)
python development environment -use of pyenv and virtualenv-
[Python] How to use hash function and tuple.
Use Python and word2vec (learned) with Azure Databricks
[Question] What happens when I use% in python?
Use python on Raspberry Pi 3 and turn on the LED when it gets dark!
Use os.getenv to get environment variables in Python
Python> Sort by number and sort by alphabet> Use sorted ()
Use data class for data storage of Python 3.7 or higher
Getting Started with python3 # 2 Learn about types and variables
[Grasshopper] When creating a data tree on Python script
[Python] Boolean operator (or / and) does not return Boolean value
[Python] [Django] How to use ChoiceField and how to add options
Python: Creating a virtual environment (venv), starting and stopping
Try creating a compressed file using Python and zlib
Findings when accelerating numerical calculations with Python and Numba
Difference between Ruby and Python in terms of variables
Include and use external Kv files in Python Kivy
Tips and precautions when porting MATLAB programs to Python
Problems when creating a csv-json conversion tool with python
% And str.format () in Python. Which one do you use?
Julia Quick Note [01] How to use variables and constants
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Docker-lambda and yumda are useful when creating Lambda Layers
Use communicate () when receiving output in a Python subprocess