Tips for coding short and easy to read in Python

Hello, this is ricky. This time, I will show you how to make the code as easy as possible and how to shorten it when coding with python. I hope python beginners can write better code.

num.py


# before
num = 10000000

# after
num = 10_00_000

swap.py


# before
a, b = 2, 3
tmp = b
b = a
a = tep

# after
a, b = 2, 3
a, b = b, a

The following is a short way to write, but it impairs readability.

Since python requires code that is easy for anyone to write, it is basically better not to write it. However, since it can be shortened, it is useful in situations where a short number of lines is required.

variable.py


# before
a = 1
b = 2

# after
a, b = 1, 2

# supplement
#It is possible to use different types
a, b = 3, "b"

#Postscript: You pointed out from the comments.
#Writing after may reduce readability.
#Vector x,Better to use for highly relevant variables such as the y coordinate

if.py


a, b = 1, 2

# before
if a < b:
    print("A")
else:
    print("B")

# after
print("A") if a < b else print("B")

#Postscript: You pointed out from the comments.
#Can be summarized by print
print("A" if a < b else "B")

# and,You can use or to bring the conditional expression forward
print(a < b and "A" or "B")

Summary When I was coding in python, when I reviewed my code later, I felt that it was not readable, so I wrote this article. Personally, how to write an if statement was very helpful. I hope you find this article useful.

Recommended Posts

Tips for coding short and easy to read in Python
~ Tips for beginners to Python ③ ~
Tips for those who are wondering how to use is and == in Python
Useful tricks related to list and for statements in Python
Create and read messagepacks in Python
Regular expressions that are easy and solid to learn in Python
Read big endian binary in Python and convert it to ndarray
Tips to make Python here-documents easier to read
Tips for dealing with binaries in Python
Easy way to use Wikipedia in Python
Read Python csv and export to txt
Read and write JSON files in Python
Tips for making small tools in python
How to use is and == in Python
Python for super beginners Python for super beginners # Easy to get angry
Techniques often used in python short coding (Notepad)
Memo # 3 for Python beginners to read "Detailed Python Grammar"
[Python] How to read data from CIFAR-10 and CIFAR-100
Memo # 1 for Python beginners to read "Detailed Python Grammar"
Send messages to Skype and Chatwork in Python
Tips for hitting the ATND API in Python
Try to calculate RPN in Python (for beginners)
Memo # 2 for Python beginners to read "Detailed Python Grammar"
Memo # 7 for Python beginners to read "Detailed Python Grammar"
Settings for Python coding in Visual Studio Code
Memo # 6 for Python beginners to read "Detailed Python Grammar"
Memo # 5 for Python beginners to read "Detailed Python Grammar"
To represent date, time, time, and seconds in Python
How to plot autocorrelation and partial autocorrelation in python
Steps to put dlib in Python Tools for Visual Studio and have fun
Difference in writing method to read external source code between Ruby and Python
Read DXF in python
Python and numpy tips
Rock-paper-scissors poi in Python for beginners (answers and explanations)
Tool to make mask image for ETC in Python
[For beginners] How to use say command in python!
How to run python in virtual space (for MacOS)
Python netCDF4 read speed and nesting of for statements
Preferences for playing Wave in Python PyAudio and PortAudio
Problems and countermeasures for Otsu's binarization overflow in Python
Tips and precautions when porting MATLAB programs to Python
Read and write single precision floating point in Python
It's not easy to write Python, it's easy to write numpy and scipy
Write tests in Python to profile and check coverage
[Python] How to sort dict in list and instance in list
Deep nesting in Python makes it hard to read
Read and write NFC tags in python using PaSoRi
Python # How to check type and type for super beginners
Recursively search for files and directories in Python and output
Expose settings.json for efficient Python coding in VS Code
How to read csv containing only integers in Python
It is easy to execute SQL with Python and output the result in Excel
Script to count and stop up to 5 seconds in Python in Blender
Tips for Python beginners to use Scikit-image examples for themselves 4 Use GUI
To flush stdout in Python
List method argument information for classes and modules in Python
Tips for developing apps with Azure Cosmos DB in Python
How to swap elements in an array in Python, and how to reverse an array.
Python regular expression basics and tips to learn from scratch
Login to website in Python
Read Euler's formula in Python