[PYTHON] Learning record 2 (6th day)

Learning record (6th day)

Start studying: Saturday, December 7th Books used: Miyuki Oshige "Details! Python3 Introductory Note ”(Sotec, 2017)

[Conditional branching, repetition, exception handling (Ch.5 / p.129)] Resume from (4th day), Finished until [Sort elements in the list (Ch.6 / p.170)](6th day)

for statement with an else block

As with the while statement, the else block is executed when the iteration ends. It is not executed when the for statement breaks in the middle.

try statement (exception handling)

By handling the expected error in advance, the process is performed to the end without breaking in the middle. Incorporate exception objects with try ~ except. You can define more than one except below. By defining with as, the exception object can be referenced by other variables.

Make a list

numbers = [1, 2, 3, 4, 5, 6, 7, 8]
words = ["flowers", "snow", "moon"]
mixture = [1, 2, "flowers", 3, 5]  #Element types can be mixed.
num = [0] * 5 = [0, 0, 0, 0, 0]

It is possible to list convert values of other types by using list (). Type list (range (-5, 6)) to get a list of values in the specified range. (In this case, -5 to 6) If you type list ("ABCDE"), it will be divided character by character like "A", "B", "C", ...

List element references and updates

words = ["flowers", "snow", "moon"] If you type words [1], the first "snow" in the list will be output. Type words [1] = "wind" to replace "snow" with "wind". You can check the number of elements that are the length of the list with len (). If it is len (words), it is 3, but since the index number starts from 0, for example, word [3] gives an error.

Add and insert elements in the list

Append with append (value). Since it is a list object method, it is necessary to prepare a list in advance. Insert with insert (position, value). The insertion location conforms to the index number. Pull out with pop (pull out position). Since pop returns the deleted value at the same time as deleting it, it is an image to be taken out of the box. remove removes. If there are multiple values you want to delete, delete only the first value found. If you want to erase everything, combine it with a while statement.

Mutual conversion between strings and lists

Split by string.split (separator). A separator is a division standard in a sentence (understood). If usa = "yes we can", type words = usa.split () based on the space between characters. Divided into each word. If usa = "yes, we, can", usa.split (,) will be the separator. If spaces or commas are mixed, you can replace it with replace. (Separator) .join (list) allows you to join each element of the list with a separator. If you type AMERICA = OBAMA.join (usa), you will get AMERICA = "YES ** OBAMA ** WE ** OBAMA ** CAN" ".

List concatenation, slicing, duplicating, comparing

Lists can be concatenated with the + operator. It can also be combined with extend (). Whereas append is the append of an element, extend is the list itself. EEIAA = ["kimi", "kara", "morainaki"] With EEIAA [1:] ["kara", "morainaki"] With EEIAA [: 2] ["kimi", "kara", "morainaki"] I feel that I had a lot of chances to see this in the pre-processing of machine learning data. By defining a name for each, you can ** list split **.

Recommended Posts

Learning record 4 (8th day)
Learning record 9 (13th day)
Learning record 5 (9th day)
Learning record 6 (10th day)
Learning record 8 (12th day)
Learning record 2 (6th day)
Learning record 16 (20th day)
Learning record 22 (26th day)
Learning record No. 21 (25th day)
Learning record 13 (17th day) Kaggle3
Learning record No. 10 (14th day)
Learning record 12 (16th day) Kaggle2
Learning record No. 24 (28th day)
Learning record No. 23 (27th day)
Learning record No. 25 (29th day)
Learning record No. 26 (30th day)
Learning record No. 20 (24th day)
Learning record No. 14 (18th day) Kaggle4
Learning record No. 15 (19th day) Kaggle5
Learning record 11 (15th day) Kaggle participation
Programming learning record day 2
Learning record
Learning record No. 18 (22nd day)
Learning record # 3
Learning record # 2
Learning record No. 19 (23rd day)
Learning record No. 29 (33rd day)
Learning record No. 28 (32nd day)
Learning record No. 27 (31st day)
Python learning day 4
Learning record (2nd day) Scraping by #BeautifulSoup
Learning record so far
Learning record (4th day) #How to get the absolute path from the relative path
Linux learning record ① Plan
Effective Python Learning Memorandum Day 15 [15/100]
<Course> Deep Learning: Day2 CNN
Effective Python Learning Memorandum Day 6 [6/100]
Effective Python Learning Memorandum Day 12 [12/100]
Effective Python Learning Memorandum Day 9 [9/100]
Effective Python Learning Memorandum Day 8 [8/100]
Rabbit Challenge Deep Learning 1Day
<Course> Deep Learning: Day1 NN
Effective Python Learning Memorandum Day 14 [14/100]
Effective Python Learning Memorandum Day 1 [1/100]
Subjects> Deep Learning: Day3 RNN
Rabbit Challenge Deep Learning 2Day
Effective Python Learning Memorandum Day 13 [13/100]
Effective Python Learning Memorandum Day 3 [3/100]
Effective Python Learning Memorandum Day 5 [5/100]
Effective Python Learning Memorandum Day 4 [4/100]
Effective Python Learning Memorandum Day 7 [7/100]
Effective Python Learning Memorandum Day 2 [2/100]
Learning record (3rd day) #CSS selector description method #BeautifulSoup scraping
Learning record (6th day) #Set type #Dictionary type #Mutual conversion of list tuple set #ndarray type #Pandas (DataFrame type)
Thoroughly study Deep Learning [DW Day 0]
Deep Learning Specialization (Coursera) Self-study record (C3W1)
[Rabbit Challenge (E qualification)] Deep learning (day2)
Deep Learning Specialization (Coursera) Self-study record (C1W3)
Record the steps to understand machine learning
Deep Learning Specialization (Coursera) Self-study record (C4W3)
[1 copy per day] Classify_images_Using_Python & Machine Learning [Daily_Coding_003]