python基礎

bit演算子

print(bin(10))
print(bin(13))
print(bin(10|13))
print(bin(10))
print(bin(13))
print(bin(10&13))

シフト演算

print(bin(15))
print(bin(15<<0))
print(bin(15<<1))
print(bin(15<<2))
print(bin(15<<3))
print(bin(15<<4))

print(bin(15))
print(bin(15>>0))
print(bin(15>>1))
print(bin(15>>2))
print(bin(15>>3))
print(bin(15>>4))

list

list2=[10,20,30]
list2.append(40)
print(list2)

list3=[1,2,3,4,5]
list3.insert(3, 10)
print(list3)

list4=[1,2,3,4,5]
print(len(list4))

dictionary

dic1 = {"A": 1, "B": 2}
dic2 = {}
dic2["key"] = "value"

print(dic1)
print(dic2)

class, instance

class Greet():
    def __init__(self, greet):
        self.value = greet

morning = Greet("おはよう!")
evening = Greet("こんばんわ!")

print(morning.value)
print(evening.value)

__str__(self)は自動的に文字出力する

class Person():
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __str__(self):
        return "Name:" + self.name + ", Age:" + str(self.age)

satoshi = Person("satoshi", 30)
print(satoshi)

import

import numpy as np

def area_circle(r):
    ans = np.pi * r **2
    return ans 
print(area_circle(3))

これをfromを使って以下のように書くことができる。コードを省略できるメリットがある。

from numpy import pi

def area_circle(r):
    ans = pi * r **2
    return ans 
print(area_circle(3))