「BaiduスカルパイロットゼロベースPython速成営」学習ノート

19934 ワード

レッスンリンク:https://aistudio.baidu.com/aistudio/course/introduce/7073
以前からPythonを使っていたので、基礎的な文法は記録されませんでした.
文字列
反転
s = 'Hello world'
rev_s = s[::-1]

出力のフォーマット
num = 88
s = 'Hello'
#  {}    Python  
print(f'{s} is a string, {num} is a number.'
print(f'{num * 100} is a number mutiply 100')

リスト生成
#    
num = range(10)
# 0-10      10,  num_list 
num_list = [n * 10 for n in num if n%2 == 1]

従来、リストの計算は、ループによって実現されていた.
#    
num_list = []
for n in num:
    if n%2 == 1:
        num_list.append(n * 10)

2つの方式を比較して、方式1は性能の上で一定の優位性を持っています.5000000のデータ量を処理する場合、方式は1時間5秒程度、方式は2時間8秒程度かかります.
ビルダー
#    ,         
num_gene = (n * 10 for n in num if n%2 == 1)
next(num_gene)

#    ,         
def my_gene():
    n = 0
    while True:
        if n%2 == 1:
            yield n * 10
        n += 1

num_gene = my_gene()
next(num_gene)

ジェネレータの利点は、すべてのデータを一度に生成するのではなく、メモリをあまり消費しないことです.
関数#カンスウ#
パラメータ
#     ,           ,     
def fun1(arg1, arg2):
	pass

#     ,    
def fun2(arg1=1, arg2='b')
	pass

#     ,     ,   tupel  
def fun3(*arg):
	pass

#      ,  dict  ,         ,fun4(arg1=1, arg2='b')
def fun4(**arg):
	pass

#        ,arg2          ,fun5(1, arg2=2)
def fun5(arg1, *, arg2):
	pass


匿名関数
#      lambda    :    
add = lambda arg1 arg2: arg1+arg2
add(1, 2)

高次関数
# map,         
def fun(n):
    return n * 10

list(map(fun, range(10)))

# reduce,                 
# 0   1   2   3   4
#   1
#       3
#           6
#               10
def fun(n, m):
    return n + m

from functools import reduce
reduce(fun, range(5))

デコレーション
#       ,      
#       :  、  
import time
#         
def timer(func):
    def wrapper(data):
        s_time = time.time()
        result = func(data)
        e_time = time.time()
        print(f'{func.__name__} worktime is {(e_time - s_time) * 1000}')
        return result
    return wrapper

@timer
def function(data):
	pass

バイアス関数
#       
def func(arg1, arg2):
    pass
    
from functools import partial
f = partial(func, arg2='b')
f(1)

オブジェクト向け
クラス属性
#   c      a  ,      cha   ,       a  
class c:
	a = 'a'
    def __init__(self):
        pass
    
    def cha(self, b):
    	self.a = b

クラスメソッド
#   @classmethod          ,         
class c:
    def __init__(self):
        pass

    def fun1(self):
        print(self.fun1.__name__)
    
    @classmethod
    def fun2(self):
        print(self.fun2.__name__)
# fun1     ,         
# c.fun1()

c.fun2()

アクセス権
#              __,      
class c:
    def __init__(self, name):
    	self.__name = name

    def __fun(self):
        pass

継承
class a:
    def __init__(self, arg1):
        self.arg1 = arg1
        pass

    def fun(self):
        print(self.fun.__name__)

#   
class b(a):
    def __init__(self, arg1, arg2):
        a.__init__(self, arg1)
        self.arg2 = arg2

B = b(1, 'b')
B.fun()

#    
class father:
	def __init__(self):
		self.a = 'F'
	
	def funcF(self):
		pass

class mother:
	def __init__(self):
		self.a = 'MF'
		self.b = 'M'
	
	def funcM(self):
		pass

class child(father, mother):
	pass

c = child()
# c.a     ,        
#   c.a 'F'
c.a
c.b
c.funcF()
c.funcM()

以上が今回の学習の主な収穫であり、一部の対象向けの内容が整理されていないため、再補完する機会がある.