Python basics

Introduction

This section describes basic operations that medical students and medical professionals (comedical) may need before performing analysis using Python.

Python operating environment

Building a Python environment has become relatively easy. Here, we will introduce two methods.

--Installing Anaconda

--Installing colaboratory

![Screenshot 2020-08-27 8.52.02.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/89181/5c97a06f-01d6-4eec-00da- 114cd26601c9.png)

That's all for preparation.

Please enter an appropriate number in the cell of colaboratory and execute it. If you want to execute it, move the cursor around the red arrow and it will be displayed like this. Click it or press shift + enter. スクリーンショット 2020-08-27 8.32.57.png

Characters and numbers that can be handled by Python

Python handles three data types.

--int type: 1, 2, 0, -1, -2000, ⋯ --Float type: 2.3, 3.1415, -0.5, 0.0, -10.4 ⋯ --String type:'abc','apple', "orange", "support",'Tokyo', ⋯ --True / False: True, False

list

It's one of the basics of Python. If you can understand this mechanism, it will be very easy to operate 2D arrays (tables arranged vertically and horizontally) created by Excel. In Excel, which is preferred in the medical field, when the amount of data becomes huge, it tends to be complicated because it takes time to process and the number of operations increases. If you can imagine working with lists using variable names in Python, there is no complicated work that depends on the amount of data. So, let's learn about the important point that amateurs have, the operation of Python lists.

Basic list type

Treat the data existing in $ [] $ (square brackets) as one data group,

python


[0,1,2,3,4,5,6,7,8,9]

Write like this.

The list is

python


[0.4,1,5,"Tokyo","Japan"]

You can mix numbers and letters like this. You can also store the list in a list.

python


[[0,1,2,3,4],102,103,104,105,["Hokkaido","Tokyo","Japan"]]

In this way, lists are excellent because they can handle various data at once.

Variable name

As illustrated earlier, we don't actually create or type in the list each time. In many cases, give a temporary name to the retrieved list (data). That name is called the variable name. At that time, = is used. The meaning of this symbol is substitute, but it may be interpreted as the same. For example

python


myd = [[0,1,2,3,4],102,103,104,105,["Hokkaido","Tokyo","Japan"]]

If you write in, myd to the left of=is the variable name, and the right is the list (data). This means that the right side of = is assigned to the left myd. And if you change the cell to print (myd) or myd, you will see the list to the right of=. ** There are rules for variable names, such as avoiding reserved words ** (details omitted)

python


print(myd)

index

In order to be able to handle 2D arrays, you should know that ʻindex`. In short, the elements (the contents of the list) are numbered. The numbers are 0,1,2, $ \ cdots $ from the left of the list. You can use that number to retrieve, modify, or delete the data in the list. (Note) Please note that it starts from 0. For example

python


print(myd[1])  ##-->102 is acquired
print(myd[0])  ##--> [0,1,2,3,4]Is obtained

Write like this.

Use : when the data you want to get has a range. The basic shape is

python


print(myd[:])

Is

python


print(myd[1:3]) ##--> [102,103]Is obtained
print(myd[1:4]) ##--> [102,103,104]Is obtained

Write like. Also,

python


print(myd[::2])  ##-->[[0,1,2,3,4],103,105]

Then, every other one is taken out.

You can also specify the list in order from the right. For example

python


print(myd[-1])  ##-->['Hokkaido','Tokyo','Japan']
print(myd[-2])  ##-->105
print(myd[-3:])  ##-->[104, 105, ['Hokkaido', 'Tokyo', 'Japan']]
print(myd[-4:-1])  ##-->[103, 104, 105]

You can write like this. Let's execute it one by one and check the output result. These are some of the most commonly used techniques.

List in list

I mentioned earlier that you can include a list in a list. If a two-dimensional (array) list is created by aligning the number of elements in the list, it will represent an Excel table. Here is a simple example.

python


df = [[11,12,13],[21,22,23],[31,32,33],[41,42,43],[51,52,53],
[61,62,63],[71,72,73],[81,82,83],[91,92,93]]

This is a list of list elements. The elements of the list, which are those elements, are numerical values. It is stored as one piece of data in the variable name df.

python


df[0] ##-->[11,12,13]
df[0:4] ##-->[[11,12,13],[21,22,23],[31,32,33],[41,42,43]]

And the list. If you want to specify a list of lists,

python


df[0][2] ##-->13
df[3][0] ##-->41

Write by connecting indexes like this. These two numbers can be regarded as the ◯ th in the vertical direction and the □ th in the horizontal direction. In other words, you can think of it as reading a table.

Finally

If you can understand so far, I think that you can handle tables in Python. There are many other useful things, but this time I'll leave it here.

Recommended Posts

Python basics
Python basics ④
Python basics ③
Python basics
Python basics
Python basics ③
Python basics ②
Python basics ②
Python basics: list
Python basics memorandum
#Python basics (#matplotlib)
Python CGI basics
Python basics: dictionary
Basics of python ①
Python slice basics
#Python basics (scope)
#Python basics (#Numpy 1/2)
#Python basics (#Numpy 2/2)
#Python basics (functions)
Python array basics
Python profiling basics
Python #Numpy basics
Python basics: functions
#Python basics (class)
Python basics summary
Python basics ② for statement
Python: Unsupervised Learning: Basics
Python
Basics of Python scraping basics
Python basics 8 numpy test
Errbot: Python chatbot basics
#Python DeepLearning Basics (Mathematics 1/4)
Python basics: Socket, Dnspython
# 4 [python] Basics of functions
Basics of python: Output
python: Basics of using scikit-learn ①
Python basics: conditions and iterations
Paiza Python Primer 4: List Basics
Basics of Python × GIS (Part 1)
kafka python
Basics of Python x GIS (Part 3)
Python Summary
Built-in python
Paiza Python Primer 5: Basics of Dictionaries
Python comprehension
Python technique
Studying python
Python 2.7 Countdown
Python FlowFishMaster
SNS Python basics made with Flask
Python service
python tips
Linux basics
python function ①
Python memo
ufo-> python (3)
Python comprehension
install python
NumPy basics
Python Memorandum 2
python memo