[Introduction to Data Scientists] Basics of Python ♬

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

Chapter1-1 Work of Data Scientist

Figure 1-1-1 shows. .. .. There is an accountant, but what is it? ?? I felt that domain knowledge was good.

Chapter1-2 Python Basics

From how to use Jupyter Notebook, I will enter but omit it.

1-2-2 Basics of Python

1-2-2-1 Variable

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.
1-2-2-2 Calculation
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

1-2-2-3 Reserved words

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 /)

1-2-3 List and dictionary type

<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
How to use class'list''tuple''set'

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.)

Dictionary type
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
Add dictionary type
dict_data['Apple'] = 100
print(dict_data)

result

{'apple': 100, 'banana': 100, 'orange': 300, 'mango': 400, 'melon': 500, 'Apple': 100}
Dictionary type deletion
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}
Concatenate (combine) dictionaries and add / update multiple elements: update ()
Concatenation (combination)
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}
Add / update multiple elements
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

Summary

·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

[Introduction to Data Scientists] Basics of Python ♬
[Introduction to Data Scientists] Basics of Python ♬ Functions and classes
[Introduction to Data Scientists] Basics of Python ♬ Conditional branching and loops
[Introduction to Data Scientists] Basics of Python ♬ Functions and anonymous functions, etc.
[Introduction to Data Scientists] Basics of Probability and Statistics ♬ Probability / Random Variables and Probability Distribution
Introduction of Python
Basics of Python ①
Basics of python ①
Introduction of Python
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.1-8.2.5)
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.3-8.3.6.1)
[Introduction to Python3 Day 19] Chapter 8 Data Destinations (8.4-8.5)
[Introduction to Python3 Day 18] Chapter 8 Data Destinations (8.3.6.2 to 8.3.6.3)
Basics of Python scraping basics
Introduction to Python language
Introduction to OpenCV (python)-(2)
Basics of python: Output
[Introduction to Udemy Python 3 + Application] 19. Copy of list
[Introduction to cx_Oracle] (Part 3) Basics of Table Reference
[Introduction to Python] How to handle JSON format data
[Introduction to cx_Oracle] (5th) Handling of Japanese data
[Introduction to Python] Basic usage of lambda expressions
[Introduction to Data Scientists] Basics of scientific calculation, data processing, and how to use graph drawing library ♬ Basics of Pandas
[Introduction to Data Scientists] Basics of scientific calculation, data processing, and how to use graph drawing library ♬ Basics of Matplotlib
Summary of tools needed to analyze data in Python
Introduction to Python Django (2) Win
[Introduction to cx_Oracle] Overview of cx_Oracle
List of Python libraries for data scientists and data engineers
[Chapter 5] Introduction to Python with 100 knocks of language processing
Introduction to Anomaly Detection 1 Basics
Reading Note: An Introduction to Data Analysis with Python
Introduction of activities applying Python
Introduction to serial communication [Python]
How to use Python Kivy ① ~ Basics of Kv Language ~
[Introduction to Udemy Python3 + Application] 53. Dictionary of keyword arguments
python: Basics of using scikit-learn ①
[Chapter 3] Introduction to Python with 100 knocks of language processing
[Chapter 2] Introduction to Python with 100 knocks of language processing
[Introduction to cx_Oracle] (Part 11) Basics of PL / SQL Execution
[Introduction to Python] <list> [edit: 2020/02/22]
Introduction to Python (Python version APG4b)
An introduction to Python Programming
[Introduction to Python] Basic usage of the library matplotlib
[Basics of data science] Collecting data from RSS with python
[Introduction to Udemy Python3 + Application] 52. Tupleization of positional arguments
Introduction to Python For, While
Basics of Python × GIS (Part 1)
[Technical book] Introduction to data analysis using Python -1 Chapter Introduction-
[Chapter 4] Introduction to Python with 100 knocks of language processing
[Introduction to Python] How to get the index of data with a for statement
[Introduction to cx_Oracle] (Part 6) DB and Python data type mapping
[Introduction to Data Scientists] Descriptive Statistics and Simple Regression Analysis ♬
[Introduction to Python] Combine Nikkei 225 and NY Dow csv data
[Raspi4; Introduction to Sound] Stable recording of sound input with python ♪
[Python] Introduction to graph creation using coronavirus data [For beginners]
[Introduction to Udemy Python 3 + Application] 31. Comments
Basics of Python x GIS (Part 3)
[Python] How to FFT mp3 data
Introduction to Python Numerical Library NumPy
[Introduction to Python3 Day 1] Programming and Python
[Introduction to Python] <numpy ndarray> [edit: 2020/02/22]