Python code memo for yourself

This is my own Python code memo. I have been in Python for about 2 years. I recently realized that if I didn't make a note, I would forget to look it up many times ... By the way, it's my first article on Qiita, so I scribbled it without worrying about the appearance. If you can afford it, I would like to add it by looking at the past code. (Write only the items that you have done) I'm not good at object orientation. I'm studying Django. We plan to make a LINE chatbot app.

Conditional branch

between

a = 10
# print(2 <= a and a <= 15)
print(2 <= a <= 15)

⇒true

Ternary operator

a = 10
# (Value at True) if (Conditional expression) else (Value at False)
b = 0 if a % 2 == 0 else 1
print(b)

⇒0

list

Reverse loop

for i in reversed(range(5):
  print(i)

⇒4, 3, 2, 1, 0

Is it in the list

l = ["a", "b", "c"]
print("a" in l, "d" in l)

⇒true, false

Search and return index

l = ["a", "b", "c"]
print(l.index("a"))
print(l.index("d"))

⇒0, ValueError

Initialize with any value

print([0] * 5)
print([0] * 3 + [1] * 2)
print([[0] * 3 for i in range(3)])
# [[0] * 3] * 3]Then all the rows will be the same object

⇒[0, 0, 0, 0, 0], [0, 0, 0, 1, 1] [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]

Add element

l1_1 = [0, 1, 2]
l1_2 = [0, 1, 2]
l2 = [3, 4]

l1_1.append(l2)
l1_2.extend(l2)

⇒[0, 1, 2, [3, 4]], [0, 1, 2, 3, 4]

Randomly sorted

import random

l = list(range(5))

#Sort the original list
random.shuffle(l)
print(l)

#Create a new sorted list
l_new = random.sample(l, len(l))
print(l_new)

⇒Example) [3, 4, 1, 0, 2], [2, 1, 0,, 4, 3]

Numerical processing

Round, round, round up

#Rounding: round(Numerical value,Number of digits you want to round)
print(round(1.2345, 2))

import math
#The number of digits cannot be specified
#Truncate
print(math.floor(1.2345))
#Round up
print(math.ceil(1.2345))

⇒1.23, 1, 2

String processing (regular expressions, etc.)

Pandas

Row-by-line loop

for index, row in dataframe.iterrows(): 
  print(row[n])

⇒The element in the nth column of the index row is output.

tkinter

File processing

Get the file list in list format by specifying the conditions

import glob
l = glob.glob("directory/*.csv")

⇒ Get the file name list of csv files under directory in list format

Natural language processing (Cabocha, etc.)

Acoustic analysis

Image processing (OpenCV)

Web scrap

Statistical processing

Automation

Batch file for task scheduler

cd /d %~dp0
python test.py

⇒Move to the directory where test.py is (it is useless without this) and execute

Error resolution

Recommended Posts

Python code memo for yourself
Python memo (for myself): Array
[Python] Sample code for Python grammar
Python memo
python memo
Python memo
python memo
Python memo
Python memo
Python memo
python [for myself]
[Python] Memo dictionary
python beginner memo (9.2-10)
python beginner memo (9.1)
python character code
★ Memo ★ Python Iroha
[Python] EDA memo
Python 3 operator memo
python> coding guide> PEP 0008 --Style Guide for Python Code
Anchoco for yourself
[Python] Algorithm-aware code
A tool for easily entering Python code
[My memo] python
Python3 metaclass memo
[Python] Basemap memo
Python beginner memo (2)
R code compatible sheet for Python users
[Python] Numpy memo
Memo # 4 for Python beginners to read "Detailed Python Grammar"
Memo # 3 for Python beginners to read "Detailed Python Grammar"
Memo # 1 for Python beginners to read "Detailed Python Grammar"
Memo # 2 for Python beginners to read "Detailed Python Grammar"
Code for checking the operation of Python Matplotlib
Memo # 7 for Python beginners to read "Detailed Python Grammar"
Settings for Python coding in Visual Studio Code
A memo for creating a python environment by a beginner
Memo # 6 for Python beginners to read "Detailed Python Grammar"
Memo for editing scenes with Blender python (W.I.P.)
Memo # 5 for Python beginners to read "Detailed Python Grammar"
[Visual Studio Code] [Python] Tasks.json + problemMatcher settings for Python
Python class (Python learning memo ⑦)
My python environment memo
python openCV installation (memo)
Python basics ② for statement
Visualization memo by Python
Reboot yourself in Python 3
Python code acceleration approach
Rewrite Python2 code to Python3 (2to3)
infomap python draw code
[Python] Memo about functions
Before writing Python code
About Python, for ~ (range)
Techniques for code testing?
python regular expression memo
Binary search (python2.7) memo
[My memo] python -v / python -V
python textbook for beginners
About Python3 character code
Python3 List / dictionary memo
[Memo] Python3 list sort
Python Tips (my memo)