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.
between
a = 10
# print(2 <= a and a <= 15)
print(2 <= a <= 15)
⇒true
a = 10
# (Value at True) if (Conditional expression) else (Value at False)
b = 0 if a % 2 == 0 else 1
print(b)
⇒0
for i in reversed(range(5):
print(i)
⇒4, 3, 2, 1, 0
l = ["a", "b", "c"]
print("a" in l, "d" in l)
⇒true, false
l = ["a", "b", "c"]
print(l.index("a"))
print(l.index("d"))
⇒0, ValueError
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]]
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]
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]
#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
Pandas
for index, row in dataframe.iterrows():
print(row[n])
⇒The element in the nth column of the index row is output.
tkinter
import glob
l = glob.glob("directory/*.csv")
⇒ Get the file name list of csv files under directory in list format
cd /d %~dp0
python test.py
⇒Move to the directory where test.py is (it is useless without this) and execute
Recommended Posts