From today, ["Data Scientist Training Course at the University of Tokyo"](https://www.amazon.co.jp/%E6%9D%B1%E4%BA%AC%E5%A4%A7%E5%AD% A6% E3% 81% AE% E3% 83% 87% E3% 83% BC% E3% 82% BF% E3% 82% B5% E3% 82% A4% E3% 82% A8% E3% 83% B3% E3% 83% 86% E3% 82% A3% E3% 82% B9% E3% 83% 88% E8% 82% B2% E6% 88% 90% E8% AC% 9B% E5% BA% A7-Python% E3% 81% A7% E6% 89% 8B% E3% 82% 92% E5% 8B% 95% E3% 81% 8B% E3% 81% 97% E3% 81% A6% E5% AD% A6% E3% 81% B6% E3% 83% 87% E2% 80% 95% E3% 82% BF% E5% 88% 86% E6% 9E% 90-% E5% A1% 9A% E6% 9C% AC% E9% 82 I will read% A6% E5% B0% 8A / dp / 4839965250 / ref = tmm_pap_swatch_0? _ Encoding = UTF8 & qid = & sr =) and summarize the parts that I have some doubts or find useful. Therefore, I think the synopsis will be straightforward, but please read it, thinking that the content has nothing to do with this book. Also, run the environment with Python 3.6 until problems occur. Python 3.6.10 |Anaconda, Inc.| (default, Jan 7 2020, 15:18:16) [MSC v.1916 64 bit (AMD64)] on win32
Figure 1-1-1 shows. .. .. There is an accountant, but what is it? ?? I felt that domain knowledge was good.
From how to use Jupyter Notebook, I will enter but omit it.
The code below
msg = 'test'
print(msg)
print(msg[0])
print(msg[1])
print(msg[5])
result
test
t
e
Traceback (most recent call last):
File "msg-ex.py", line 5, in <module>
print(msg[5])
IndexError: string index out of range
If you get an error, it says google, so try google. There are a lot of answers, but this time I will post the ones from the Stack Overflow site that I often use. Even with this kind of error, it is likely that you will stumble. IndexError: string index out of range in Python [closed] The answer is as follows
As other people have indicated, s[s.Length] isn't actually a valid index; indices are in the closed interval [0, length - 1](i.e. the last valid index is length - 1 and the first index is 0). Note that this isn't true for every language (there are languages where the first index is 1), but it's certainly true for Python.
The following is a Google translation.
As others have shown, s[s.Length]Is not really a valid index. Index is closed interval[0、length-1](Thatis,thelastvalidindexislength-1andthefirstindexis0).Notethatthisisnotthecaseforalllanguages(somelanguageshaveaninitialindexof1), but it is certainly true for Python.
data=1
print(data)
data = data+10
print(data)
result
1
11
The important story here is that the message "Variable names are important" is important. I think you should check it below. List of Python naming conventions ** It's a good idea to read the coding conventions once. ** ** Standard python coding convention (reference URL [B-4]; p413) PEP: 8 Python Code Coding Standards Included in Python Standard Library PEP Naming Convention
Reserved words and built-in function names cannot be used as variable names. [Python3.7 reserved word or keyword list and built-in function acquisition / confirmation method](https://wpchiraura.xyz/python3-7-reserved-words-keyword-list-and-built-in-function-acquisition-confirmation- method /)
<class 'list'>
data_list = [1,2,3,4,5,6,7,8,9]
print(data_list)
print(type(data_list))
print(data_list[1])
print(len(data_list))
result
[1, 2, 3, 4, 5, 6, 7, 8, 9]
<class 'list'>
2
9
The list can be changed as follows.
data_list = [1,2,3,4,5,6,7,8,9]
print(data_list*2)
data_list.append(4)
print(data_list)
data_list.remove(5)
print(data_list)
print(data_list.pop(6))
print(data_list)
del data_list[3:6]
print(data_list)
result
[1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 4]
[1, 2, 3, 4, 6, 7, 8, 9, 4]
8
[1, 2, 3, 4, 6, 7, 9, 4]
[1, 2, 3, 9, 4]
【reference】 Clear, pop, remove, del to remove list (array) elements in Python <class 'tuple'> tuple is similar to list but cannot be modified.
data_tuple = (1,2,3,4,5,6,7,8,9)
print(data_tuple)
print(type(data_tuple))
print(data_tuple[1])
print(len(data_tuple))
result
(1, 2, 3, 4, 5, 6, 7, 8, 9)
<class 'tuple'>
2
9
<class 'set'> set is a ** set ** and does not allow duplicate elements
data_set = {1,2,3,4,5,6,7,8,9}
print(data_set)
print(type(data_set))
#print(data_set[1])
print(len(data_set))
result Comment out line TypeError: 'set' object does not support indexing set does not support indexes
{1, 2, 3, 4, 5, 6, 7, 8, 9}
<class 'set'>
9
list has a high degree of freedom, and elements can be added and deleted. tuple has no freedom and cannot be changed set is a collection of non-overlapping elements (elements that do not have the same value, unique elements), and can perform set operations such as union, intersection, and difference. Since it does not allow duplicate elements, it can be made a unique element by the following changes. When combined, the following can be done.
data_list = [1,2,3,4,5,6,7,2,3,4,8,9]
print(data_list)
data_tuple = tuple(data_list)
print(data_tuple)
data_set = set(data_list)
print(data_set)
data_list_new = list(data_set)
print(data_list_new)
data_tuple_new = tuple(data_set)
print(data_tuple_new)
data_set.discard(6)
print(data_set)
data_set.add(11)
print(data_set)
result
[1, 2, 3, 4, 5, 6, 7, 2, 3, 4, 8, 9]
(1, 2, 3, 4, 5, 6, 7, 2, 3, 4, 8, 9)
{1, 2, 3, 4, 5, 6, 7, 8, 9}
[1, 2, 3, 4, 5, 6, 7, 8, 9]
(1, 2, 3, 4, 5, 6, 7, 8, 9)
{1, 2, 3, 4, 5, 7, 8, 9}
{1, 2, 3, 4, 5, 7, 8, 9, 11}
【reference】 Know the Python set Set operation with Python, set type (union, intersection and subset judgment, etc.)
dict_data = {'apple':100,'banana':100,'orange':300,'mango':400,'melon':500}
print(dict_data['apple'])
print(dict_data['apple']+dict_data['orange'])
result
100
400
dict_data['Apple'] = 100
print(dict_data)
result
{'apple': 100, 'banana': 100, 'orange': 300, 'mango': 400, 'melon': 500, 'Apple': 100}
removed_value = dict_data.pop('banana')
print(removed_value)
print(dict_data)
result
100
{'apple': 100, 'orange': 300, 'mango': 400, 'melon': 500, 'Apple': 100}
Another deletion
del dict_data['mango']
print(dict_data)
result
{'apple': 100, 'orange': 300, 'melon': 500, 'Apple': 100}
d1 = {'apple': 100, 'banana': 100}
d2 = {'orange': 300, 'mango': 400}
d3 = {'melon': 500, 'Apple': 100}
d1.update(d2)
print(d1)
d1.update(**d2, **d3)
print(d1)
result
{'apple': 100, 'banana': 100, 'orange': 300, 'mango': 400}
{'apple': 100, 'banana': 100, 'orange': 300, 'mango': 400, 'melon': 500, 'Apple': 100}
d={'apple': 100, 'banana': 100}
print(d)
d.update([('orange', 300), ('mango', 400), ('melon', 500), ('Apple', 100)])
print(d)
result
{'apple': 100, 'banana': 100}
{'apple': 100, 'banana': 100, 'orange': 300, 'mango': 400, 'melon': 500, 'Apple': 100}
【reference】 Add elements to dictionaries with Python, concatenate (combine) dictionaries
·variable ·Calculation -Reserved words and built-in functions ・ Lists, sets, and dictionaries Saw
It is easy to understand if you go back to the basics and arrange them side by side.
Recommended Posts