Python入門1. 計算
In [1]:
10/3
Out[1]:
3.3333333333333335
In [2]:
10//3
Out[2]:
3
In [3]:
10%3
Out[3]:
1
In [4]:
2**10
Out[4]:
1024
In [5]:
2**0.5
Out[5]:
1.4142135623730951
2. 変数
In [6]:
x = 2
type(x)
Out[6]:
int
In [7]:
y = 0.5
type(y)
Out[7]:
float
In [8]:
help( type )
Help on class type in module builtins:

class type(object)
 |  type(object_or_name, bases, dict)
 |  type(object) -> the object's type
 |  type(name, bases, dict) -> a new type
 |  
 |  Methods defined here:
 |  
 |  __call__(self, /, *args, **kwargs)
 |      Call self as a function.
 |  
 |  __delattr__(self, name, /)
 |      Implement delattr(self, name).
 |  
 |  __dir__(...)
 |      __dir__() -> list
 |      specialized __dir__ implementation for types
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __init__(self, /, *args, **kwargs)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  __instancecheck__(...)
 |      __instancecheck__() -> bool
 |      check if an object is an instance
 |  
 |  __new__(*args, **kwargs)
 |      Create and return a new object.  See help(type) for accurate signature.
 |  
 |  __prepare__(...)
 |      __prepare__() -> dict
 |      used to create the namespace for the class statement
 |  
 |  __repr__(self, /)
 |      Return repr(self).
 |  
 |  __setattr__(self, name, value, /)
 |      Implement setattr(self, name, value).
 |  
 |  __sizeof__(...)
 |      __sizeof__() -> int
 |      return memory consumption of the type object
 |  
 |  __subclasscheck__(...)
 |      __subclasscheck__() -> bool
 |      check if a class is a subclass
 |  
 |  __subclasses__(...)
 |      __subclasses__() -> list of immediate subclasses
 |  
 |  mro(...)
 |      mro() -> list
 |      return a type's method resolution order
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __abstractmethods__
 |  
 |  __dict__
 |  
 |  __text_signature__
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __base__ = <class 'object'>
 |      The most base type
 |  
 |  __bases__ = (<class 'object'>,)
 |  
 |  __basicsize__ = 864
 |  
 |  __dictoffset__ = 264
 |  
 |  __flags__ = -2146675712
 |  
 |  __itemsize__ = 40
 |  
 |  __mro__ = (<class 'type'>, <class 'object'>)
 |  
 |  __weakrefoffset__ = 368

In [9]:
my_university = '近大'
type( my_university )
Out[9]:
str
In [10]:
n_list = [1,2,3,4,5]
type( n_list )
Out[10]:
list
3. maath モジュール
In [11]:
import math
math.log(2)
Out[11]:
0.6931471805599453
In [12]:
math.log(2,10)
Out[12]:
0.30102999566398114
In [13]:
degree = math.pi/180
math.sin(30*degree)
Out[13]:
0.49999999999999994
4. calendarモジュール
In [14]:
import calendar
print( calendar.month(2019,4) )
     April 2019
Mo Tu We Th Fr Sa Su
 1  2  3  4  5  6  7
 8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30

In [15]:
for n in range(1,13):
    print( calendar.month(2019,n) )
    January 2019
Mo Tu We Th Fr Sa Su
    1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

   February 2019
Mo Tu We Th Fr Sa Su
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28

     March 2019
Mo Tu We Th Fr Sa Su
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

     April 2019
Mo Tu We Th Fr Sa Su
 1  2  3  4  5  6  7
 8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30

      May 2019
Mo Tu We Th Fr Sa Su
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

     June 2019
Mo Tu We Th Fr Sa Su
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

     July 2019
Mo Tu We Th Fr Sa Su
 1  2  3  4  5  6  7
 8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31

    August 2019
Mo Tu We Th Fr Sa Su
          1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

   September 2019
Mo Tu We Th Fr Sa Su
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30

    October 2019
Mo Tu We Th Fr Sa Su
    1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

   November 2019
Mo Tu We Th Fr Sa Su
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30

   December 2019
Mo Tu We Th Fr Sa Su
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31

5. urllibモジュール
In [16]:
import urllib
In [17]:
import urllib.request as req
url = 'https://buturi.heteml.jp/Python/'
res = req.urlopen( url )
html = res.read()
html = html.decode('utf-8')
print(html)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Python</title>
<style>
html, body{ width: 100%; height:100%; background-color: white;}
body{width: 80%; margin: 0 auto; background-color: antiquewhite; padding: 0.5em 1em;
border-left: 1px solid lightgray; border-right: 1px solid lightgray;}
</style>
</head>
<body>
<article>
<h1>Python</h1>

<ul>
<li>Pythonインタプリタの起動:Anaconda Promptを起動して,python &#9166;
<li>IPythonの起動:Anaconda Promptを起動して,ipython &#9166;
<ol>
<li>Tab補完機能
</ol>
</ul>

<ol>
<li><a href="./find_root.pdf">平方根の近似値を2分法により求める</a>
<li><a href="./moving_balls.pdf">moving_balls.pdf:理想気体</a>
<li><a href="./tkinter_celsius_to_fahrenheit.pdf">tkinter:摂氏・華氏変換アプリ</a>
<li><a href="./pythonCGI.pdf">cgi解説</a>
</ol>

<h3>Python参考</h3>
<ul>
<li><a href="https://perso.limsi.fr/pointal/_media/python:cours:mementopython3-english.pdf">python cheatsheet</a>
<li><a href="https://docs.python.jp/">Python documentation</a>
<li><a href="https://docs.python.jp/3/library/index.html">標準ライブラリ</a>
<li><a href="https://docs.python.jp/3.3/library/functions.html">組み込み関数</a>
<li><a href="https://matplotlib.org/index.html">Matplotlib:Python 2D plotting library</a>
</ul>

</body>
</html>


In [18]:
req.getproxies()
Out[18]:
{}
  1. matplotlibモジュール
In [22]:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-10,10,100)
y = np.exp(-x*x)
plt.xlabel('x')
plt.grid()
plt.plot(x,y)
plt.title('y = exp(-x*x)')
plt.hlines(0,-10,10)
plt.vlines(0,-0,1.2)
plt.xlim(-10,10)
plt.ylim(0,1)
plt.show()
7. randomモジュール
In [20]:
import random
janken = ['グー','チョキ','パー']
for n in range(10):
    print(n,': ', random.choice(janken) )
0 :  グー
1 :  パー
2 :  パー
3 :  グー
4 :  グー
5 :  パー
6 :  チョキ
7 :  グー
8 :  チョキ
9 :  チョキ
8. オブジェクトの定義
In [21]:
class Student:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def introduct(self):
        print( 'My name is', self.name, ',', self.age, 'years old.' )
        
aoki = Student('青木', '20')
aoki.introduct()
My name is 青木 , 20 years old.