Write for yourself so that you don't forget the basics of pthon.
In all languages, the first thing to remember is "comment out".
python1.py
#Comment out
There seems to be other description methods, but it is enough to remember only #
Thrust (** [] ** after the variable) is a python-specific (?) Usage. It can be used quite well. Also, for python beginners, it looks like an array, which makes it difficult to decipher. Even if you remember the function before this, you will stumble on the slice.
python2.py
num="0123456789"
#Get "1st" from the left * When viewed from the left, the leftmost is "0"
a1=num[1]
# '1'
#Get "1st" from "Right" * When viewed from the right, the far right is ""-No. 1 "<I can understand this far
a2=num[-1]
# '9'
#Obtained "3rd to 5th" <Stumble around here. 5th instead of 5
a3=num[3:5]
# '34'
#Obtained "3rd ~" <":Pay attention to the relationship between "" and "..."
a4=num[3:]
# '3456789'
#Get "~ 3rd"
a5=num[:3]
# '012'
#Obtained by "skipping 2 pieces" * Since it starts from 0, it happened to be an even number
a6=num[::2]
# '02468'
#"Reverse order(In order from the back)Get to
a7=num[::-1]
# '9876543210'
In other words, the rule is ** [start: stop: step] **. (Minus is in reverse order)
python3.py
#Dare to fill in the slice and display it in the same way as the original (default value?)
a1=num[0::1] #a
# '0123456789'
#Can be used in combination
#Obtained by "reverse order" and "skip two". Set to "reverse order". * The end of the character string was 9 which was an odd number, so it happened to be an odd number.
a2=num[::-1][::2][::-1]
# '13579'
#It can also be used as a list type. Obtained by "skipping two"
numl=[0,1,2,3,4,5,6,7,8,9]
n1=numl[::2]
# [0, 2, 4, 6, 8]
** Reference ** If you want to know more about slices, this is a good choice. Explains the processing inside the sequence. https://qiita.com/tanuk1647/items/276d2be36f5abb8ea52e
A common feeling. it's simple.
python4.py
num="0123456789"
#Get the display position
foo=num.find("1")
# 1
#Search and convert to the specified character
foo=num.replace("1","a")
# '0a23456789'
abc1="abc"
abc2="abc"
#Simple comparison is easy with operators
abc1==abc2
# True
#Partial comparison
"bc" in abc1
# True
The point is that the delimiter is "blank"
python5.py
abc='a b c d e f'
#Convert from character string to list. * Blank delimiter
abcl = abc.split()
# ['a','b','c','d','e','f']
#list(letter)⇒letter列に変換する ※空白区切り
a1=" ".join(abcl)
# 'a,b,c,d,e,f'
numl=[0,1,2,3,4,5,6,7,8,9]
#list(Numbers)⇒Convert to a character string * Blank delimiter <The point is to make it str type in advance with map
n1=" ".join(map(str,numl))
# '0 1 2 3 4 5 6 7 8 9'
Click here for details on join split https://techacademy.jp/magazine/28688 https://note.nkmk.me/python-split-rsplit-splitlines-re/ https://hydrocul.github.io/wiki/programming_languages_diff/string/join.html
Easy to remember
python6.py
st="hello"
ed=" world"
#Combine two characters
l1=st+ed
# 'hello world'
#Erase extra white space
ed.strip()
# 'world'
Recommended Posts